Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Poor query optimizing

Author  Topic 

jdblack
Starting Member

11 Posts

Posted - 2007-11-04 : 08:20:54
Hi there,

I've got a SQL 2005 table(DataTable) with 5.5 million rows and a clustered index on an integer column. The column is a foreign key pointing to another table (LookupTable) that associates the integer with an nvarchar(50) column. Both integer and nvarchar(50) columns are in a clustered index.

I'm running equivalent select statements with slightly different where clauses, and am not seeing the performance similarities I'd expect.

select * from DataTable join LookupTable on DataTable.Integer = LookupTable.Integer

1. Setting the where clause to the nvarchar(50) value on the LookupTable takes 64 seconds.
2. Setting the where clause to Integer = (select integer from LookupTable where nvarchar(50) = 'mysearchstring') takes 61 seconds.
3. Setting the where clause to Integer = 526 takes 1 second.

Is this poor design in SQL, or is it a misconfiguration? I would have expected SQL to modify my query to essentially be #3 in all cases before touching the DataTable, which would cause all three versions to take 1 second.

Thanks,
Jason

evilDBA
Posting Yak Master

155 Posts

Posted - 2007-11-04 : 10:35:18
It means that when SQL server knows the exact value, it generates much more efficient execution plan.
Compare execution plans for:

select ... WHERE IntColumn = 526

with

declare @n int
set @n = 456
select ... WHERE intCOlumn = @n

Possible cause: irregular selectivity values
http://www.sqlsolutions.com/articles/articles/How_Values_with_Irregular_Selectivity_Impact_SQL_Server_Database_Performance.htm
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-11-04 : 13:13:52
"The column is a foreign key pointing to another table (LookupTable) that [b]associates the integer with an nvarchar(50) column. Both integer and nvarchar(50) columns are in a clustered index."

Some DDL for the two tables would help. Otherwise I'm just guessing at a number of possible thing.

Change the names of the tables/columns if there is some confidentiality issue, otherwise don't because IME something may well get lost in translation

Kristen
Go to Top of Page
   

- Advertisement -