简体   繁体   中英

How to determine what to Index in a MySQL Table

I have the following query:

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

which takes 12 to 17s to run (slow!). I believe I can use indexes to speed this up, because the below query takes < 1 second to run - with the same table and conditions in the WHERE clause:

SELECT MACAddress
FROM ts_VisitorDuration
WHERE eventDate >= '2012-12-01'
AND eventDate <= '2014-03-03'
AND venueID = 1007
GROUP BY MACAddress, eventDate

I have tried to doing Indexing on what I thought were the right columns, but it did not seem to impact performance.

Here is the EXPLAIN of the top (slow) query:

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

And using SHOW INDEX FROM on the table gives:

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       

Can someone explain to me if Indexing will speed up my top query, and if so, how I can determine what I need to Index?

Thank you!

you want to index the fields that you are filtering. ie the WHERE fields. think of it like a book. The "WHERE" fields are what you are looking for in the book. With no index, you have no table of contents, so when you want to find info you have to search every page, every word. IF you have an index, in your book for this example, you can look at table of contents and find that chapter X has your info, so turn to that chapter, and you have found your data much faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM