简体   繁体   中英

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?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM