简体   繁体   English

SQL 查询在 MySql 8 中比在 MySql 5.6 中慢得多

[英]SQL query much slower in MySql 8 than in MySql 5.6

I have a simple query:我有一个简单的查询:

select  count(*)
    from  ror
    where  rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )
      and  ( ( rorbcd = '00009310022487'
                      and  rorfid = 'VDR' )
              or  ( rorfid = 'VDR'
                      and  rorbcd in (
                        SELECT  pplbcd
                            from  ppl
                            where  pplfid = 'VDR'
                              and  pplscb6 = '00009310022487'
                              and  pplsflg = 'Y'
                              and  pplsku = '0332690-008'
                              and  ppldoc = '73'
                              and  pplsca9 = ''
                              and  pplven = '10112' ) ) 
           ) 

On a MySql 5.6 machine, it runs quite quick:在 MySql 5.6 机器上,它运行得非常快:

+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.01 sec)

But on my MySql 8 server, it runs much slower:但是在我的 MySql 8 服务器上,它的运行速度要慢得多:

+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.90 sec)

I am mostly using default parameters for both servers (well, Amazon RDS defaults).我主要使用两台服务器的默认参数(嗯,Amazon RDS 默认值)。 What parameter would cause this slowdown?什么参数会导致这种减速? What configuration might help it run in MySql 8 with roughly the same speed as MySql 5.6.什么配置可能有助于它在 MySql 8 中以与 MySql 5.6 大致相同的速度运行。

As per many other places online, this is a bad SQL.根据网上许多其他地方,这是一个糟糕的 SQL。 I could easily re-write it as an exists , I believe, which should be much more efficient.我相信,我可以很容易地将它重写为exists ,这应该会更有效率。 My problem is that we are migrating from 5.6 to 8.0, and there is a good chance I will either miss a query, or not be able to fix one of the queries because of some weird requirement on my end.我的问题是我们正在从 5.6 迁移到 8.0,很有可能我会错过一个查询,或者由于我的一些奇怪的要求而无法修复其中一个查询。 I bet there is some parameter that I could tweak in my MySql 8 configuration to make this run in a way roughly comparable to 5.6.我敢打赌,我可以在我的 MySql 8 配置中调整一些参数,以使其以与 5.6 大致相当的方式运行。 That is what I am looking for.这就是我要找的。

More information:更多信息:

  1. Same Amazon RDS server setup, in terms of hardware就硬件而言,相同的 Amazon RDS 服务器设置
  2. We just upgraded 5.6 to 5.7, then upgraded 5.7 to 8.0.我们只是将 5.6 升级到 5.7,然后将 5.7 升级到 8.0。 As much as possible, I kept with the default MySql parameters.我尽可能地保留了默认的 MySql 参数。 I only purposely changed lower_case_table_names = 1 and max_connections=300 , to match 5.6 and our current requirements.我只是故意更改了lower_case_table_names = 1max_connections=300 ,以匹配 5.6 和我们当前的要求。
  3. The PPL and ROR tables are extremely complex. PPL 和 ROR 表非常复杂。 137 columns for PPL, and 144 columns for ROR. PPL 137 列,ROR 144 列。

For MySql 5.6 explanation, with an even simpler (and still faster) query, and some index information:对于 MySql 5.6 的解释,使用更简单(而且更快)的查询和一些索引信息:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.51-log |
+------------+
1 row in set (0.01 sec)

mysql> explain extended select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl where pplfid = 'VDR' and pplscb6 = '00009310022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----+--------------------+-------+-----------------+----------------------------------------------------------------+--------------+---------+------------+------+----------+------------------------------------+
| id | select_type        | table | type            | possible_keys                                                  | key          | key_len | ref        | rows | filtered | Extra                              |
+----+--------------------+-------+-----------------+----------------------------------------------------------------+--------------+---------+------------+------+----------+------------------------------------+
|  1 | PRIMARY            | ror   | ref             | ROR_RORBCDK,ROR_RORSTSK,ROR_RORBSTSK,ROR_RORSDT3K,ROR_RORGMOQK | ROR_RORGMOQK | 5       | const      |    1 |   100.00 | Using index condition; Using where |
|  2 | DEPENDENT SUBQUERY | ppl   | unique_subquery | PPL_PPLBCDC,PPL_PPLDOCK,PPL_PPLVENC,PPL_PPLSKUC,PPL_PPLFID     | PPL_PPLBCDC  | 29      | func,const |    1 |   100.00 | Using where                        |
+----+--------------------+-------+-----------------+----------------------------------------------------------------+--------------+---------+------------+------+----------+------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> show warnings;                                                                                                                                                                                                                         +-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select count(0) AS `count(*)` from `celltreat`.`ror` where ((`celltreat`.`ror`.`RORSTS` not in ('RECD','CANC','CCNS','SDNY')) and (((`celltreat`.`ror`.`RORFID` = 'VDR') and (`celltreat`.`ror`.`RORBCD` = '00009310022487')) or ((`celltreat`.`ror`.`RORFID` = 'VDR') and <in_optimizer>(`celltreat`.`ror`.`RORBCD`,<exists>(<primary_index_lookup>(<cache>(`celltreat`.`ror`.`RORBCD`) in ppl on PPL_PPLBCDC where ((`celltreat`.`ppl`.`PPLVEN` = '10112') and (`celltreat`.`ppl`.`PPLSCA9` = '') and (`celltreat`.`ppl`.`PPLDOC` = '73') and (`celltreat`.`ppl`.`PPLSKU` = '0332690-008') and (`celltreat`.`ppl`.`PPLSFLG` = 'Y') and (`celltreat`.`ppl`.`PPLSCB6` = '00009310022487') and (`celltreat`.`ppl`.`PPLFID` = 'VDR') and (<cache>(`celltreat`.`ror`.`RORBCD`) = `celltreat`.`ppl`.`PPLBCD`)))))))) |
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select count(*) from ppl;
+----------+
| count(*) |
+----------+
|    95108 |
+----------+
1 row in set (0.01 sec)

mysql> explain extended select count(*) from ppl;
+----+-------------+-------+-------+---------------+------------+---------+------+-------+----------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | ppl   | index | NULL          | PPL_PPLFID | 5       | NULL | 11194 |   100.00 | Using index |
+----+-------------+-------+-------+---------------+------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+---------------------------------------------------------------------+
| Level | Code | Message                                                             |
+-------+------+---------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select count(0) AS `count(*)` from `celltreat`.`ppl` |
+-------+------+---------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT TABLE_NAME,  index_name, COUNT(1) column_count FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'veeder' and table_name in ( 'ppl', 'ror' ) group by table_name, index_name order by table_name collate utf8_general_ci, index_name collate utf8_general_ci;
+------------+--------------+--------------+
| TABLE_NAME | index_name   | column_count |
+------------+--------------+--------------+
| ppl        | PPLKEY       |            1 |
| ppl        | PPL_PPLBCDC  |            2 |
| ppl        | PPL_PPLDOCK  |            3 |
| ppl        | PPL_PPLFID   |            1 |
| ppl        | PPL_PPLPONOC |            4 |
| ppl        | PPL_PPLSKUC  |            4 |
| ppl        | PPL_PPLVENC  |            5 |
| ror        | RORKEY       |            1 |
| ror        | ROR_RORBCDK  |            5 |
| ror        | ROR_RORBSTSK |            4 |
| ror        | ROR_RORCNSK  |            3 |
| ror        | ROR_RORCRDTK |            4 |
| ror        | ROR_RORCUIDK |            5 |
| ror        | ROR_RORDOCK  |            5 |
| ror        | ROR_RORGMOQK |            4 |
| ror        | ROR_RORPONOK |            6 |
| ror        | ROR_RORRORK  |            2 |
| ror        | ROR_RORSDT3K |            4 |
| ror        | ROR_RORSKUK  |            5 |
| ror        | ROR_RORSTSK  |            5 |
| ror        | ROR_RORUSTSK |            4 |
| ror        | ROR_RORVENK  |            7 |
| ror        | ROR_RORVSTSK |            4 |
+------------+--------------+--------------+
23 rows in set (0.00 sec)

And now the MySQL 8 explanation, with an even simpler (and still slower) query, and some index information:现在 MySQL 8 解释,更简单(而且更慢)的查询,以及一些索引信息:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.23    |
+-----------+
1 row in set (0.00 sec)

