我有以下表格:
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 |
+----+-------------+----------------+-------+---------------+--------+---------+--------+--------+-------------+
答案 0 :(得分:2)
MySQL不能在函数内的列上使用索引
从select中删除函数date()
,MySQL将使用索引。
您可以通过将forwardtime
的列定义更改为DATE
来实现此目的
或者您可以像这样更改查询
SELECT
f_shop
, COUNT(*) as RowCount
, SUM(toolbarUser=1) as NumberOfToolbarUsers
FROM sal_forwarding
WHERE forwardTime BETWEEN '2011-09-01 00:00' AND '2011-09-01 23:59'
GROUP BY f_shop
备注
count(*)
比count(namedcolumn)
快; if(a=1,1,0)
; forwardtime
),则查询将运行得更快。 KEY fasttime (forwardTime,f_shop,toolbarUser)