我的 MySQL 查询的正确索引
我有下面的表:
CREATE TABLE `sal_forwarding` (
`sid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`f_shop` INT(11) NOT NULL,
`f_offer` INT(11) DEFAULT NULL,
.
.
.
PRIMARY KEY (`sid`),
KEY `forwardTime` (`forwardTime`,`f_shop`),
KEY `forwardTime_2` (`forwardTime`),
KEY `f_shop` (`f_shop`)
) ENGINE=INNODB AUTO_INCREMENT=10457068 DEFAULT CHARSET=latin1
该表有超过 500 万行。
我已经设置了索引,正如您在上面看到的,但是在我的查询中没有使用索引,我不明白为什么。有人看到我的问题吗?
解释:
EXPLAIN SELECT
f_shop
, COUNT(sid)
, SUM(IF(toolbarUser=1,1,0))
FROM sal_forwarding
WHERE DATE(forwardTime) = "2011-09-01"
GROUP BY f_shop
结果:
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
| ID | SELECT_TYPE | TABLE | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | EXTRA |
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
| | | | | | | | | | |
| 1 | SIMPLE | sal_forwarding | index | (NULL) | f_shop | 4 | (NULL) | 232449 | Using where |
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
I have the following Table:
CREATE TABLE `sal_forwarding` (
`sid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`f_shop` INT(11) NOT NULL,
`f_offer` INT(11) DEFAULT NULL,
.
.
.
PRIMARY KEY (`sid`),
KEY `forwardTime` (`forwardTime`,`f_shop`),
KEY `forwardTime_2` (`forwardTime`),
KEY `f_shop` (`f_shop`)
) ENGINE=INNODB AUTO_INCREMENT=10457068 DEFAULT CHARSET=latin1
This table has more than 5 million rows.
I've set indexes, as you can see above, but in my query no indexes are used and I don't understand why. Does anybody see my problem?
Explain:
EXPLAIN SELECT
f_shop
, COUNT(sid)
, SUM(IF(toolbarUser=1,1,0))
FROM sal_forwarding
WHERE DATE(forwardTime) = "2011-09-01"
GROUP BY f_shop
Result:
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
| ID | SELECT_TYPE | TABLE | TYPE | POSSIBLE_KEYS | KEY | KEY_LEN | REF | ROWS | EXTRA |
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
| | | | | | | | | | |
| 1 | SIMPLE | sal_forwarding | index | (NULL) | f_shop | 4 | (NULL) | 232449 | Using where |
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL 无法在函数内的列上使用索引。
从 select 中删除函数
date()
,MySQL 将使用索引。您可以通过将
forwardtime
列定义更改为DATE
来实现此目的
或者您可以更改查询,如下所示
Remarks
count(*)
比count(namedcolumn)
更快;if(a=1,1,0)
;forwardtime
),查询将运行得更快。KEY fasttime (forwardTime,f_shop,toolbarUser)
MySQL cannot use an index on a column inside a function.
Remove the function
date()
from your select and MySQL will use the index.You can do this by changing your column definition of
forwardtime
toDATE
Or you can change the query like so
Remarks
count(*)
is faster thancount(namedcolumn)
;if(a=1,1,0)
can be shortened;forwardtime
), you query will run even faster.KEY fasttime (forwardTime,f_shop,toolbarUser)