@yossiz
נראה שיש באמת שינוי משמעותי לאחר סידור האינדקסים כפי שכתבת למעלה.
להלן השוואה:
עבור שאילתה:
EXPLAIN ANALYZE SELECT * FROM (SELECT * FROM `Pupils` WHERE `ProjectID` LIKE 'tifertMoshe') AS Pupils LEFT JOIN (SELECT `EnterId`
, MAX(IF(`EnterDate` = CURRENT_DATE ,co,0)) AS 'day_co', MAX(IF(`EnterDate` = CURRENT_DATE,SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'day_su'
,MAX(IF(`EnterDate` = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY) ,co,0)) AS 'Yesterday_co', MAX(IF(`EnterDate` = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY),SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'Yesterday_su'
, MAX(IF(YEARWEEK(`EnterDate`, 1) = YEARWEEK(CURRENT_DATE, 1) ,co,0)) AS 'week_co', MAX(IF(YEARWEEK(`EnterDate`, 1) = YEARWEEK(CURRENT_DATE, 1),SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'week_su'
, MAX(IF(YEARWEEK(`EnterDate`, 1) = YEARWEEK(DATE_SUB(CURRENT_DATE,INTERVAL 7 DAY), 1) ,co,0)) AS 'lest_week_co', MAX(IF(YEARWEEK(`EnterDate`, 1) = YEARWEEK(DATE_SUB(CURRENT_DATE,INTERVAL 7 DAY), 1),SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'lest_week_su'
, MAX(IF(MONTH(`EnterDate`) = MONTH(CURRENT_DATE) ,co,0)) AS 'month_co', MAX(IF(MONTH(`EnterDate`) = MONTH(CURRENT_DATE),SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'month_su'
, MAX(IF(MONTH(`EnterDate`) = MONTH(DATE_SUB(CURRENT_DATE,INTERVAL 1 MONTH)) ,co,0)) AS 'lest_month_co', MAX(IF(MONTH(`EnterDate`) = MONTH(DATE_SUB(CURRENT_DATE,INTERVAL 1 MONTH)),SEC_TO_TIME(CONVERT(su, SIGNED)),0)) AS 'lest_month_su'
, MAX(co) AS all_co
, SEC_TO_TIME(CONVERT(SUM(su), SIGNED)) AS all_su
FROM (SELECT `EnterId`, COUNT(`id`) AS co, SUM(`TimeTotal`) AS su, `EnterDate` FROM `LogPlaybackPlayStop` WHERE `ProjectID` LIKE 'tifertMoshe' GROUP BY `EnterId`, `EnterDate`) AS t1 GROUP BY EnterId) AS t2 ON `Pupils`.`user_id` = `t2`.EnterId
ORDER BY `class`, `Family`, `name`
בטבלה המקורית לקח הביצוע 2.651 שניות
וזה התוצאה
-> Nested loop left join (actual time=2565.828..2568.774 rows=716 loops=1)
-> Sort: Pupils.class, Pupils.Family, Pupils.`name` (cost=812.76 rows=716) (actual time=9.546..9.867 rows=716 loops=1)
-> Index range scan on Pupils using ProjectID, with index condition: (Pupils.ProjectID like 'tifertMoshe') (actual time=0.423..4.044 rows=716 loops=1)
-> Filter: (Pupils.user_id = t2.EnterId) (actual time=3.573..3.574 rows=1 loops=716)
-> Index lookup on t2 using <auto_key0> (EnterId=Pupils.user_id) (actual time=0.002..0.002 rows=1 loops=716)
-> Materialize (actual time=3.573..3.573 rows=1 loops=716)
-> Table scan on <temporary> (actual time=0.001..0.513 rows=666 loops=1)
-> Aggregate using temporary table (actual time=2553.656..2554.225 rows=666 loops=1)
-> Table scan on t1 (actual time=0.002..3.429 rows=16831 loops=1)
-> Materialize (actual time=2454.324..2459.523 rows=16831 loops=1)
-> Table scan on <temporary> (actual time=0.002..5.742 rows=16831 loops=1)
-> Aggregate using temporary table (actual time=2437.997..2445.624 rows=16831 loops=1)
-> Index range scan on LogPlaybackPlayStop using ProjectID, with index condition: (LogPlaybackPlayStop.ProjectID like 'tifertMoshe') (cost=717075.83 rows=641706) (actual time=138.827..1616.996 rows=354415 loops=1)
בטבלה שבה סידרתי את האינדקסים כפי שכתבת:
זמן ביצוע לקח 2.079
והתוצאה
-> Nested loop left join (actual time=1946.354..1956.694 rows=716 loops=1)
-> Sort: Pupils.class, Pupils.Family, Pupils.`name` (cost=812.76 rows=716) (actual time=5.865..6.479 rows=716 loops=1)
-> Index range scan on Pupils using ProjectID, with index condition: (Pupils.ProjectID like 'tifertMoshe') (actual time=0.252..1.879 rows=716 loops=1)
-> Filter: (Pupils.user_id = t2.EnterId) (actual time=2.718..2.721 rows=1 loops=716)
-> Index lookup on t2 using <auto_key0> (EnterId=Pupils.user_id) (actual time=0.003..0.003 rows=1 loops=716)
-> Materialize (actual time=2.715..2.717 rows=1 loops=716)
-> Table scan on <temporary> (actual time=0.002..0.549 rows=666 loops=1)
-> Aggregate using temporary table (actual time=1937.403..1938.763 rows=666 loops=1)
-> Table scan on t1 (actual time=0.002..11.759 rows=16831 loops=1)
-> Materialize (actual time=1842.499..1875.116 rows=16831 loops=1)
-> Table scan on <temporary> (actual time=0.002..13.555 rows=16831 loops=1)
-> Aggregate using temporary table (actual time=1793.238..1827.118 rows=16831 loops=1)
-> Index range scan on LogPlaybackPlayStop using ProjectID, with index condition: (LogPlaybackPlayStop.ProjectID like 'tifertMoshe') (cost=838658.37 rows=733932) (actual time=79.989..1189.331 rows=354415 loops=1)
ולמען האמת מהשאילתה עם EXPLAIN ANALYZE אני עדיין לא כ"כ מבין את הלוגים, אבל בהרצת השאילתה עצמה ושליפת התוצאות אני רואה:
בטבלה המקורית 2.686
בטבלה לאחר השינוי 1.510