【发布时间】:2014-02-09 04:34:20
【问题描述】:
我有以下疑问:
SELECT TIMESTAMPDIFF(MINUTE, firstOccurrence, lastOccurrence) as dwellTime
FROM ts_VisitorDuration
WHERE eventDate >= '2012-12-01'
AND eventDate <= '2014-03-03'
AND venueID = 1007
GROUP BY MACAddress, eventDate
运行需要 12 到 17 秒(慢!)。我相信我可以使用索引来加快速度,因为下面的查询需要
SELECT MACAddress
FROM ts_VisitorDuration
WHERE eventDate >= '2012-12-01'
AND eventDate <= '2014-03-03'
AND venueID = 1007
GROUP BY MACAddress, eventDate
我尝试对我认为正确的列进行索引,但它似乎并没有影响性能。
这是顶部(慢)查询的EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE ts_VisitorDuration range PRIMARY,venueID venueID 7 NULL 167776 Using where; Using temporary; Using file sort
在桌子上使用SHOW INDEX FROM 给出:
Table Non_unique Key_name Seq_in_index Column_name Collation
Cardinality Sub_part Packed Null Index_type Comment Index_comment
ts_VisitorDuration 0 PRIMARY 1 eventDate A 21 NULL NULL BTREE
ts_VisitorDuration 0 PRIMARY 2 MACAddress A 11714214 NULL NULL BTREE
ts_VisitorDuration 0 PRIMARY 3 venueID A 11714214 NULL NULL BTREE
ts_VisitorDuration 1 venueID 1 venueID A 4542 NULL NULL BTREE
ts_VisitorDuration 1 venueID 2 eventDate A 172267 NULL NULL BTREE
ts_VisitorDuration 1 MACAddress 1 MACAddress A 11714214 NULL NULL BTREE
ts_VisitorDuration 1 MACAddress 2 eventDate A 11714214 NULL NULL BTREE
ts_VisitorDuration 1 MACAddress 3 venueID A 11714214 NULL NULL BTREE
ts_VisitorDuration 1 MACAddress 4 firstOccurrence A 11714214 NULL NULL BTREE
ts_VisitorDuration 1 MACAddress 5 lastOccurrence A 11714214 NULL NULL YES BTREE
ts_VisitorDuration 1 firstOccurrence 1 firstOccurrence A 11714214 NULL NULL BTREE
有人可以向我解释索引是否会加快我的顶级查询,如果可以,我如何确定我需要索引什么?
谢谢!
【问题讨论】:
标签: mysql performance indexing