1

i have a mysql database with about 60 Million records. i will have to generate report every day.

I have two type of sql statement.

Method1

SELECT TransferStatus as Transfer_Status, count(ID) as Count 
FROM calldetails 
WHERE YEAR(IVRStartTime) = '2015' AND MONTH(IVRStartTime) = '11' AND DAy(IVRStartTime)='05' AND CallTransfer LIKE '163%' 
GROUP BY TransferStatus;

Method2

SELECT TransferStatus as Transfer_Status,  
count(ID) as Count FROM  calldetails 
WHERE  (IVRStartTime BETWEEN '2015-11-05 00.00.00' AND '2015-11-05 23.59.59') 
AND CallTransfer LIKE '163%'  
GROUP BY TransferStatus

I tried both queries both takes approximately 15 minutes to give results. My question is which one is the best to use and is there any other way i can improve the performance.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Sathya Baman
  • 3,424
  • 7
  • 44
  • 77

2 Answers2

3

Just use the DATE function to return the date part of your datetime value

SELECT TransferStatus as Transfer_Status,  
count(ID) as Count FROM  calldetails 
WHERE  DATE(IVRStartTime) = DATE('2015-11-05') 
AND CallTransfer LIKE '163%'  
GROUP BY TransferStatus

That will be more efficient for handling the date condition.

You need to use EXPLAIN to see what the execution plan is so that you can optimise it with indexes to speed up the query

David Soussan
  • 2,698
  • 1
  • 16
  • 19
2

use BETWEEN best approach

SELECT TransferStatus as Transfer_Status,  
count(ID) as Count FROM  calldetails 
WHERE  (IVRStartTime BETWEEN '2015-11-05 00.00.00' AND '2015-11-05 23.59.59') 
AND CallTransfer LIKE '163%'  
GROUP BY TransferStatus

this one is best because BETWEEN perform fast as compare many where clause

or use index is always be a good habit because :-

A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookups and efficient ordering of access to records.

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
  • Tnx. I created the index and tested both sql statement. This one works well , it takes approximately 2 minutes where other one takes approximately 10 minutes to give results. – Sathya Baman Nov 28 '15 at 15:05