Hello,
I’m trying to make the right index, to speed up the following query:
SELECT
FROM_DAYS(TO_DAYS(D.timein)) as date,
TO_DAYS(D.timein) as day, DATE_FORMAT(D.timein, ‘%H:%i’) AS timein,
DATE_FORMAT(D.timeout, ‘%H:%i’) AS timeout,
D.locID AS location,
(UNIX_TIMESTAMP(timeout)-UNIX_TIMESTAMP(timein))/60 as timemins,
D.adjustment,
D.note1,
D.note2,
F.ID AS fid,
F.description AS fdesc,
F.factor AS ffactor,
F.hours AS fhours, ((TO_DAYS(D.timein) % 7) >= 2) AS weekday,
(TO_DAYS(D.timein) % 7) AS dayno
FROM tid D
LEFT JOIN tidtypes F ON F.ID = D.tidID
WHERE
D.userID = 2
AND D.timein < DATE_ADD(‘2014-02-09’, INTERVAL 1 DAY)
AND D.timein >= ‘2014-02-02’
AND D.ID != 0
ORDER BY date, F.ID DESC, timein
I’m using Percona Server 5.6, and have made a index with the fields that are used to limit the query with in the order they are accessed:
Table TID:
Index: tidid, userid, timeid, id:
But the server does not use the index, the result of explain is:
±—±------------±------±-------±--------------±--------±--------±---------------±-----±----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
±—±------------±------±-------±--------------±--------±--------±---------------±-----±----------------------------+
| 1 | SIMPLE | D | range | PRIMARY | PRIMARY | 3 | NULL | 2935 | Using where; Using filesort |
| 1 | SIMPLE | F | eq_ref | PRIMARY | PRIMARY | 1 | phptid.D.tidID | 1 | NULL |
±—±------------±------±-------±--------------±--------±--------±---------------±-----±----------------------------+
Can someone give me pointers to what the correct indexes for the above SQL would be, and why.
Thanks in advance for your input
Regards,