information_schema.TABLE_STATISTICS

冷不防 2023-08-17 16:23 194阅读 0赞

version: 5.7.21 Percona Server

  1. >CREATE TABLE `t1` (
  2. `id` int(11) NOT NULL,
  3. `a` int(11) DEFAULT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  6. >select * from t1;
  7. +----+------+
  8. | id | a |
  9. +----+------+
  10. | 1 | 12 |
  11. | 2 | 2 |
  12. | 3 | 3 |
  13. | 4 | 4 |
  14. +----+------+
  15. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  16. +--------------+------------+-----------+--------------+
  17. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  18. +--------------+------------+-----------+--------------+
  19. | sbtest | t1 | 118 | 17 |
  20. +--------------+------------+-----------+--------------+
  21. 1 row in set (0.00 sec)
  22. >select * from t1 limit 1;
  23. +----+------+
  24. | id | a |
  25. +----+------+
  26. | 1 | 12 |
  27. +----+------+
  28. 1 row in set (0.00 sec)
  29. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  30. +--------------+------------+-----------+--------------+
  31. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  32. +--------------+------------+-----------+--------------+
  33. | sbtest | t1 | 119 | 17 |

### 本以为是读出来几行 ROWS_READ 就加几个,没成想不是这样的,请看下面的例子

  1. >CREATE TABLE `t1` (
  2. `id` int(11) NOT NULL,
  3. `a` int(11) DEFAULT NULL,
  4. PRIMARY KEY (`id`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  6. >select * from t1;
  7. +----+------+
  8. | id | a |
  9. +----+------+
  10. | 1 | 12 |
  11. | 2 | 2 |
  12. | 3 | 3 |
  13. | 4 | 4 |
  14. +----+------+
  15. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  16. +--------------+------------+-----------+--------------+
  17. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  18. +--------------+------------+-----------+--------------+
  19. | sbtest | t1 | 87 | 16 |
  20. +--------------+------------+-----------+--------------+
  21. >explain select * from t1 order by a limit 1;
  22. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
  23. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  24. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
  25. | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | Using filesort |
  26. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
  27. 1 row in set, 1 warning (0.00 sec)
  28. >select * from t1 order by a limit 1;
  29. +----+------+
  30. | id | a |
  31. +----+------+
  32. | 2 | 2 |
  33. +----+------+
  34. 1 row in set (0.00 sec)
  35. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  36. +--------------+------------+-----------+--------------+
  37. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  38. +--------------+------------+-----------+--------------+
  39. | sbtest | t1 | 91 | 16 |
  40. +--------------+------------+-----------+--------------+
  41. 1 row in set (0.00 sec)

### order by 没走索引,最后走的filesort, 表里面有4条数据,最后ROWS_READ 是加的4。(应该是order by 了几条,加的就是几个),可以对比一下下面的例子

  1. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  2. +--------------+------------+-----------+--------------+
  3. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  4. +--------------+------------+-----------+--------------+
  5. | sbtest | t1 | 95 | 16 |
  6. +--------------+------------+-----------+--------------+
  7. >explain select * from t1 where id >2 order by a limit 1;
  8. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  9. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  10. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  11. | 1 | SIMPLE | t1 | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.00 | Using where; Using filesort |
  12. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  13. 1 row in set, 1 warning (0.00 sec)
  14. >select * from t1 where id >2 order by a limit 1;
  15. +----+------+
  16. | id | a |
  17. +----+------+
  18. | 3 | 3 |
  19. +----+------+
  20. 1 row in set (0.00 sec)
  21. # 可以看到ROWS_READ 加的是2,因为已经通过索引把数据过滤剩两个了。
  22. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  23. +--------------+------------+-----------+--------------+
  24. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  25. +--------------+------------+-----------+--------------+
  26. | sbtest | t1 | 97 | 16 |
  27. +--------------+------------+-----------+--------------+

### 假如where没有用上索引,再order by 的话还是4个

  1. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  2. +--------------+------------+-----------+--------------+
  3. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  4. +--------------+------------+-----------+--------------+
  5. | sbtest | t1 | 91 | 16 |
  6. +--------------+------------+-----------+--------------+
  7. 1 row in set (0.00 sec)
  8. >select * from t1 where a<10 order by a limit 1;
  9. +----+------+
  10. | id | a |
  11. +----+------+
  12. | 2 | 2 |
  13. +----+------+
  14. 1 row in set (0.00 sec)
  15. >explain select * from t1 where a<10 order by a limit 1;
  16. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------------------+
  17. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  18. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------------------+
  19. | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 33.33 | Using where; Using filesort |
  20. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------------------+
  21. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  22. +--------------+------------+-----------+--------------+
  23. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  24. +--------------+------------+-----------+--------------+
  25. | sbtest | t1 | 95 | 16 |
  26. +--------------+------------+-----------+--------------+

### 但是如果条件里面没有order 并且没有filesout,limit 几个就加几个

  1. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  2. +--------------+------------+-----------+--------------+
  3. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  4. +--------------+------------+-----------+--------------+
  5. | sbtest | t1 | 97 | 16 |
  6. +--------------+------------+-----------+--------------+
  7. >explain select * from t1 limit 1;
  8. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
  9. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  10. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
  11. | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | NULL |
  12. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
  13. 1 row in set, 1 warning (0.00 sec)
  14. > select * from t1 limit 1;
  15. +----+------+
  16. | id | a |
  17. +----+------+
  18. | 1 | 12 |
  19. +----+------+
  20. 1 row in set (0.00 sec)
  21. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  22. +--------------+------------+-----------+--------------+
  23. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  24. +--------------+------------+-----------+--------------+
  25. | sbtest | t1 | 98 | 16 |
  26. +--------------+------------+-----------+--------------+
  27. 1 row in set (0.00 sec)

### 但是 where条件里面有主键和非索引,order by非索引的话 也是limit 几就是几

  1. # 先插入一行数据
  2. >insert into t1 values(5,3);
  3. Query OK, 1 row affected (0.01 sec)
  4. >select * from t1;
  5. +----+------+
  6. | id | a |
  7. +----+------+
  8. | 1 | 12 |
  9. | 2 | 2 |
  10. | 3 | 3 |
  11. | 4 | 4 |
  12. | 5 | 3 |
  13. +----+------+
  14. 5 rows in set (0.00 sec)
  15. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  16. +--------------+------------+-----------+--------------+
  17. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  18. +--------------+------------+-----------+--------------+
  19. | sbtest | t1 | 104 | 17 |
  20. +--------------+------------+-----------+--------------+
  21. 1 row in set (0.00 sec)
  22. >explain select * from t1 where id >2 and a=3 order by a limit 1;
  23. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  24. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  25. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  26. | 1 | SIMPLE | t1 | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 3 | 20.00 | Using where |
  27. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  28. 1 row in set, 1 warning (0.00 sec)
  29. >select * from t1 where id >2 and a=3 order by a limit 1;
  30. +----+------+
  31. | id | a |
  32. +----+------+
  33. | 3 | 3 |
  34. +----+------+
  35. 1 row in set (0.00 sec)
  36. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  37. +--------------+------------+-----------+--------------+
  38. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  39. +--------------+------------+-----------+--------------+
  40. | sbtest | t1 | 105 | 17 |
  41. +--------------+------------+-----------+--------------+
  42. 1 row in set (0.00 sec)

### 下面这个我没想明白是为什么

a>3 已经过滤剩两行了,为啥还是加3 呢???

难道是有filesort 的话就是扫描多少行ROWS_READ就加几吗???

  1. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  2. +--------------+------------+-----------+--------------+
  3. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  4. +--------------+------------+-----------+--------------+
  5. | sbtest | t1 | 115 | 17 |
  6. +--------------+------------+-----------+--------------+
  7. 1 row in set (0.00 sec)
  8. >explain select * from t1 where id >2 and a>3 order by a limit 1;
  9. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  10. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  11. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  12. | 1 | SIMPLE | t1 | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 3 | 33.33 | Using where; Using filesort |
  13. +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------+
  14. 1 row in set, 1 warning (0.01 sec)
  15. >select * from t1 where id >2 and a>3 order by a limit 1;
  16. +----+------+
  17. | id | a |
  18. +----+------+
  19. | 4 | 4 |
  20. +----+------+
  21. 1 row in set (0.01 sec)
  22. >select TABLE_SCHEMA,TABLE_NAME,ROWS_READ,ROWS_CHANGED from information_schema.TABLE_STATISTICS where TABLE_NAME="t1";
  23. +--------------+------------+-----------+--------------+
  24. | TABLE_SCHEMA | TABLE_NAME | ROWS_READ | ROWS_CHANGED |
  25. +--------------+------------+-----------+--------------+
  26. | sbtest | t1 | 118 | 17 |
  27. +--------------+------------+-----------+--------------+

转载于:https://www.cnblogs.com/moon1223/p/11171305.html

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读