ashishduh
ashishduh

Reputation: 6699

SQL Server Join between two unique columns poor performance

I have the following query that is taking a long time to execute.

SELECT
    Table1.A, Table1.B, Table2.C
FROM
    Table1
    INNER JOIN Table2
        ON Table1.UniqueCol = Table2.UniqueCol
WHERE
    Table1.UniqueCol = @storedProcParam

Both tables have a unique nonclustered index on UniqueCol which is a varchar(64) but neither has it as its primary key. One of the tables has a few million records and the other has a few thousand. Is it the fact that the column is a varchar(64) that is holding back performance, or is there something else I'm missing?

Upvotes: 1

Views: 52

Answers (2)

Brennan Pope
Brennan Pope

Reputation: 1083

The values in your select clause should be included in your non-clustered index. This would allow the engine to use the index exclusively instead of having to go to your clustered index. Have you rebuilt your clustered and/or non-clustered indexes recently? Do you know if your indexes are highly fragmented? Can you please post your actual execution plan?

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453348

Apart from some external issue such as blocking the only reason that springs to mind would be if the parameter was of a higher precedence datatype that causes an implicit cast and prevents a simple index seek on the two tables (using the implied predicate on table2) and a cross join of the 0 or 1 row result that both sides return.

You say the columns are varchar. You would get this scan instead of seek if the parameter was nvarchar under some collations.

Upvotes: 1

Related Questions