Skip to main content
4 of 5
formatting, spelling, grammar, tagging
Andriy M
  • 23.2k
  • 6
  • 59
  • 103

Sql server changes execution plan

I have SQL code that is executing slow

enter image description here

DECLARE @ccode varchar(500)
SET @ccode = '%NIP%'


SELECT  [CVN_UID]
  FROM [Klasje].[dbo].[VERSION_CATEGORY_XREFS]
 WHERE DESCRIPTOR_TEXT LIKE @ccode

Why is SQL Server using nested loops?

This code is working fine, it uses only index scan:

SELECT  [CVN_UID]
  FROM [Klasje].[dbo].[VERSION_CATEGORY_XREFS]
 WHERE DESCRIPTOR_TEXT LIKE '%NIP%'

The slow query is executing 5.5 seconds, without the parameter it's 1.8 seconds.

This query is also working fine:

SELECT  [CVN_UID]
  FROM [Klasje].[dbo].[VERSION_CATEGORY_XREFS]
 WHERE DESCRIPTOR_TEXT LIKE @ccode
 OPTION (recompile)
Matej
  • 107
  • 6