具有许多嵌套in子句的查询的慢度

liwlm1x9  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(320)

在一个有许多嵌套在子句中的查询中,我面临着速度缓慢的问题。
查询结果如下:

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              |
+----+--------------+-------------------+------------+------------+----------------------------------------+---------------------+---------+----------------------------------------------------+------+----------+--------------------------+

有人能提出一个解决方案让这个查询快速执行吗?
我使用内部联接重写了查询,如下所示:
从表中选择sa0.sid作为stid从表中选择sa0.sid从表中选择sa0.sid从表中选择sa0.sid(选择sb1.sid,ac0\ acid来自表\u arstaff ac0\ left outer join table\u stf sb1\ on ac0\ sid=sb1\ sid.left outer join table\u stfgr sg2\ on sb1\ sgid=sg2\ sgid)tab19 inner join(选择acid from((选择ac1\ acid,sa0\ sid来自表\u starea sa0\ left outer join table\u arcons ac1\ on sa0\ acid=ac1\ acid)tab17 inner join(选择sid from((选择sb1\ sid,ac0\ acid来自表\u arstaff ac0\ left outer join table\u stf sb1\ on ac0\ sid=sb1\ sid.left outer join table\u stfgr sg2\ on sb1\ sgid=sg2\ sgid)tab15 inner join(选择acid from((选择ac1\ acid,sa0\ sid来自表\u starea sa0\ left outer join table\u arcons ac1\ on sa0\ acid=ac1\ acid)tab13 inner join(选择sid from((选择sb1\ sid,ac0\表acid\ arstaff ac0\左外联接表ACU stf sb1\上的sb1\ sid=sb1\ sid。左外联接表ACU stfgr sg2\上的sb1\ sgid=sg2\ sgid)tab11内部联接(选择acid from((选择ac1\ acid,sa0\ sid from表ACU starea sa0\左外联接表ACU arcons ac1\上的sa0\ acid=ac1\ acid)tab9内部联接(选择sid from((选择sb1\ sid,ac0\ acid来自表\u arstaff ac0\左侧外部联接表\u stf sb1\上的ac0\ sid=sb1\ sid.left outer join表\u stfgr sg2\上的sb1\ sgid=sg2\ sgid)tab7内部联接(从表中选择acid((从表中选择ac1\ acid,从表中选择sa0\ sid)starea sa0\左侧外部联接表\u arcons ac1\上的ac0\ acid=ac1\ acid)tab5内部联接(从表中选择sid(从表中选择sb1\ sid,ac0\表acid\ arstaff ac0\左侧外部联接表ac0\上的stf sb1\上的sid=sb1\上的sid.left outer join表ACU stfgr sg2\上的sb1\上的sgid=sg2\上的tab3内部联接(选择ac1\ acid,sa0\表中的sa0\ sid开始sa0\左侧外部联接表中的sa0\ arcons ac1\在sa0\上。acid=ac1\ acid)tab2内部联接(从ac0\上的表中选择sb1\ sid从ac0\左侧外部联接表中的sb1\ stf sb1\上。sid=sb1\ sid左侧外部联接表中的sg2\在sb1\上。sgid=sg2\ sgid其中acid=19)tab2.sid=tab1.sid)tab3.acid=tab4.acid)tab6开tab5.sid=tab6.sid)tab7.acid=tab8.acid)tab10 on tab9.sid=tab10.sid)tab12 on tab11.acid=tab12.acid)tab14 on tab13.sid=tab14.sid)tab16 on tab15.acid=tab16.acid)tab18 on tab17.sid=tab18.sid)tab20 on tab19.acid=tab20.acid));
这会带来优化吗?

6rvt4ljy

6rvt4ljy1#

对表演的第一个建议。。每个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   )

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

......
   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 

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

结果是一样的,但是内部连接应该更快。。
你可以从更深层的一个开始重构你的代码,然后一个接一个地重构所有具有相应内部连接的in子句

相关问题