Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1466055
  • 博文数量: 180
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3926
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-30 13:00
个人简介

About me:Oracle ACE pro,optimistic,passionate and harmonious. Focus on ORACLE,MySQL and other database programming,peformance tuning,db design, j2ee,Linux/AIX,Architecture tech,etc

文章分类

全部博文(180)

文章存档

2025年(8)

2024年(27)

2023年(28)

2022年(43)

2020年(62)

2014年(3)

2013年(9)

分类: Mysql/postgreSQL

2025-03-17 08:57:19

rows_examined是什么?
https://dev.mysql.com/doc/refman/8.4/en/slow-query-log.html
文档上的slow query log部分有解释:
The number of rows examined by the server layer (not counting any processing internal to storage engines).

rows examined实际上是server层检查的行数,没有统计storage engine层内部处理的行数。这样很多看似是有问题的,就很好理解了,

但是,很多人把rows examined当成SQL性能的一种重要指标,认为这个是SQL扫描的行数,这就有问题了,实际上rows examined有时候,并不能准确反应storage engines扫描的行数。

engine层返回给server层数据,engine层对表使用访问方法:索引访问、全表扫描,过滤条件也即执行计划里的Filter是在server层做的。


如果索引访问有ICP,则可能导致rows_examined并不能反应索引真正扫描的行数,还有其它缺陷,比如分组聚合、排序影响。


总结如下:
1)二级索引的ICP,会导致返回给server层行数变少,所以此时的rows_examined是包含ICP的结果,并不能准确反应索引扫描的行数。
   而覆盖索引扫描,是没有ICP的,过滤发生在server层,则是准确的。


2)分组聚合如果没有其他字段会让Rows_examined是0,这应该是个BUG


3)index skip scan貌似是0,禁用skip scan则rows_examined不是0


4)排序的行数会加到Rows_examined里,如果有limit,rows_examined会加上limit行数。

1.有ICP时候rows_examined不能反应索引扫描的行数

点击(此处)折叠或打开

  1. explain select * from employees e where e.first_name like 'Am%' and e.last_name like 'B%';
  2. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+-----------------------+
  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  4. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+-----------------------+
  5. | 1 | SIMPLE | e | NULL | range | idx1_emp,idx2_emp | idx1_emp | 94 | NULL | 696 | 11.11 | Using index condition |
  6. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+-----------------------+
  7. 1 row in set, 1 warning (0.00 sec)

  8. flush status;
  9. show status like '%handler_read%';
  10. +-----------------------+-------+
  11. | Variable_name | Value |
  12. +-----------------------+-------+
  13. | Handler_read_first | 0 |
  14. | Handler_read_key | 0 |
  15. | Handler_read_last | 0 |
  16. | Handler_read_next | 0 |
  17. | Handler_read_prev | 0 |
  18. | Handler_read_rnd | 0 |
  19. | Handler_read_rnd_next | 0 |
  20. +-----------------------+-------+
  21. 7 rows in set (0.00 sec)

  22.  explain analyze select * from employees e where e.first_name like 'Am%' and e.last_name like 'B%'\G
  23. *************************** 1. row ***************************
  24. EXPLAIN: -> Index range scan on e using idx1_emp over
  25. ('Am\0\0\0\0\0\0\0\0\0\0\0\0' <= first_name <= 'Am????????????' AND 'B\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
  26. <= last_name <= 'B???????????????'), with index condition: ((e.first_name like 'Am%') and (e.last_name like 'B%'))
  27.  (cost=836.21 rows=696) (actual time=3.599..65.615 rows=69 loops=1)

  28. 1 row in set (0.07 sec)

  29. (root@localhost) [employees_10:58:41]> show status like '%handler_read%';
  30. +-----------------------+-------+
  31. | Variable_name | Value |
  32. +-----------------------+-------+
  33. | Handler_read_first | 0 |
  34. | Handler_read_key | 1 |
  35. | Handler_read_last | 0 |
  36. | Handler_read_next | 69 |
  37. | Handler_read_prev | 0 |
  38. | Handler_read_rnd | 0 |
  39. | Handler_read_rnd_next | 0 |
  40. +-----------------------+-------+
  41. 7 rows in set (0.00 sec)

  42. select THREAD_ID,SQL_TEXT,ROWS_AFFECTED,ROWS_SENT,ROWS_EXAMINED from performance_schema.events_statements_history where thread_id=48;
  43. +-----------+---------------------------------------------------------------------------------------------------+---------------+-----------+---------------+
  44. | THREAD_ID | SQL_TEXT | ROWS_AFFECTED | ROWS_SENT | ROWS_EXAMINED |
  45. +-----------+---------------------------------------------------------------------------------------------------+---------------+-----------+---------------+
  46. | 48 | show status like '%handler_read%' | 0 | 7 | 7 |
  47. | 48 | explain analyze select * from employees e where e.first_name like 'Am%' and e.last_name like 'B%' | 0 | 1 | 69 |
  48. +-----------+---------------------------------------------------------------------------------------------------+---------------+-----------+---------------+
  49. 10 rows in set (0.00 sec)

  50. # Time: 2025-03-14T10:58:41.069062+08:00
  51. # User@Host: root[root] @ localhost [] Id: 8
  52. # Query_time: 0.066471 Lock_time: 0.000005 Rows_sent: 1 Rows_examined: 69 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 104 Bytes_sent: 440 Read_first: 0 Read_last: 0 Read_key: 1 Read_next: 69 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T10:58:41.002591+08:00 End: 2025-03-14T10:58:41.069062+08:00
  53. SET timestamp=1741921121;
  54. explain analyze select * from employees e where e.first_name like 'Am%' and e.last_name like 'B%';


