I think that your search by template probably slows it down, since you cannot use indexes in these fields. Also, if you can avoid subqueries and just do a direct connection, this may help, but finding wildcards is much worse. In any case, can you modify the table to have categoryName or categoryID, which can have an index and do not require wildcard searches? For example, where categoryName = 'Outlook' "
To optimize the data in your tables, add a category identifier (ideally this would be a link to a separate table, but just use arbitrary numbers for this example):
alter table rawData add column categoryID int not null alter table rawData add index (categoryID)
Then fill in the categoryID field for existing data:
update rawData set categoryID=1 where name like '%Outlook%' update rawData set categoryID=2 where name like '%Facebook%'
Then change your insert to follow the same rules.
Then make a SELECT query this way (change wild cards to categoryID):
SELECT rawdata.user, t1.Facebook_Time, t2.Outlook_Time, t3.Excel_time FROM rawdata left join (SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Facebook_Time' FROM rawdata WHERE categoryID = 2 GROUP by user)t1 on rawdata.user = t1.user left join (SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Outlook_Time' FROM rawdata WHERE categoryID = 1 GROUP by user)t2 on rawdata.user = t2.user left join (SELECT user, sec_to_time(SuM(time_to_sec(EndTime-StartTime))) as 'Excel_Time' FROM rawdata WHERE categoryID = 3 GROUP by user)t3 on rawdata.user = t3.user
source share