1

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?

2
  • Assuming you have an index on the UniqueCol field, how is the @storedProcParam defined? Is it defined as a parameter to a stored procedure or a local variable in the stored procedure. I've seen better performance on query cache of the execution plan when using a local variable vs. a passed in variable. Not much else to go by though... Commented Mar 25, 2015 at 23:26
  • Does the query run better with a hard coded value vs. the parameter? Commented Mar 26, 2015 at 0:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I checked the actual execution plan and I see exactly this issue. I didn't realize one of the tables was nvarchar(64), thanks!
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.