Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am doing attendance application. In my application I have a table for store Punch in/out
I create indexing for this table for following columns (EntryDate, EmployeeID)

I need to read First Time In for all Employees so I am using following query

SQL
Select (select min( TimeIn) from tbl_TimeInOutEntry WITH(INDEX( tmescangardindx) 

where EmployeeID = E.EmployeeID and  EntryDate = '2015-03-02' group by 

EntryDate,EmployeeID) as TimeIn

from tbl_employee E where E.CompanyID = 1 


But this query Execution is nearly 30 sec.

One scenario is when I have high number of records in tbl_TimeInOutEntry then this query execution is very slow.

Can you give a suggestion for optimize this query

Thanks
Siva Rm K

Attachments

Execution Plan

Timescangard detail -> TimeInTimeout Table
Posted
Updated 15-Mar-15 21:54pm
v2
Comments
Saral S Stalin 16-Mar-15 3:03am    
Can you post a screen shot of the execution plan?

An index on TimeIn, EmployeeId columns might help. But if the table is heavily inserted indexes will get fragmented soon. This will require detailed analysis and proper fill factor.

Having another table which has only the first entry for the day also might help. This table can be populated with a trigger or message broker.
rmksiva 16-Mar-15 3:16am    
Thanks For your reply. i will share you execution plan in a moment
Maciej Los 16-Mar-15 4:18am    
Have you tried to reduce query into single select statement (remove subquery)?

 
Share this answer
 
Comments
Maciej Los 16-Mar-15 4:24am    
Good suggestion. +5!
Please, see my answer ;)
As i mentioned in the comment to the question, i'd suggest to create query based on single select statement (reduce the count of subqueries).

If you want to get first time for the selected date and company, try this:
SQL
SELECT MIN(TI.TimeIn) AS TimeIn
FROM tbl_employee EM INNER JOIN tbl_TimeInOutEntry TI ON EM.EmployeeID = TI.EmployeeID
WHERE TI.EntryDate = '2015-03-02' AND  EM.CompanyID = 1


Note: Please, follow the solution1 by Mehdi Gholam[^] to reduce the count of indexes.
 
Share this answer
 
Comments
Mehdi Gholam 16-Mar-15 4:26am    
Simple is always good! 5'ed!
Maciej Los 16-Mar-15 4:27am    
Thank you, Mehdi ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900