What is query execution doing to this query in SQL Server 2005?

前端 未结 1 436
太阳男子
太阳男子 2021-01-27 16:37

Today I found this query in our code that pulls up a list of errors from our database:

SELECT *
FROM   (
        SELECT Substring(title, 9, Patindex(\'%)%\', tit         


        
相关标签:
1条回答
  • 2021-01-27 16:51

    Your code is making invalid assumptions. In a declarative set oriented language like SQL the execution is free to choose whatever execution plan it sees fit. What you see as inefficient is most likely a valid optimization where the title is projected first from an index that satisfies the predicates on lastmodified or something similar. You cannot make any assumption about order of execution, and you are therefore not allowed to have in the projection list expressions like SUBSTRING (..,9,..) which will bomb on certain rows.

    Another example of problems that arise from a similar invalid assumption are SQL Server boolean operator short-circuit evaluation bugs.

    0 讨论(0)
提交回复
热议问题