0

以下のように 4 つのテーブルがあります。これらのテーブルを結合しようとしていますが、クエリの実行に時間がかかりすぎています。これを最適化する方法を教えてください。以下にしようとしています

1)サブクエリを使用して、入力日付範囲に基づいてテーブルを作成しています

2) フロントエンドで結果を州 - >地区 - >銀行としてフィルタリングできるように、銀行、地区、州の順に結果をグループ化する必要があります。

3)また、not like句を使用して行っているジャンクデータを回避する必要があります。

select substring(a.ifsc,1,4) as code,
       s.new_state as state, 
       s.state_id as stid,
       d.new_dist as dist,
       b.ifbank as bank,
       count(a.amt) as num,
       sum(a.amt) as amt from 
             (SELECT * FROM mtr where orgdate between '$fdate_new' and '$tdate_new') 
       as a JOIN ifsc b on b.ifscd=a.ifsc 
       JOIN user c on a.excd=c.mtr 
       JOIN state_mapping s on b.state=s.org_state 
       JOIN dist_mapping d on b.dist=d.org_dist 
       where 
             s.state_id ='$stid' and 
             TRIM(d.new_dist) <> '' and 
             d.new_dist IS NOT NULL 
       group by bank,dist order by amt desc;

dist_mapping テーブル

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| org_dist | varchar(20) | YES  |     | NULL    |       |
| new_dist | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

ifsc テーブル

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ifscd   | varchar(11) | NO   | PRI |         |       |
| ifscbr  | varchar(40) | YES  |     | NULL    |       |
| ifbank  | varchar(40) | YES  |     | NULL    |       |
| newifsc | varchar(11) | YES  |     | NULL    |       |
| dist    | varchar(20) | YES  |     | NULL    |       |
| state   | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

state_mapping テーブル

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| org_state | varchar(20) | YES  |     | NULL    |       |
| new_state | varchar(20) | YES  |     | NULL    |       |
| state_id  | int(2)      | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

ユーザーテーブル

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| excode  | int(2)      | YES  |     | NULL    |       |
| mtr  | int(2)      | YES  |     | NULL    |       |
| exname  | varchar(40) | YES  |     | NULL    |       |
| country | varchar(10) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

mysql> desc mtr;
+---------+---------------------+------+-----+---------+-------+
| Field   | Type                | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+-------+
| excd    | int(2)              | NO   | PRI | 0       |       |
| orgdate | date                | YES  |     | NULL    |       |
| amt     | double(12,2)        | YES  |     | NULL    |       |
| obank   | int(1)              | YES  |     | NULL    |       |
| brcd    | int(5)              | YES  |     | NULL    |       |
| brname  | varchar(40)         | YES  |     | NULL    |       |
| rname   | varchar(40)         | YES  |     | NULL    |       |
| bname   | varchar(40)         | YES  |     | NULL    |       |
| baddr   | varchar(60)         | YES  |     | NULL    |       |
| mob     | varchar(32)         | YES  |     | NULL    |       |
| ifsc    | varchar(12)         | YES  |     | NULL    |       |
+---------+---------------------+------+-----+---------+-------+
4

1 に答える 1

0

サブクエリは結合に比べて時間がかかりますこれを試してください

select substring(a.ifsc,1,4) as code,
       s.new_state as state, 
       s.state_id as stid,
       d.new_dist as dist,
       b.ifbank as bank,
       count(a.amt) as num,
       sum(a.amt) as amt 
       From  mtr  as a 
       JOIN ifsc b on b.ifscd=a.ifsc and orgdate between '$fdate_new' and '$tdate_new'
       JOIN user c on a.excd=c.mtr 
       JOIN state_mapping s on b.state=s.org_state 
       JOIN dist_mapping d on b.dist=d.org_dist 
       where 
             s.state_id ='$stid' and `enter code here`
             TRIM(d.new_dist) <> '' and 
             d.new_dist IS NOT NULL 
       group by bank,dist order by amt desc;
于 2013-10-24T06:15:59.533 に答える