[Solved] How to increase the performance of the query

EverSQL Database Performance Knowledge Base

How to increase the performance of the query

Database type:

Using SQL Server 2000

Holiday (table)

dates

2012-08-02 
2012-08-19 
2012-08-20 

Table1

id dates time

001 01/08/2012 00:00
001 02/08/2012 00:00
001 03/08/2012 00:00
001 04/08/2012 12:00
...
...
001 17/04/2012 11:00
001 18/08/2012 00:00
001 19/08/2012 00:00
001 20/08/2012 00:00
001 21/08/2012 12:00
...

I want to check previous date and next date of each column, then I want to update present or absent or holiday.

Condition's

Note: this query is running witout any fail, only concern is time consuming...

Query

Select 
    t2.id, 
    t2.dates, 
    case 
        when t2.time <> '00:00' 
            then 'P' 
        when t4.dates = t2.dates and t1.intime = '00:00' and t3.intime = '00:00' 
            then 'AB' 
        else 'H' 
    end as attn 
from
    (
        Select id, dates, time from table1 
        where t1.dates = Cast('18/08/2012' as datetime)
    ) t1 
    left outer join
    (
        Select id, dates, time from table1 
        where t2.dates = Cast('19/08/2012' as datetime)
    ) t2 
    on t1.id = t2.id
    left outer join 
    (
        Select id, dates, time from table1 
        where t2.dates = Cast('20/08/2012' as datetime)
    ) t3 
    on t2.id = t3.id
    left outer join 
    (
        select dates from holiday
    ) t4 
    on t4.dates = t2.dates

The above query is working fine, but it was taking more time to display because I want to view the data from 01/09/2012 to 30/09/2012 for each id, i have n number of id, system is checking previous date and next date for each id and showing the result.

Any other alternative query or solution is there for displaying the data

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Prefer Direct Join Over Joined Subquery (query line: 21): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
  3. Prefer Direct Join Over Joined Subquery (query line: 33): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
  4. Prefer Direct Join Over Joined Subquery (query line: 45): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
  5. Prefer Direct Join Over Joined Subquery (query line: 11): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
Optimal indexes for this query:
CREATE INDEX holiday_idx_dates ON holiday (dates);
CREATE INDEX table1_idx_dates_id ON table1 (dates,id);
The optimized query:
SELECT
        t2.id,
        t2.dates,
        CASE 
            WHEN t2.time <> '00:00' THEN 'P' 
            WHEN t4.dates = t2.dates 
            AND t1.intime = '00:00' 
            AND t3.intime = '00:00' THEN 'AB' 
            ELSE 'H' END AS attn 
FROM
table1 t1 
LEFT OUTER JOIN
table1 t2 
    ON t1.id = t2.id 
    AND t2.dates = CAST('19/08/2012' AS datetime) 
LEFT OUTER JOIN
table1 t3 
    ON t2.id = t3.id 
    AND t2.dates = CAST('20/08/2012' AS datetime) 
LEFT OUTER JOIN
holiday t4 
    ON t4.dates = t2.dates 
WHERE
(
    t1.dates = CAST('18/08/2012' AS datetime)
)

Related Articles



* original question posted on StackOverflow here.