Mike
Mike

Reputation: 1738

Query that uses Clustered Index Scan instead of seek

I have the following query that returns < 300 results. It is currently taking about 4 seconds to complete, and when I look at the execution plan, it shows that it is spending 41% of resources on a clustered index scan. My limited knowledge of database administration suggests that a clustered index seek would improve performance. How can I get the query to use a clustered index seek instead of a clustered index scan? Below is the pertinent information and the query.

    DECLARE @start date, @end date
    SET @start = '2013-01-01'
    SET @end = CAST(GETDATE() AS DATE)
    SELECT 
        b.total,    
        c.intakes, 
        d.ships, 
        a.CODE_, 
        RTRIM(a.NAME_) as name, 
        f.employee as Salesperson,
        g.referral_type_id, 
        h.referral_type,
        e.slscode, 
        a.city, 
        a.STATE_, 
        a.zip
    FROM PACWARE.ADS.RFDME a
    LEFT OUTER JOIN (
        SELECT SUM(b.quantity) total, a.ref_id from event.dbo.sample a 
        JOIN event.dbo.sample_parts b on a.id = b.sample_id
        JOIN PACWARE.ADS.PTDME c on b.part_id = c.CODE_
        WHERE c.MEDICAREID = 'E0607' AND a.order_date between @start and @end
        GROUP BY  a.ref_id
    )b on a.CODE_ = b.ref_id
    LEFT OUTER JOIN (
        SELECT COUNT(a.CODE_)as intakes, rfcode
        FROM PACWARE.ADS.PMDME a
        WHERE a.REGDATETIME BETWEEN @start and @end
        GROUP BY a.RFCODE
    ) c on a.CODE_ = c.rfcode
    LEFT OUTER JOIN (
        SELECT
        COUNT(a.CODE) as ships, b.rfcode
        FROM
        (
            SELECT
            A.ACCOUNT AS CODE,
            MIN(CAST(A.BILLDATETIME AS DATE)) AS SHIPDATE

            FROM PACWARE.ADS.ARODME A
            LEFT OUTER JOIN PACWARE.ADS.PTDME B ON A.PTCODE=B.CODE_
            LEFT OUTER JOIN event.dbo.newdate() D ON A.ACCOUNT=D.ACCOUNT
            LEFT OUTER JOIN event.dbo.newdate_extras() D2 ON A.ACCOUNT=D2.ACCOUNT
            WHERE A.BILLDATETIME>=@start
            AND A.BILLDATETIME=@start AND D.NEWDATE=@start AND D2.NEWDATE'ID'
            Group by 
            A.ACCOUNT,
            B.MEDICAREID,
            A.CATEGORY
        ) a
        JOIN PACWARE.ADS.PMDME b on a.CODE = b.CODE_
        GROUP BY b.RFCODE
    ) d on a.CODE_ = d.rfcode
    LEFT OUTER JOIN event.dbo.employee_slscode e on a.SLSCODE = e.slscode
    JOIN event.dbo.employee f on e.employee_id = f.id
    JOIN event.dbo.referral_data g on a.CODE_ = g.CODE_
    JOIN event.dbo.referral_type h on g.referral_type_id = h.id
    WHERE total > 0 

Upvotes: 2

Views: 706

Answers (1)

Luis Quijada
Luis Quijada

Reputation: 2405

I would try creating first and index just for the colum REGDATETIME on PACWARE.ADS.PMDME table.

GO
CREATE NONCLUSTERED INDEX [IX_PMDME_REGDATETIME] ON PACWARE.ADS.PMDME
(
    [REGDATETIME] ASC
)
GO

Test how it works. I would also test adding another index to the column RFCODE (same table) if the selectivity of the column is good enough.

Upvotes: 2

Related Questions