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 回答