CHARINDEX vs LIKE search gives very different performance, why?

前端 未结 2 1751
日久生厌
日久生厌 2020-12-31 06:19

We use Entity Frameworks for DB access and when we \"think\" LIKE statement - it actually generates CHARINDEX stuff. So, here is 2 simple queries, after I simplified them to

2条回答
  •  感动是毒
    2020-12-31 06:44

    I will answer my own question since it was hard to find correct answer and I was pointed to the problem by SQL Server 2012 Execution Plan output. As you see in original question - everything looks OK on surface. This is SQL Server 2008.

    When I run same query on 2012 I got warning on CHARINDEX query. Problem is - SQL Server had to do type conversion. Address1 is VarChar and query has N'1124' which is Unicode or NVarChar. If I change this query as so:

    SELECT * 
    FROM LOCAddress 
    WHERE (CAST(CHARINDEX(LOWER('1124'), LOWER([Address1])) AS int)) 
    

    It then runs same as LIKE query. So, type conversion that was caused by Entity Framework generator was causing this horrible hit in performance.

提交回复
热议问题