实际行数:实际上index access只按前导列,应该是扫描696行,但是rows_examined=69行,这是MySQL缺陷,
MySQL的rows_examined实际上是engine返回给server层的行数,因为有ICP,导致返回给server层行数包括ICP,
对应的行数是engine(index)=69行--->engine(table)回表 69行--->server 69行:


点击(此处)折叠或打开

  1. select count(*) from employees e where e.first_name like 'Am%';
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 696 |
  6. +----------+

  7.  select count(*) from employees e where e.first_name like 'Am%' and e.last_name like 'B%' ;
  8. +----------+
  9. | count(*) |
  10. +----------+
  11. | 69 |
  12. +----------+

 如果没有ICP,比如下面的是覆盖索引扫描,则是696行,直接访问表扫描696行,过滤是在server层做的。


点击(此处)折叠或打开

  1. explain select first_name,last_name from employees e where e.first_name like 'Am%' and e.last_name like 'B%'
  2.     -> ;
  3. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+--------------------------+
  4. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  5. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+--------------------------+
  6. | 1 | SIMPLE | e | NULL | range | idx1_emp,idx2_emp | idx1_emp | 94 | NULL | 696 | 11.11 | Using where; Using index |
  7. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+------+----------+--------------------------+
  8. 1 row in set, 1 warning (0.00 sec)

  9. explain analyze select first_name,last_name from employees e where e.first_name like 'Am%' and e.last_name like 'B%'\G
  10. *************************** 1. row ***************************
  11. EXPLAIN: -> Filter: ((e.first_name like 'Am%') and (e.last_name like 'B%')) (cost=141.53 rows=77) (actual time=0.053..0.584 rows=69 loops=1)
  12.     -> Covering index range scan on e using idx1_emp over ('Am\0\0\0\0\0\0\0\0\0\0\0\0' <= first_name <= 'Am????????????' AND 'B\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' <= last_name <= 'B???????????????') (cost=141.53 rows=696) (actual time=0.030..0.431 rows=696 loops=1)

  13. 1 row in set (0.00 sec)

  14. # Time: 2025-03-14T14:31:44.984084+08:00
  15. # User@Host: root[root] @ localhost [] Id: 15
  16. # Query_time: 0.001385 Lock_time: 0.000004 Rows_sent: 1 Rows_examined: 696 Thread_id: 15 Errno: 0 Killed: 0 Bytes_received: 123 Bytes_sent: 507 Read_first: 0 Read_last: 0 Read_key: 1 Read_next: 696 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T14:31:44.982699+08:00 End: 2025-03-14T14:31:44.984084+08:00
  17. SET timestamp=1741933904;
  18. explain analyze select first_name,last_name from employees e where e.first_name like 'Am%' and e.last_name like 'B%';

可以看到,覆盖索引扫描的Rows_examined: 696。


2.分组聚合,如果是通过索引直接聚合的会让Rows_examined是0,如果是全表扫描则没有问题

无条件的聚合操作,Rows_examined: 0,因为走索引


点击(此处)折叠或打开

  1. select count(*) from employees e;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 300024 |
  6. +----------+
  7. 1 row in set (0.14 sec)

  8. explain select count(*) from employees e;
  9. +----+-------------+-------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  10. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  11. +----+-------------+-------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  12. | 1 | SIMPLE | e | NULL | index | NULL | idx3_emp | 1 | NULL | 300024 | 100.00 | Using index |
  13. +----+-------------+-------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  14. 1 row in set, 1 warning (0.01 sec)

  15. select THREAD_ID,SQL_TEXT,ROWS_AFFECTED,ROWS_SENT,ROWS_EXAMINED from performance_schema.events_statements_history where thread_id=48;
  16. +-----------+---------------------------------------------------------------------------------------------------+---------------+-----------+---------------+
  17. | THREAD_ID | SQL_TEXT | ROWS_AFFECTED | ROWS_SENT | ROWS_EXAMINED |
  18. +-----------+---------------------------------------------------------------------------------------------------+---------------+-----------+---------------+
  19. | 48 | select count(*) from employees e | 0 | 1 | 0 |


  20. # Time: 2025-03-14T11:02:53.082873+08:00
  21. # User@Host: root[root] @ localhost [] Id: 8
  22. # Query_time: 0.130915 Lock_time: 0.000005 Rows_sent: 1 Rows_examined: 0 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 39 Bytes_sent: 61 Read_first: 0 Read_last: 0 Read_key: 0 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:02:52.951958+08:00 End: 2025-03-14T11:02:53.082873+08:00
  23. SET timestamp=1741921372;
  24. select count(*) from employees e;


  25. 有条件的也是Rows_examined: 0,因为走last_name列索引
  26. select count(*) from employees e where last_name like 'A%';
  27. +----------+
  28. | count(*) |
  29. +----------+
  30. | 10673 |
  31. +----------+


  32. # Time: 2025-03-14T11:03:51.564739+08:00
  33. # User@Host: root[root] @ localhost [] Id: 8
  34. # Query_time: 0.107009 Lock_time: 0.000005 Rows_sent: 1 Rows_examined: 0 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 65 Bytes_sent: 60 Read_first: 1 Read_last: 0 Read_key: 2551 Read_next: 10673 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:03:51.457730+08:00 End: 2025-03-14T11:03:51.564739+08:00
  35. SET timestamp=1741921431;
  36. select count(*) from employees e where last_name like 'A%';


  37. # Time: 2025-03-14T11:04:47.356855+08:00
  38. # User@Host: root[root] @ localhost [] Id: 8
  39. # Query_time: 0.026070 Lock_time: 0.000005 Rows_sent: 1 Rows_examined: 0 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 71 Bytes_sent: 66 Read_first: 1 Read_last: 0 Read_key: 2551 Read_next: 10673 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:04:47.330785+08:00 End: 2025-03-14T11:04:47.356855+08:00
  40. SET timestamp=1741921487;
  41. select max(last_name) from employees e where last_name like 'A%';

  42. -- 全表扫描则没有问题
  43. SELECT/*+no_index(e)*/ COUNT(*) FROM employees e WHERE last_name LIKE 'A%';
  44. +----------+
  45. | COUNT(*) |
  46. +----------+
  47. | 10673 |
  48. +----------+
  49. 1 row in set (0.10 sec)

  50. # Time: 2025-03-14T15:06:13.796973+08:00
  51. # User@Host: root[root] @ localhost [] Id: 15
  52. # Query_time: 0.092579 Lock_time: 0.000006 Rows_sent: 1 Rows_examined: 300024 Thread_id: 15 Errno: 0 Killed: 0 Bytes_received: 81 Bytes_sent: 60 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 300025 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T15:06:13.704394+08:00 End: 2025-03-14T15:06:13.796973+08:00
  53. SET timestamp=1741935973;
  54. SELECT/*+no_index(e)*/ COUNT(*) FROM employees e WHERE last_name LIKE 'A%';

3.index skip scan貌似是0,禁用skip scan则rows_examined不是0



点击(此处)折叠或打开

  1. explain select last_name from employees e where last_name like 'A%';
  2. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+-------+----------+----------------------------------------+
  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  4. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+-------+----------+----------------------------------------+
  5. | 1 | SIMPLE | e | NULL | range | idx1_emp,idx2_emp | idx1_emp | 94 | NULL | 33332 | 100.00 | Using where; Using index for skip scan |
  6. +----+-------------+-------+------------+-------+-------------------+----------+---------+------+-------+----------+----------------------------------------+
  7. 1 row in set, 1 warning (0.00 sec)

  8. -- index skip scan的Rows_examined: 0
  9. # User@Host: root[root] @ localhost [] Id: 8
  10. # Query_time: 0.030014 Lock_time: 0.000006 Rows_sent: 10673 Rows_examined: 0 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 66 Bytes_sent: 129296 Read_first: 1 Read_last: 0 Read_key: 2551 Read_next: 10673 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:05:08.350082+08:00 End: 2025-03-14T11:05:08.380096+08:00
  11. SET timestamp=1741921508;
  12. select last_name from employees e where last_name like 'A%';


  13. -- 禁用skip scan则rows_examined不是0
  14. select/*+set_var(optimizer_switch='skip_scan=off')*/ last_name from employees e where last_name like 'A%';

  15. # Time: 2025-03-14T15:18:20.895184+08:00
  16. # User@Host: root[root] @ localhost [] Id: 15
  17. # Query_time: 0.083965 Lock_time: 0.000005 Rows_sent: 10673 Rows_examined: 300024 Thread_id: 15 Errno: 0 Killed: 0 Bytes_received: 112 Bytes_sent: 129296 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 300024 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T15:18:20.811219+08:00 End: 2025-03-14T15:18:20.895184+08:00
  18. SET timestamp=1741936700;
  19. select/*+set_var(optimizer_switch='skip_scan=off')*/ last_name from employees e where last_name like 'A%';




  20. 有其他字段则可以
  21. select gender,count(*) from employees e where last_name like 'A%' group by gender;
  22. # Time: 2025-03-14T14:49:36.060981+08:00
  23. # User@Host: root[root] @ localhost [] Id: 15
  24. # Query_time: 0.886257 Lock_time: 0.000008 Rows_sent: 2 Rows_examined: 300024 Thread_id: 15 Errno: 0 Killed: 0 Bytes_received: 88 Bytes_sent: 129 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 300024 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T14:49:35.174724+08:00 End: 2025-03-14T14:49:36.060981+08:00
  25. SET timestamp=1741934975;
  26. select gender,count(*) from employees e where last_name like 'A%' group by gender;

  27. -- 查点其他字段才对,Rows_examined: 300024

  28. # Time: 2025-03-14T11:05:51.264599+08:00
  29. # User@Host: root[root] @ localhost [] Id: 8
  30. # Query_time: 0.153229 Lock_time: 0.000004 Rows_sent: 1 Rows_examined: 300024 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 75 Bytes_sent: 266 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 300025 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:05:51.111370+08:00 End: 2025-03-14T11:05:51.264599+08:00
  31. SET timestamp=1741921551;
  32. explain analyze select * from employees e where last_name like 'A%';





4.order by的行会累加到rows_examined,这个应该也是BUG,order by实际上是在server层发生的