mysql> explain select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl where pplfid = 'VDR' and pplscb6 = '00009310022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys                                                  | key          | key_len | ref                     | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
|  1 | PRIMARY     | ror   | NULL       | ref  | ROR_RORBCDK,ROR_RORSTSK,ROR_RORBSTSK,ROR_RORSDT3K,ROR_RORGMOQK | ROR_RORGMOQK | 5       | const                   | 185179 |    50.03 | Using where |
|  2 | SUBQUERY    | ppl   | NULL       | ref  | PPL_PPLBCDC,PPL_PPLDOCK,PPL_PPLVENC,PPL_PPLSKUC,PPL_PPLFID     | PPL_PPLVENC  | 53      | const,const,const,const |      2 |     2.50 | Using where |
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
2 rows in set, 1 warning (0.02 sec)

mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select count(0) AS `count(*)` from `veeder`.`ror` where ((`veeder`.`ror`.`RORSTS` not in ('RECD','CANC','CCNS','SDNY')) and (((`veeder`.`ror`.`RORFID` = 'VDR') and (`veeder`.`ror`.`RORBCD` = '00009310022487')) or ((`veeder`.`ror`.`RORFID` = 'VDR') and <in_optimizer>(`veeder`.`ror`.`RORBCD`,`veeder`.`ror`.`RORBCD` in ( <materialize> (/* select#2 */ select `veeder`.`ppl`.`PPLBCD` from `veeder`.`ppl` where ((`veeder`.`ppl`.`PPLVEN` = '10112') and (`veeder`.`ppl`.`PPLSCA9` = '') and (`veeder`.`ppl`.`PPLDOC` = '73') and (`veeder`.`ppl`.`PPLSKU` = '0332690-008') and (`veeder`.`ppl`.`PPLSFLG` = 'Y') and (`veeder`.`ppl`.`PPLSCB6` = '00009310022487') and (`veeder`.`ppl`.`PPLFID` = 'VDR')) ), <primary_index_lookup>(`veeder`.`ror`.`RORBCD` in <temporary table> on <auto_distinct_key> where ((`veeder`.`ror`.`RORBCD` = `<materialized_subquery>`.`pplbcd`)))))))) |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 
mysql> select count(*) from ppl;
+----------+
| count(*) |
+----------+
|    94657 |
+----------+
1 row in set (0.49 sec)

mysql> explain select count(*) from ppl;
+----+-------------+-------+------------+-------+---------------+------------+---------+------+-------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key        | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | ppl   | NULL       | index | NULL          | PPL_PPLFID | 5       | NULL | 89284 |   100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+-------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------------------------------+
| Level | Code | Message                                                          |
+-------+------+------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select count(0) AS `count(*)` from `veeder`.`ppl` |
+-------+------+------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT TABLE_NAME,  index_name, COUNT(1) column_count FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'veeder' and table_name in ( 'ppl', 'ror' ) group by table_name, index_name order by table_name collate utf8_general_ci, index_name collate utf8_general_ci;
+------------+--------------+--------------+
| TABLE_NAME | INDEX_NAME   | column_count |
+------------+--------------+--------------+
| ppl        | PPLKEY       |            1 |
| ppl        | PPL_PPLBCDC  |            2 |
| ppl        | PPL_PPLDOCK  |            3 |
| ppl        | PPL_PPLFID   |            1 |
| ppl        | PPL_PPLPONOC |            4 |
| ppl        | PPL_PPLSKUC  |            4 |
| ppl        | PPL_PPLVENC  |            5 |
| ror        | RORKEY       |            1 |
| ror        | ROR_RORBCDK  |            5 |
| ror        | ROR_RORBSTSK |            4 |
| ror        | ROR_RORCNSK  |            3 |
| ror        | ROR_RORCRDTK |            4 |
| ror        | ROR_RORCUIDK |            5 |
| ror        | ROR_RORDOCK  |            5 |
| ror        | ROR_RORGMOQK |            4 |
| ror        | ROR_RORPONOK |            6 |
| ror        | ROR_RORRORK  |            2 |
| ror        | ROR_RORSDT3K |            4 |
| ror        | ROR_RORSKUK  |            5 |
| ror        | ROR_RORSTSK  |            5 |
| ror        | ROR_RORUSTSK |            4 |
| ror        | ROR_RORVENK  |            7 |
| ror        | ROR_RORVSTSK |            4 |
+------------+--------------+--------------+
23 rows in set (0.01 sec)

I don't understand the warnings MySql 8 give, but maybe that is the key to finding the proper server setting.我不明白 MySql 8 给出的警告,但这也许是找到正确服务器设置的关键。

