gpt4 book ai didi

mysql - 索引未在 MySQL 查询中使用

转载 作者:行者123 更新时间:2023-11-30 22:56:19 27 4
gpt4 key购买 nike

我已经编写了下面的查询,它需要将近 5 分钟才能运行。我在表中有600万行数据,从执行计划中发现,即使表的所有字段都有索引,我的查询也没有使用索引。

查询

SELECT 
event_date as date,
(CAST('2014-05-31' AS DATE)- INTERVAL 5 MONTH + INTERVAL 1 DAY) AS FROM_DATE,
COUNT(DISTINCT(IF( Column1 !=0 OR Column2!=0 OR Column3 !=0, account, NULL))) AS total_account1,
COUNT(DISTINCT(IF( Column4 !=0 OR Column5 !=0 OR Column6!=0, account, NULL))) AS total_account2,
COUNT(DISTINCT(IF( Column7 !=0 OR Column8 !=0 OR Column9!=0, account, NULL))) AS total_account3
FROM Table_name
WHERE cast(event_date as DATE) BETWEEN CAST('2014-05-31' AS DATE)- INTERVAL 5 MONTH and CAST('2014-05-31' AS DATE)
AND cast(event_date as DATE) < NOW() - INTERVAL 2 DAY
GROUP BY MONTH(event_date)

查询输出上面的“解释”是-

+----+-------------+---------+------+---------------+------+---------+------+---------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+---------+-----------------------------+
| 1 | SIMPLE | table_name | ALL | NULL | NULL | NULL | NULL | 5764552 | Using where; Using filesort |
+----+-------------+---------+------+---------------+------+---------+------+---------+-----------------------------+

为什么我的查询没有使用可用的索引?

最佳答案

您可以明确强制引擎使用索引。

检查一下 http://dev.mysql.com/doc/refman/5.1/en/index-hints.html

关于mysql - 索引未在 MySQL 查询中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26234278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com