点击(此处)折叠或打开

  1. -- order by竟然让Rows_examined翻倍
  2. # Time: 2025-03-14T11:06:59.889233+08:00
  3. # User@Host: root[root] @ localhost [] Id: 8
  4. # Query_time: 0.000366 Lock_time: 0.000005 Rows_sent: 10 Rows_examined: 10 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 29 Bytes_sent: 131 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 11 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:06:59.888867+08:00 End: 2025-03-14T11:06:59.889233+08:00
  5. SET timestamp=1741921619;
  6. select * from limit_t1;
  7. # Time: 2025-03-14T11:07:14.836767+08:00
  8. # User@Host: root[root] @ localhost [] Id: 8
  9. # Query_time: 0.000443 Lock_time: 0.000005 Rows_sent: 10 Rows_examined: 20 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 41 Bytes_sent: 131 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 11 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 10 Sort_scan_count: 1 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:07:14.836324+08:00 End: 2025-03-14T11:07:14.836767+08:00
  10. SET timestamp=1741921634;
  11. select * from limit_t1 order by id;


  12. -- 走索引,查询出4行,对
  13. # Time: 2025-03-14T11:09:16.819234+08:00
  14. # User@Host: root[root] @ localhost [] Id: 8
  15. # Query_time: 0.000482 Lock_time: 0.000005 Rows_sent: 4 Rows_examined: 4 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 52 Bytes_sent: 95 Read_first: 0 Read_last: 0 Read_key: 1 Read_next: 4 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 0 Sort_scan_count: 0 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:09:16.818752+08:00 End: 2025-03-14T11:09:16.819234+08:00
  16. SET timestamp=1741921756;
  17. select * from limit_t1 where id>3 order by id;




  18. show create table t\G
  19. *************************** 1. row ***************************
  20.        Table: t
  21. Create Table: CREATE TABLE `t` (
  22.   `id` int NOT NULL,
  23.   `a` int DEFAULT NULL,
  24.   `b` int DEFAULT NULL,
  25.   PRIMARY KEY (`id`),
  26.   KEY `idx_a` (`a`),
  27.   KEY `idx1_t` (`b`)
  28. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  29. 1 row in set (0.00 sec)

  30. (root@localhost) [employees_11:10:17]> select * from t;
  31. +----+------+------+
  32. | id | a | b |
  33. +----+------+------+
  34. | 1 | 1 | 1 |
  35. | 2 | 1 | 2 |
  36. | 3 | 1 | 3 |
  37. | 4 | 1 | 4 |
  38. | 5 | 1 | 5 |
  39. | 6 | 1 | 6 |
  40. | 7 | 1 | 7 |
  41. | 8 | 1 | 8 |
  42. | 9 | 1 | 9 |
  43. | 10 | 1 | 10 |
  44. +----+------+------+
  45. 10 rows in set (0.00 sec)

  46. (root@localhost) [employees_11:10:21]> select * from t where a=1 and b>3 order by b limit 2;
  47. +----+------+------+
  48. | id | a | b |
  49. +----+------+------+
  50. | 4 | 1 | 4 |
  51. | 5 | 1 | 5 |
  52. +----+------+------+
  53. 2 rows in set (0.00 sec)

  54. 表有10行,order by limit 2,Rows_examined: 12,还是order by问题,因为a列有索引,全是1,读取10行+排序2行=12
  55. # Time: 2025-03-14T11:10:35.769475+08:00
  56. # User@Host: root[root] @ localhost [] Id: 8
  57. # Query_time: 0.000579 Lock_time: 0.000007 Rows_sent: 2 Rows_examined: 12 Thread_id: 8 Errno: 0 Killed: 0 Bytes_received: 59 Bytes_sent: 155 Read_first: 0 Read_last: 0 Read_key: 1 Read_next: 10 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 0 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 2 Sort_scan_count: 1 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T11:10:35.768896+08:00 End: 2025-03-14T11:10:35.769475+08:00
  58. SET timestamp=1741921835;
  59. select * from t where a=1 and b>3 order by b limit 2;



  60. explain select * from t where a=1 and b>3 order by b limit 2;
  61. +----+-------------+-------+------------+------+---------------+-------+---------+-------+------+----------+-----------------------------+
  62. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  63. +----+-------------+-------+------------+------+---------------+-------+---------+-------+------+----------+-----------------------------+
  64. | 1 | SIMPLE | t | NULL | ref | idx_a,idx1_t | idx_a | 5 | const | 10 | 70.00 | Using where; Using filesort |
  65. +----+-------------+-------+------------+------+---------------+-------+---------+-------+------+----------+-----------------------------+
  66. 1 row in set, 1 warning (0.00 sec)

  67. -- 不走索引,也是12行
  68. select/*+no_index(t)*/ * from t where a=1 and b>3 order by b limit 2;

  69. # Time: 2025-03-14T14:54:34.514207+08:00
  70. # User@Host: root[root] @ localhost [] Id: 15
  71. # Query_time: 0.000935 Lock_time: 0.000008 Rows_sent: 2 Rows_examined: 12 Thread_id: 15 Errno: 0 Killed: 0 Bytes_received: 75 Bytes_sent: 155 Read_first: 1 Read_last: 0 Read_key: 1 Read_next: 0 Read_prev: 0 Read_rnd: 0 Read_rnd_next: 11 Sort_merge_passes: 0 Sort_range_count: 0 Sort_rows: 2 Sort_scan_count: 1 Created_tmp_disk_tables: 0 Created_tmp_tables: 0 Start: 2025-03-14T14:54:34.513272+08:00 End: 2025-03-14T14:54:34.514207+08:00
  72. SET timestamp=1741935274;
  73. select/*+no_index(t)*/ * from t where a=1 and b>3 order by b limit 2;








阅读(35) | 评论(0) | 转发(0) |
0

上一篇:MySQL丢失order by cost计算导致走错索引

下一篇:没有了

给主人留下些什么吧!~~