MySQL Explain 中 filtered 列知多少?
最近同事问,MySQL Explain 中 filtered 列也是表示过滤,为什么过滤是100%的时候,查询效率也很高呢?一开始以为是个别同事遇到的困惑,没太当回事,就进行了一对一解答,后来发现团队不少同学都有相同的疑惑,于是就有了这篇文章。
先说结论:对于 MySQL Explain 结果,filtered 列不并需要太关心,重点关注type、key、rows以及extra等列。尤其是rows列,正常情况下 rows 列值越小该SQL的性能越好。这个结论并没有回答同学们的问题,阅读完本文希望各位有答案了。另外,如果需要了解Explain每列具体含义,可以直接看文末内容推荐。
官方介绍
MySQL 5.7 官方文档 中对其描述如下:
The filtered column indicates an estimated percentage of table rows filtered by the table condition. The maximum value is 100, which means no filtering of rows occurred. Values decreasing from 100 indicate increasing amounts of filtering. rows shows the estimated number of rows examined and rows × filtered shows the number of rows joined with the following table. For example, if rows is 1000 and filtered is 50.00 (50%), the number of rows to be joined with the following table is 1000 × 50% = 500.
栗子实战
create_time
和 update_time
字段联合索引后,这条语句的 filtered 列应该会变大,甚至变成100。这个猜测是否对呢,实验验证下。##增加索引前,语句1的 filtered 列值是1.
mysql> explain select t.* from pay_order t ,pay_order_info t1 where t.create_time='2020-06-01 16:09:40' and t.update_time ='2020-02-26 15:41:29' and t1.order_id=t.order_id;
+----+-------------+-------+------------+------+---------------+--------------+---------+----------------+-------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------------+---------+----------------+-------+----------+--------------------------+
| | SIMPLE | t | | ALL | order_id_idx | | | | 10064 | 1.00 | Using where |
| 1 | SIMPLE | t1 | | ref | order_id_idx | order_id_idx | 128 | pay.t.order_id | | 100.00 | Using where; Using index |
+----+-------------+-------+------------+------+---------------+--------------+---------+----------------+-------+----------+--------------------------+
2 rows in set, 1 warning (0.00 sec)
mysql>
mysql>
##增加联合索引(create_time,update_time)联合索引
mysql> alter table pay_order add index idx_ct_ut(create_time,update_time);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql>
##联合索引增加后,语句1的 filtered 列值是100
mysql> explain select t.* from pay_order t ,pay_order_info t1 where t.create_time='2020-06-01 16:09:40' and t.update_time ='2020-02-26 15:41:29' and t1.order_id=t.order_id;
+----+-------------+-------+------------+------+------------------------+--------------+---------+----------------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+------------------------+--------------+---------+----------------+------+----------+--------------------------+
| 1 | SIMPLE | t | | ref | order_id_idx,idx_ct_ut | idx_ct_ut | | const,const | | 100.00 | |
| | SIMPLE | t1 | | ref | order_id_idx | order_id_idx | 128 | pay.t.order_id | 1 | 100.00 | Using where; Using index |
+----+-------------+-------+------------+------+------------------------+--------------+---------+----------------+------+----------+--------------------------+
rows in set, warning (0. sec)
mysql>
mysql>
上面的测试验证了我们的猜测是正确的,如果 SQL语句可以通过索引很好的过滤,那么filtered值是比较大的,甚至可以是100;而索引过滤性越差,他的值越小。由此可见,filtered的100%确实是要比1%要好。
总结
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36