The table definitions and indexes are kind of long, especially for both DBs, but I do have copies of them and they look virtually the same.表定义和索引有点长,尤其是对于两个数据库,但我确实有它们的副本,它们看起来几乎相同。 The biggest difference seems to be that in 5.6 I have some integer columns defined as "int(11)", and on 8.0, they are defined as "int".最大的区别似乎是在 5.6 中我有一些 integer 列定义为“int(11)”,而在 8.0 中,它们被定义为“int”。 In the "show create table" output, the indexes are the same.在“show create table”output中,索引是一样的。 Obviously, the "AUTO_INCREMENT" value is different, but this makes sense as the MySql 8 version is a week out of date.显然,“AUTO_INCREMENT”值不同,但这是有道理的,因为 MySql 8 版本已经过时一周了。 Hopefully this is enough information.希望这是足够的信息。 When I do "describe ppl", "show indexes from ppl", "describe ror", and "show indexes from ror" on both DB servers, they are virtually the same except for the following:当我在两个数据库服务器上执行“描述 ppl”、“显示来自 ppl 的索引”、“描述 ror”和“显示来自 ror 的索引”时,它们实际上是相同的,除了以下内容:

  1. As above, int(11) changed to int .如上, int(11)更改为int
  2. The "Cardinality" column changes. “基数”列发生变化。 It is less for the MySQL 8 version, probably because it is outdated MySQL 8版本比较少,可能是已经过时了
  3. Obviously, 8.0 "show indexes" command shows more columns.显然,8.0 的“显示索引”命令显示了更多的列。 The "Visible" column is always "YES" and the "Expression" column is always "NULL". “可见”列始终为“YES”,“表达式”列始终为“NULL”。 Incidentally, INDEX_TYPE is "BTREE" for all indexes.顺便说一下,INDEX_TYPE 是所有索引的“BTREE”。

Because these are kind of big tables, the show create table and describe <table>;show indexes from <table> produce results too long for the body of a post.因为这些是大表,所以show create tabledescribe <table>;show indexes from <table>产生的结果对于帖子的正文来说太长了。 So if you need more information, I will need to know what you don't need of what I have currently provided.因此,如果您需要更多信息,我需要知道您不需要我目前提供的内容。 Thanks.谢谢。

Edit:编辑:

Edit after applying advice of @o-jones: The results below are after I ran ANALYZE TABLE ror, ppl;在应用@o-jones 的建议后进行编辑:以下结果是在我运行ANALYZE TABLE ror, ppl; , but they match what was happening before I ran ANALYZE TABLE ror, ppl; ,但它们与我运行ANALYZE TABLE ror, ppl; . .

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.23    |
+-----------+
1 row in set (0.00 sec)

mysql> explain select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl where pplfid = 'VDR' and '00009310022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys                                                  | key          | key_len | ref                     | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
|  1 | PRIMARY     | ror   | NULL       | ref  | ROR_RORBCDK,ROR_RORSTSK,ROR_RORBSTSK,ROR_RORSDT3K,ROR_RORGMOQK | ROR_RORGMOQK | 5       | const                   | 185179 |    50.03 | Using where |
|  2 | SUBQUERY    | ppl   | NULL       | ref  | PPL_PPLBCDC,PPL_PPLDOCK,PPL_PPLVENC,PPL_PPLSKUC,PPL_PPLFID     | PPL_PPLVENC  | 53      | const,const,const,const |      2 |     2.50 | Using where |
+----+-------------+-------+------------+------+----------------------------------------------------------------+--------------+---------+-------------------------+--------+----------+-------------+
2 rows in set, 1 warning (0.01 sec)

mysql> select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl where pplfid = 'VDR' and pplscb610022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.80 sec)

mysql> explain select count(*) from ror USE INDEX(ROR_RORSTSK) where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl wd = 'VDR' and pplscb6 = '00009310022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----+-------------+-------+------------+------+------------------------------------------------------------+-------------+---------+-------------------------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys                                              | key         | key_len | ref                     | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+------------------------------------------------------------+-------------+---------+-------------------------+--------+----------+-------------+
|  1 | PRIMARY     | ror   | NULL       | ALL  | ROR_RORSTSK                                                | NULL        | NULL    | NULL                    | 370358 |    50.03 | Using where |
|  2 | SUBQUERY    | ppl   | NULL       | ref  | PPL_PPLBCDC,PPL_PPLDOCK,PPL_PPLVENC,PPL_PPLSKUC,PPL_PPLFID | PPL_PPLVENC | 53      | const,const,const,const |      2 |     2.50 | Using where |
+----+-------------+-------+------------+------+------------------------------------------------------------+-------------+---------+-------------------------+--------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

