SQL查询执行缓慢

时间:2018-10-19 19:32:48

标签: mysql mysql-5.5

查询意外地花费了三倍的时间来执行:

SELECT p.rowid as 'promosk_rowid',s.id_pro as 'promosId',s.*,p.*,c.*,m.*
FROM promos s
LEFT JOIN promost t ON t.pro_prs=s.id_pro
LEFT JOIN promrule m ON m.pro_prr=s.id_pro
LEFT JOIN promosk p ON p.pro_psk=s.id_pro,coupons c
WHERE c.id_cpn=s.cpn_pro
AND c.typ_cpn='1'
AND c.act_cpn <> 'N'
AND s.act_pro <> 'N'
AND p.rowid IS NULL
AND t.rowid IS NULL
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') >= s.sd_pro
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') <= s.ed_pro

UNION

SELECT p.rowid as 'promosk_rowid',s.id_pro as 'promosId',s.*,p.*,c.*,m.*
FROM promos s
LEFT JOIN promost t ON t.pro_prs=s.id_pro
LEFT JOIN promrule m ON m.pro_prr=s.id_pro,promosk p,articles a,coupons c
WHERE s.id_pro=p.pro_psk
AND c.typ_cpn='1'
AND c.id_cpn=s.cpn_pro
AND c.act_cpn <> 'N'
AND s.act_pro <> 'N'
AND (a.article='0030269484')
AND (p.art_psk = a.article OR p.art_psk = '')
AND (p.coll_psk = a.collect OR p.coll_psk = '')
AND (p.type_psk = a.type_c OR p.type_psk = '')
AND (p.pat_psk = a.pat_c OR p.pat_psk = '')
AND (p.scl_psk = a.sclass_c OR p.scl_psk = '')
AND (p.cl1_psk = a.cl1_c OR p.cl1_psk = '')
AND (p.cl2_psk = a.cl2_c OR p.cl2_psk = '')
AND (p.ven_psk = a.model_c OR p.ven_psk = '')
AND (p.colr_psk = a.color_c OR p.colr_psk = '')
AND (p.siz_psk = a.size1 OR p.siz_psk = '')
AND (p.styl_psk = a.venitem OR p.styl_psk = '')
AND t.rowid IS NULL
AND p.rowid IS NOT NULL
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') >= s.sd_pro
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') <= s.ed_pro

UNION

SELECT p.rowid as 'promosk_rowid',s.id_pro as 'promosId',s.*,p.*,c.*,m.*
FROM promos s
LEFT JOIN promost t ON t.pro_prs=s.id_pro
LEFT JOIN promrule m ON m.pro_prr=s.id_pro
LEFT JOIN promosk p ON p.pro_psk=s.id_pro,coupons c,stores r
WHERE c.id_cpn=s.cpn_pro
AND c.typ_cpn='1'
AND c.act_cpn <> 'N'
AND s.act_pro <> 'N'
AND t.rowid IS NOT NULL
AND p.rowid IS NULL
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') >= s.sd_pro
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') <= s.ed_pro
AND r.st_num='218657'
AND (t.stc_prs = r.st_num OR t.stc_prs = '-1')
AND (t.clnt_prs = r.st_clnt OR t.clnt_prs = '-1')

UNION

SELECT p.rowid as 'promosk_rowid',s.id_pro as 'promosId',s.*,p.*,c.*,m.*
FROM promos s
LEFT JOIN promost t ON t.pro_prs=s.id_pro
LEFT JOIN promrule m ON m.pro_prr=s.id_pro,
promosk p,
articles a,
coupons c,
stores r
WHERE s.id_pro=p.pro_psk
AND c.typ_cpn='1'
AND c.id_cpn=s.cpn_pro
AND c.act_cpn <> 'N'
AND s.act_pro <> 'N'
AND (a.article='0030269484')
AND (p.art_psk = a.article OR p.art_psk = '')
AND (p.coll_psk = a.collect OR p.coll_psk = '')
AND (p.type_psk = a.type_c OR p.type_psk = '')
AND (p.pat_psk = a.pat_c OR p.pat_psk = '')
AND (p.scl_psk = a.sclass_c OR p.scl_psk = '')
AND (p.cl1_psk = a.cl1_c OR p.cl1_psk = '')
AND (p.cl2_psk = a.cl2_c OR p.cl2_psk = '')
AND (p.ven_psk = a.model_c OR p.ven_psk = '')
AND (p.colr_psk = a.color_c OR p.colr_psk = '')
AND (p.siz_psk = a.size1 OR p.siz_psk = '')
AND (p.styl_psk = a.venitem OR p.styl_psk = '')
AND t.rowid IS NOT NULL
AND p.rowid IS NOT NULL
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') >= s.sd_pro
AND STR_TO_DATE('2018-09-25','%Y-%m-%d') <= s.ed_pro
AND r.st_num='218657'
AND (t.stc_prs = r.st_num OR t.stc_prs = '-1')
AND (t.clnt_prs = r.st_clnt OR t.clnt_prs = '-1')
;

执行计划是:

+----+--------------+----------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------+---------+---------+-------------------+-------+--------------------------------------+
| id | select_type  | table          | type   | possible_keys                                                                                                                                      | key     | key_len | ref               | rows  | Extra                                |
+----+--------------+----------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------+---------+---------+-------------------+-------+--------------------------------------+
|  1 | PRIMARY      | c              | ref    | PRIMARY,typ_cpn                                                                                                                                    | typ_cpn | 5       | const             |    52 | Using where                          |
|  1 | PRIMARY      | s              | ref    | cpn_pro                                                                                                                                            | cpn_pro | 13      | pandora.c.id_cpn  |     1 | Using where                          |
|  1 | PRIMARY      | t              | ref    | pro_prs                                                                                                                                            | pro_prs | 5       | pandora.s.id_pro  |    25 | Using where; Using index; Not exists |
|  1 | PRIMARY      | m              | ref    | pro_prr                                                                                                                                            | pro_prr | 5       | pandora.s.id_pro  |     1 |                                      |
|  1 | PRIMARY      | p              | ref    | pro_psk                                                                                                                                            | pro_psk | 5       | pandora.s.id_pro  |   781 | Using where; Not exists              |
|  2 | UNION        | a              | const  | PRIMARY,type_c,model_c,collect,color_c,pat_c,sclass_c,cl1_c,cl2_c,venitem,venitem__act,model_c__venitem__color_c,collect__venitem,model_c__venitem | PRIMARY | 12      | const             |     1 |                                      |
|  2 | UNION        | p              | ALL    | rowid,pro_psk,art_psk                                                                                                                              | NULL    | NULL    | NULL              | 34364 | Using where                          |
|  2 | UNION        | s              | eq_ref | PRIMARY,cpn_pro                                                                                                                                    | PRIMARY | 4       | pandora.p.pro_psk |     1 | Using where                          |
|  2 | UNION        | t              | ref    | pro_prs                                                                                                                                            | pro_prs | 5       | pandora.p.pro_psk |    25 | Using where; Using index; Not exists |
|  2 | UNION        | m              | ref    | pro_prr                                                                                                                                            | pro_prr | 5       | pandora.p.pro_psk |     1 |                                      |
|  2 | UNION        | c              | eq_ref | PRIMARY,typ_cpn                                                                                                                                    | PRIMARY | 12      | pandora.s.cpn_pro |     1 | Using where                          |
|  3 | UNION        | r              | const  | PRIMARY,st_clnt                                                                                                                                    | PRIMARY | 4       | const             |     1 |                                      |
|  3 | UNION        | c              | ref    | PRIMARY,typ_cpn                                                                                                                                    | typ_cpn | 5       | const             |    52 | Using where                          |
|  3 | UNION        | s              | ref    | PRIMARY,cpn_pro                                                                                                                                    | cpn_pro | 13      | pandora.c.id_cpn  |     1 | Using where                          |
|  3 | UNION        | m              | ref    | pro_prr                                                                                                                                            | pro_prr | 5       | pandora.s.id_pro  |     1 |                                      |
|  3 | UNION        | t              | ref    | rowid,pro_prs                                                                                                                                      | pro_prs | 5       | pandora.s.id_pro  |    25 | Using where                          |
|  3 | UNION        | p              | ref    | pro_psk                                                                                                                                            | pro_psk | 5       | pandora.t.pro_prs |   781 | Using where; Not exists              |
|  4 | UNION        | a              | const  | PRIMARY,type_c,model_c,collect,color_c,pat_c,sclass_c,cl1_c,cl2_c,venitem,venitem__act,model_c__venitem__color_c,collect__venitem,model_c__venitem | PRIMARY | 12      | const             |     1 |                                      |
|  4 | UNION        | r              | const  | PRIMARY,st_clnt                                                                                                                                    | PRIMARY | 4       | const             |     1 |                                      |
|  4 | UNION        | p              | ALL    | rowid,pro_psk,art_psk                                                                                                                              | NULL    | NULL    | NULL              | 34364 | Using where                          |
|  4 | UNION        | s              | eq_ref | PRIMARY,cpn_pro                                                                                                                                    | PRIMARY | 4       | pandora.p.pro_psk |     1 | Using where                          |
|  4 | UNION        | m              | ref    | pro_prr                                                                                                                                            | pro_prr | 5       | pandora.p.pro_psk |     1 |                                      |
|  4 | UNION        | t              | ref    | rowid,pro_prs                                                                                                                                      | pro_prs | 5       | pandora.p.pro_psk |    25 | Using where                          |
|  4 | UNION        | c              | eq_ref | PRIMARY,typ_cpn                                                                                                                                    | PRIMARY | 12      | pandora.s.cpn_pro |     1 | Using where                          |
| NULL | UNION RESULT | <union1,2,3,4> | ALL    | NULL                                                                                                                                               | NULL    | NULL    | NULL              |  NULL |                                      |
+----+--------------+----------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------+---------+---------+-------------------+-------+--------------------------------------+
25 rows in set (0.19 sec)

使用InnoDB表会发生此问题。 任何其他信息,请让我知道。谢谢。

0 个答案:

没有答案