查询中包含许多嵌套子句的查询速度缓慢

时间:2018-08-22 11:07:06

标签: mysql in-clause

我在具有许多嵌套in子句的查询中面临速度缓慢。

查询如下:

select SA0_.sid as stid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid in (select AC1_.acid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid in (select AC1_.acid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid in (select AC1_.acid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid in (select AC1_.acid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid in (select AC1_.acid from table_starea SA0_ left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid where SA0_.sid in (select SB1_.sid from table_arstaff AC0_ left outer join table_stf SB1_ on AC0_.sid=SB1_.sid left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid where AC0_.acid=19)))))))))));

我检查了解释,发现如下:

+----+--------------+-------------------+------------+------------+----------------------------------------+---------------------+---------+----------------------------------------------------+------+----------+--------------------------+
| id | select_type  | table     | partitions | type       | possible_keys                          | key                 | key_len | ref                        | rows | filtered | Extra                    |
+----+--------------+-------------------+------------+------------+----------------------------------------+---------------------+---------+----------------------------------------------------+------+----------+--------------------------+
|  1 | SIMPLE       | <subquery2>   | NULL       | ALL        | NULL                                   | NULL                | NULL    | NULL                       | NULL |   100.00 | Using where              |
|  1 | SIMPLE       | SA0_      | NULL       | ref        | table_starea_ix01                      | table_starea_ix01   | 5       | <subquery2>.sid                    |    1 |   100.00 | Using index              |
|  1 | SIMPLE       | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | const                      |    4 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | SA0_      | NULL       | ref        | FKFA890976F203761F,table_starea_ix01   | table_starea_ix01   | 5       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | ain2013Realdb.SA0_.acid                |    2 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | SA0_      | NULL       | ref        | FKFA890976F203761F,table_starea_ix01   | table_starea_ix01   | 5       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | ain2013Realdb.SA0_.acid                |    2 |   100.00 | Using index              |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | SA0_      | NULL       | ref        | FKFA890976F203761F,table_starea_ix01   | table_starea_ix01   | 5       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | ain2013Realdb.SA0_.acid                |    2 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | SA0_      | NULL       | ref        | FKFA890976F203761F,table_starea_ix01   | table_starea_ix01   | 5       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | ain2013Realdb.SA0_.acid                |    2 |   100.00 | Using index              |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | SA0_      | NULL       | ref        | FKFA890976F203761F,table_starea_ix01   | table_starea_ix01   | 5       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | Using where; Using index |
|  2 | MATERIALIZED | AC1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SA0_.acid                |    1 |   100.00 | Using index              |
|  2 | MATERIALIZED | AC0_      | NULL       | ref        | table_arstaff_ix01,tab_attach_fk2      | table_arstaff_ix01  | 4       | ain2013Realdb.SA0_.acid                |    2 |   100.00 | Using index              |
|  2 | MATERIALIZED | SB1_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.AC0_.sid             |    1 |   100.00 | NULL                     |
|  2 | MATERIALIZED | SG2_      | NULL       | eq_ref     | PRIMARY                                | PRIMARY             | 4       | ain2013Realdb.SB1_.sgid                |    1 |   100.00 | Using index              |
+----+--------------+-------------------+------------+------------+----------------------------------------+---------------------+---------+----------------------------------------------------+------+----------+--------------------------+

有人可以提出解决方案以使此查询快速执行吗?

我使用INNER JOIN重写了查询,如下所示:

