SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


NC index seek vs NC index scan with implicit conversion


NC index seek vs NC index scan with implicit conversion

Author
Message
jhager
jhager
SSC Veteran
SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)

Group: General Forum Members
Points: 212 Visits: 331
Hi all,

I've been looking thru our production plan cache and have found a number of implicit conversions. Most are of similar "type", such as NVARCHAR(4) --> VARCHAR(15). I built a small test script to see if there is any significant impact of the conversion: I discovered when there is no conversion, I get a NC index seek; when there is conversion, I get a NC index scan. But the cost, estimated vs. actual rows, etc. is the same (perhaps my sample size is too smal, or not "diverse" enough?)

Any info is appreciated.

SCRIPT:

IF OBJECT_ID(N'JSHTest', N'U') IS NOT NULL
DROP TABLE JSHTest;
GO

CREATE TABLE JSHTest
(
Col01 INT NOT NULL
,Col02 VARCHAR(15)
);

INSERT INTO JSHTest
VALUES
( 1, 'ABC' ),
( 2, 'DEF' ),
( 3, 'GHI' ),
( 4, 'JKL' ),
( 5, 'MNO' ),
( 6, 'PQR' ),
( 7, 'STU' ),
( 8, 'GHI' ),
( 9, 'YZ' ),
( 10, 'GHI' );

CREATE NONCLUSTERED INDEX JSHTest_NCIX01 ON JSHTest
(Col02);
ALTER TABLE JSHTest
ADD CONSTRAINT JSHTest_PK PRIMARY KEY (Col01);

UPDATE STATISTICS JSHTest
WITH FULLSCAN;

DECLARE @VarNVC NVARCHAR(4) = N'GHI';
DECLARE @VarVC VARCHAR(15) = 'GHI';

SELECT
*
FROM
JSHTest
WHERE
Col02 = @VarNVC;

SELECT
*
FROM
JSHTest
WHERE
Col02 = @VarVC;
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89287 Visits: 45284
Your sample set is way too small. Everything's fast on 10 rows.

Estimated and actual rows will be the same for the seek and scan, because it's the number of rows returned by the operator, not the number of rows it read.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jhager
jhager
SSC Veteran
SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)

Group: General Forum Members
Points: 212 Visits: 331
Thanks, I'll cook up a test on a much larger scale. I was hoping the "quick" test would show not only the operator difference but some indicator why the difference.

Thanks Gail.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89287 Visits: 45284
The why is pretty simple. Any function when used on a column makes the predicate non-SARGable, or not usable as a seek predicate. Implicit conversions are same as explicit conversions, same as any other function on the column.

SELECT
*
FROM
JSHTest
WHERE
Col02 = '' + @VarNVC;

SELECT
*
FROM
JSHTest
WHERE
Col02 = UPPER(@VarNVC);



Same thing in both of those cases

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search