Reputation: 2884
I have the following query:
SELECT SUM(sureness) FROM tweet WHERE ID in
(select DataitemID from entity_dataitem_relation where EpochID IN
(SELECT ID FROM epoch WHERE StartDateTime>='2013-11-01' AND EndDateTime<='2013-11-31')AND
DataitemType=3) ;
And I indexed DataitemID in entity_dataitem_relation table to increase the speed. I even indexed EpochID to help increasing speed but still this query is very slow and it takes 2 min to be executed(it is noteworthy that I entered in a project in the middle of that so I have to continue with what others did so I dont have much flexibility in terms of design)
NB: the following part is very fast:
SELECT DataitemID from entity_dataitem_relation where EpochID IN
(SELECT ID FROM epoch WHERE StartDateTime>='2013-11-01' AND EndDateTime<='2013-11-31');
Another thing : even when I replace sureness with ID which is prinmary key of the table it still takes that much time what should I do ? Is there anything that I can do to improve speed?
Upvotes: 0
Views: 303
Reputation: 53830
To try to reduce table scanning, you can rewrite this query using JOINS:
SELECT SUM(t.sureness)
FROM tweet t
JOIN entity_dataitem_relation edr
ON edr.DataitemID = t.ID
AND edr.DataitemType = 3
JOIN epoch e
ON e.ID = edr.EpochID
AND e.StartDateTime >= '2013-11-01'
AND e.EndDateTime <= '2013-11-31'
Add the following covering indexes:
tweet(ID, sureness)
entity_dataitem_relation(DataitemID, DataitemType, EpochID)
epoch(ID, StartDateTime, EndDateTime)
MySQL will probably still scan through all the rows in the index on tweet
, so the more records you have in the tweet
table, the slower this will be.
Upvotes: 2