从table_starea中选择SA0_.sid作为stid SA0_在SA0_.acid = AC1_.acid上从左外部加入table_arcons AC1_,其中SA0_.sid在(从((从table_arstaff AC0_选择SB1_.sid,AC0_.acid在AC0_.sid = SB1_.sid上连接table_stf SB1_在SB1_.sgid = SG2_.sgid上左外部连接table_stfgr SG2_)TAB19 INNER JOIN(从((从table_starea SA0_中选择AC1_.acid,SA0_.sid选择酸)(从table_starea SA0_选择左酸)在SA0_.acid = AC1_.acid上)TAB17 INNER JOIN(从((从table_arstaff中选择SB1_.sid,AC0_.acid,从AC0_.sid = SB1_.sid在SB1_上的左外部连接table_stf SB1_中选择sid。 sgid = SG2_.sgid)TAB15内联接(从((从table_starea SA0_左SA2_左外联接table_arcons AC1_在SA0_.acid = AC1_.acid上选择AC1_.acid,SA0_.sid中选择酸)TAB13 INNER JOIN(从((选择sid SB1_.sid,来自table_arstaff的AC0_.acid AC0_左外部联接table_stf AC0_.sid = SB1_.sid左外部联接table_stfgr SG2_ SB1_.sgid = SG2_.sgid)TAB11 INNER JOIN(从((从table_starea SA0_选择左AC1_.acid,SA0_.sid从table_starea SA0_.acid = AC1_.acid上选择AC1_。酸,SA0_.sid)选择酸)TAB9 INNER JOIN(从((从选择(SB1_.sid,AC0_.acid从table_arstaff AC0_在AC0_.sid = SB1_.sid上的左外部连接table_stf SB1_。在SB1_.sgid = SG2_.sgid上的左外部连接table_stfgr SG2_)TAB7 INNER JOIN(从((从table_starea SA0中选择AC1_.acid,SA0_.sid_左外部联接table_arcons SA1_上的AC1_.acid = AC1_.acid)TAB5内部联接(从(选择SB1_.sid中选择sid,从table_arstaff AC0_中选择AC0_.acid AC0_左外部联接table_stf在AC0_.sid = SB1_.sid上的SB1_左外部联接table_stfgrgr SB1_.sgid = SG2_.sgid上的SG2_)TAB3内联接(从(选择AC1_.acid,从table_starea SA0_左SA2_左外联接table_arcons AC1_在SA0_.acid = AC1_.acid上选择酸)TAB2内联接(选择SB1_。来自table_arstaff AC0_的sid,位于AC0_.sid = SB1_.sid的左外部联接table_stf SB1_。位于SB1_.sgid = SG2_.sgid的左外部联接table_stfgr SG2_,其中AC0_.acid = 19) TAB1 ON TAB2.sid = TAB1.sid)TAB4 on TAB3.acid = TAB4.acid)TAB6 on TAB5.sid = TAB6.sid))TAB8 ON TAB7.acid = TAB8.acid))TAB10 on TAB9.sid = TAB10。 sid))TAB11.acid上的TAB12 = TAB12.acid))TAB13.sid上的TAB14 = TAB14.sid))TAB15.acid上的TAB16 = TAB16.acid)))TAB17.sid上的TAB18 = TAB18.sid))TAB19上的TAB20 .acid = TAB20.acid));

这会带来优化吗?

1 个答案:

答案 0 :(得分:0)

关于性能的第一条建议..每个IN子句都可以轻松更改大多数性能的INNER JOIN子句,例如,对于更深层的嵌套级别,您可以更改代码

    ....
     select AC1_.acid 
                      from table_starea SA0_ 
      left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid 
      where SA0_.sid in ( 
        select SB1_.sid 
        from table_arstaff AC0_ 
        left outer join table_stf SB1_ on AC0_.sid=SB1_.sid 
        left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid 
        where AC0_.acid=19   )

     ....)))))))));

  ......
   select AC1_.acid 
    from table_starea SA0_ 
    left outer join table_arcons AC1_ on SA0_.acid=AC1_.acid 

  ) t2 on AC0_.acid = t2.acid
  INNER JOIN (
    select SB1_.sid 
    from table_arstaff AC0_ 
    left outer join table_stf SB1_ on AC0_.sid=SB1_.sid 
    left outer join table_stfgr SG2_ on SB1_.sgid=SG2_.sgid 
    where AC0_.acid=19 
  ) t1 on t1.sid = SA0_.sid 


      ....)))))))));

resul相同,但是内部联接应该更快。

您可以从更深层次的代码开始重构代码,并一一对应地重构所有具有相应INNER JOIN的IN子句