2

Good day. I am tasked with trying to speed up some slow running queries, but being a MYSQL newbie I'm not sure if I have achieved the best possible result. I realize the forum is littered with these types of questions already and I have read through a lot of them, but I would still appreciate some further assistance if that is possible.

I started the optimizing procedure by stripping away the joins and first trying to speed up the basic select. The table has about 4 400 000 entries in it, and the query returns in the region of 1 800 000 entries.

I started with this:

select  
   ID,
   CallStarted,
   CallDirection 
from 
   TRMSMain.tblcalldata CALLDATA  
   where CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59' 
ORDER BY ID DESC LIMIT 0 , 50;

It took about 360 seconds. This, however took 2 seconds:

select
  ID,
  CallStarted,
  CallDirection 
from 
  TRMSMain.tblcalldata CALLDATA  
where CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59' 
LIMIT 0 , 50;

Although the last query returned the first 50 in the range, instead of the last 50, it led me to believe that the "order by desc" operation is very expensive. I then fiddled around a bit and came up with the following (which of course is not the only way to have done it), which takes anywhere from 20 to (sometimes) 40 seconds:

use trmsmain;
RESET QUERY CACHE;
drop procedure if exists intTest;
delimiter #
create procedure intTest()
BEGIN
    declare lastID int unsigned default 0;
    declare frstID int unsigned default 0;
    select
        (select max(ID) from trmsmain.tblcalldata where  (CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59'))
        into lastID;
    set     frstID = lastID - 49;
    CREATE TEMPORARY TABLE IF NOT EXISTS table2 (index (ID)) AS
    (       
        select
            ID,
            CallStarted,
            CallDirection
        from
            trmsmain.tblcalldata CALLDATA  
        where
            ID between frstID AND lastID
    );
    select * from table2 order by ID desc;
END #
delimiter ;
call intTest();

The results are exactly what I need, and callstarted is an indexed field, but my question is if this kind of performance is acceptable (the best I can expect). My PC is mid range with 4GB of Ram.

Please advise. Thank you.

MORE INFO:

My SQL script equates to this:

select
    ID,
    CallStarted,
    CallDirection
  from trmsmain.tblcalldata CALLDATA  

where  ID between (select(select max(ID) from trmsmain.tblcalldata  where  (CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59'))  - 49) AND 
(select max(ID) from trmsmain.tblcalldata   where  (CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59'));

I only did it the way I did because I wanted to see if the above would re-use the result of MAX, so I compared the result of the above to that of the script (which was similar).

After I looked at he explain, I was confused and removed all the indexes except for the one on callstarted. ID is the primary key.

Having only the one index brought the time down to one second. Even when using "force index(callstarted) I could not come close to this result.

I am somehow confused even further now.

Regards

4
  • 2
    If you want help with performance, you would typically need to provide proper DDLs and the result of the EXPLAIN.
    – Strawberry
    Jan 12, 2015 at 12:22
  • Do you have any primary key? What about the Index and TABLE Schema?
    – Sridhar DD
    Jan 12, 2015 at 12:23
  • 1 800 000 entries is a huge number to return, and represents nearly a half of your table. Is there a way you can reduce the number of entries you require? Jan 12, 2015 at 12:24
  • Do you have index on CallStarted?
    – i486
    Jan 12, 2015 at 12:29

3 Answers 3

0

If you know the ids are assigned sequentially, you could try this:

select ID, CallStarted, CallDirection 
from TRMSMain.tblcalldata CALLDATA  cd JOIN
     (select MIN(id) as minid, MAX(id) as maxid
      from TRMSMain.tblcalldata CALLDATA cd2
      where CallStarted BETWEEN '2014-02-10' AND '2014-05-11 23:59:59'
     ) s
     on cd.id >= minid and cd.id <= maxid
order by id desc
limit 0, 50;

The performance this could be faster than your version, if MySQL is smart enough to the use the index for both the on and order by at the same time.

Try creating an index on TRMSMain.tblcalldata(CallsStarted).

1
  • This gives the correct result but takes about 100 seconds. Thanks for taking the time to try and help.
    – Anton
    Jan 12, 2015 at 12:42
0

Thanks for all those who took the time to look at my question, I am always amazed by the support and help that one gets from the computing community. It would seem my problem was that the correct index was not being used, and also the workaround that replaced the "ORDER BY DESC" also shaved a lot off the time. I will try and use what I discovered here to improve my code. Keep well.

0

If you are looking for the most recent per your date/time, and per your feedback, the calls are inserted WHEN THE CALL HAS ENDED, thus would be sequential based on the END Time of the call. Sample of the data:

ID   CallStarted          CallEnded
1    2015-01-12  8:37     2015-01-12  8:44
2    2015-01-12  8:42     2015-01-12  8:52
3    2015-01-12  8:29     2015-01-12  9:09
4    2015-01-12  8:56     2015-01-12  9:11
5    2015-01-12  8:30     2015-01-12  9:12
6    2015-01-12  9:09     2015-01-12  9:32

If this is the case, does it make more sense to do your query based on when the call ENDED instead of starting? The only time you would have an issue is for those phone calls that are explicitly rolling over midnight to the next day.

I would still have a COVERING INDEX that has the elements you are interested in so it does not have to go back to the raw data pages, but can get them from the index directly.. in this case move the call END Date into the index in primary position:

Build an index on (CallEnded, CallStarted, ID, CallDirection)

Then try

select
      C.ID,
      C.CallStarted,
      C.CallDirection 
   from 
      TRMSMain.tblcalldata C  
   where 
      C.CallEnded BETWEEN '2014-02-10' AND '2014-05-11 23:59:59'
   order by
      C.CallEnded DESC
   LIMIT 
      0, 50

Would this work for you...

3
  • Unfortunately calls are added only after the call is done, so it might be out of order now and again. Thanks anyway for looking at my question.
    – Anton
    Jan 12, 2015 at 14:02
  • @Anton, revised possibility?
    – DRapp
    Jan 12, 2015 at 14:27
  • @Anton, revised possibility -- BASED on CALL ENDED?
    – DRapp
    Jan 12, 2015 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.