mysql> select count(*) from ror USE INDEX(ROR_RORSTSK) where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) or ( rorfid = 'VDR' and rorbcd in ( select pplbcd from ppl where ppl' and pplscb6 = '00009310022487' and pplsflg = 'Y' and pplsku = '0332690-008' and ppldoc = '73' and pplsca9 = '' and pplven = '10112'  ) ) );
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.25 sec)

mysql> explain select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) );+----+-------------+-------+------------+-------+----------------------------------------------------------------+--------------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys                                                  | key          | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-------+------------+-------+----------------------------------------------------------------+--------------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | ror   | NULL       | range | ROR_RORBCDK,ROR_RORSTSK,ROR_RORBSTSK,ROR_RORSDT3K,ROR_RORGMOQK | ROR_RORBSTSK | 35      | NULL |   37 |   100.00 | Using where; Using index |
+----+-------------+-------+------------+-------+----------------------------------------------------------------+--------------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select count(*) from ror where rorsts not in ( 'RECD', 'CANC', 'CCNS', 'SDNY' )  and  ( ( rorbcd = '00009310022487' and rorfid = 'VDR'  ) );
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

Incidentally, using the index hint is not an acceptable solution, in this case, because this is just one example SQL.顺便说一句,在这种情况下,使用索引提示不是一个可接受的解决方案,因为这只是 SQL 的一个示例。 I always knew I could make this particular SQL more efficient.我一直都知道我可以让这个特殊的 SQL 更高效。 My problem was that MySql 8 should be quicker, but is considerably slower, for the same SQL, so trying to figure out why.我的问题是 MySql 8 应该更快,但速度要慢得多,对于相同的 SQL,所以试图找出原因。 I suspect the same thing would happen in MySql 5.7, but I have not tried that yet since I heard MySql 8 was the way to go.我怀疑在 MySql 5.7 中也会发生同样的事情,但我还没有尝试过,因为我听说 MySql 8 是通往 go 的方式。

Query plans for the primary (top-level) query -- the one using your ror table --compared.主要(顶级)查询的查询计划 - 使用您的ror表的查询 - 比较。

version  type   key        key_len  ref     rows Filtered Extra
 5.6.61 range  ROR_RORSTSK       6  NULL    3256    ----  Using index condition; Using where 
 8.0.23 ref    ROR_RORGMOQK      5  const 185179    50.03 Using where

For some reason the 8.0.23 query optimizer didn't choose the ROR_RORSTSK index to satisfy your query, but instead chose ROR_RORGMOQK.由于某种原因,8.0.23 查询优化器没有选择 ROR_RORSTSK 索引来满足您的查询,而是选择了 ROR_RORGMOQK。 As a result the query couldn't use a range scan and had to filter (compare one-by-one) 185k rows rather than 3k rows.结果,查询不能使用范围扫描,必须过滤(逐一比较)185k 行而不是 3k 行。 That is the visible difference between your two plans.这是您的两个计划之间的明显差异。

(5.6 EXPLAIN output doesn't include the Filtered column. You need EXPLAIN EXTENDED.) (5.6 EXPLAIN output 不包括 Filtered 列。您需要 EXPLAIN EXTENDED。)

You could try changing this line in your query您可以尝试在查询中更改此行

from ror

to

from ror USE INDEX(ROR_RORSTSK)

or或者

from ror IGNORE INDEX(ROR_RORGMOQK)

One of those might get v8 to use a similar query plan to v5.6.其中之一可能会让 v8 使用与 v5.6 类似的查询计划。

You can probably figure out more with your knowledge of what columns are included in each index.您可能可以通过了解每个索引中包含哪些列来了解更多信息。

EDIT编辑

Also try doing ANALYZE TABLE ror, ppl;也尝试做ANALYZE TABLE ror, ppl; especially if you just bulk-loaded the tables as part of your migration.特别是如果您只是在迁移过程中批量加载表。 If ANALYZE is unnecessary it won't hurt anything, and it may help the query planner choose the best plan.如果ANALYZE是不必要的,它不会伤害任何东西,它可以帮助查询计划者选择最佳计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM