|
Hi All, Kindly help me on the below query, In most of the queries we are using the below query as sub query to restrict the employees who are on leave, but due to this query taking much time for execution and it is causing performance problems. please suggest me to tune this one to run my queries faster Table structures like below :
(comments are locked)
|
|
The first thing that jumps out at me is the DATEDIFF in the where clause. Whenever you use a function on a column in the WHERE clause, you lose much of the efficiency that comes from the underlying indexes. If you can find a way to express that formula that doesn't involve using a function on the Speaking of indexes, if you haven't already, it could be useful to create an index on the On the table side, it would be nice to use the DATE datatype for the
(comments are locked)
|
|
You say this query is taking "much time for execution". How long does it take, how much data are you working with? I have tested the query with two tables as you define them and I can query 10,000 employees with 500,000 leave records and get 120,000 records in the result set in approx 1s. The Execution Plan has a cost of 0.0032831 which is pretty low. I agree with the other answers saying the functions in your WHERE clause are undesirable but am wondering if you might be better off looking elsewhere for your slow TSQL. Can you post any more of your scenario - how you use this query etc etc? That's a great take on the difficulty of tuning a single query in isolation instead of addressing the larger context.
May 09 '12 at 02:35 PM
KenJ
yeah, I'm wondering if this is appearing slow simply because it is being used 000's of times and. We may end up putting the results in a temp table to join on and save a lot of work but we need more info. Actually having problems repro my figures, think I may have missed clearing the cache between executions. Still not what I'd call slow though ...
May 09 '12 at 02:56 PM
Fatherjack ♦♦
(comments are locked)
|
|
this "(DATEDIFF(day,leav_start,leav_end)+1)-(DateDiff(week,leav_start,leav_end)*2) >=3" is killing your query. Are you able to store a no_of_days in your table instead of calculating it in this query ? anyway that part looks like calculating the of of days excluding weekend. You are better off calculating that and store it in the table when the leave record is created.
(comments are locked)
|

