![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I was building a table-valued function that included this in the WHERE clause: * * *AND tab.SOME_COLUMN = '0148' I wanted to drive the query with a parameter and changed the WHERE statement to this: * * *AND tab.SOME_COLUMN = @SOME_VARIABLE_CD Simply substituting the variable for the literal caused the query to run _four times slower_. I checked my DECLARE statement, and found this: DECLARE @SOME_VARIABLE_CD nvarchar(4) The database column tab.SOME_COLUMN is ASCII (varchar) not Unicode (nvarchar). I changed my variable declaration to ASCII data type: * * *DECLARE @SOME_VARIABLE_CD varchar(4) The query zipped back to its original speed I suspect the performance hit when comparing varchar to nvarchar is related to implicit conversions. However, my suspicion does not seem to be borne out by the query plan. I have not had a chance to test with a Unicode database. Does anyone know why the simple DECLARE change *would have a 4x impact on query speed? Regardless, I am going to make sure that the ASCII/UNICODE nature of my variables matches my columns. Thanks, Bill |
#3
| |||
| |||
|
--Clustered Index Scan(OBJECT [Testing].[dbo].[Foo].[PK__Foo__98D78B4403439144]),WHERE CONVERT_IMPLICIT(nvarchar(30),[Testing].[dbo].[Foo].[datacol],0)=[@search]))
|
#4
| |||
| |||
|
#5
| |||
| |||
|
--Clustered Index Seek(OBJECT [TICDW].[dbo].[Foo].[PK__Foo__98D78B442690946A]), SEEK [TICDW].[dbo].[Foo].[keycol]
|
--Clustered Index Seek(OBJECT [TICDW].[dbo].[Foo].[PK__Foo__98D78B442690946A]), SEEK [TICDW].[dbo].[Foo].[keycol]=
|
#6
| |||
| |||
|
|
Here's something interesting: The engine appears to use the index, even if it has to do an implicit conversion. So it isn't doing a scan, but that conversion seems to be pretty expensive. |
![]() |
| Thread Tools | |
| Display Modes | |
| |