如何使mysql-slow.log不告诉我我没有使用索引?
名为如何使mysql-slow.log不告诉我我没有使用索引?,mysql,indexing,mysql-slow-query-log,Mysql,Indexing,Mysql Slow Query Log,名为的表markets有这样的列:id,visible,position,我每次都在visible上创建索引 而缓慢的日志总是 # Time: 2021-05-12T00:46:14.248654Z # User@Host: tase[tase] @ ip-172-31-22-13.ap-northeast-1.compute.internal [172.31.22.13] Id: 134987 # Query_time: 0.000155 Lock_time: 0.000048 Rows_
的表markets
有这样的列:id
,visible
,position
,我每次都在visible上创建索引
而缓慢的日志总是
# Time: 2021-05-12T00:46:14.248654Z
# User@Host: tase[tase] @ ip-172-31-22-13.ap-northeast-1.compute.internal [172.31.22.13] Id: 134987
# Query_time: 0.000155 Lock_time: 0.000048 Rows_sent: 7 Rows_examined: 17
SET timestamp=1620780374;
SELECT `markets`.* FROM `markets` WHERE `markets`.`visible` = TRUE ORDER BY `markets`.`position` DESC;
并解释:
+----+-------------+---------+------------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+------+---------+------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+------+---------+------+------+----------+-----------------------------+
| 1 | SIMPLE | markets | NULL | ALL | index_markets_on_visible| NULL | NULL | NULL | 10 | 70.00 | Using where; Using filesort |
+----+-------------+---------+------------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+------+---------+------+------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
我想知道:
index\u markets\u on\u visible
,为什么仍然记录查询并将其视为:“not indexed query”
SHOW VARIABLES LIKE 'log_queries_not_using_indexes';
它可能会在上显示。将其关闭
。(你已经发现了为什么我认为这种设置基本上是无用的。)
如果您想要一个有用的索引,请将复合索引设置为索引(可见,位置)
它仍然可以说它没有使用任何索引。原因如下。7/17行匹配visible=TRUE
。这超过了(大约)表的20%,因此优化器认为不值得使用索引,但简单地扫描所有17行,跳过不可见的
行可能会更快
另一个注意事项:如果它确实使用我的索引,它将避免“使用文件排序”,这将进一步提高性能。布尔列上的索引完全是浪费时间和空间。它只是将列分为TRUE和FALSE,即平均分为行数的一半。因此,它不会实质性地提高性能。所以MySQL忽略了它。