Question

I am running a query -

SELECT COUNT(DISTINCT c.msisdn) 
FROM cdr c 
WHERE c.evedate>='2013-10-01';

On executing first time, taking 1 minute. But executing the same query within second taking 3 minutes.

Why is it happening?

I have found the same behavior with other queries too.

Details if required:
My machine have 4 GB RAM
Mysql 5.5 version.
key_buffer_size = 350M.

This table have 140 million records with 10 partitions.

Thank you.

Was it helpful?

Solution

Do you have an index on

 ( eveDate, msisdn )

This would make it a covering index and not have to go to the actual data pages for the ID, yet be query optimized by the "eveDate" portion of the where clause.

Suggestion per feedback.

If the data is date-based, I would suggest having a separate table that had nothing but the date in question (or hourly if that might be better for your work as you stated you had an index by the hour) and a count of IDs. If the day's activity is not going to change (or rarely), this table just sits there with a count. Even if you ran it nightly to update counts on a per day/hour basis, such as for the last 10 days, your subsequent queries would be almost instantaneous.

Additionally, you might even just create a trigger on the table in question with all these records. As a new record is added, it just does an insert/update to the counters table something like...

update CountersSummary
   set TotalCount = TotalCount +1
   where eveDate = just date portion of DateTimeValueOfNewRecord
      and eveHour = hour of DateTimeValueOfNewRecord

if the record count updated = 0
   insert into CountersSummary ( eveDate, eveHour, TotalCount )
      values ( just date portion of DateTimeValueOfNewRecord,
               hour of DateTimeValueOfNewRecord,
               1 )
end if
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top