My VIEW doesnt use Index.

  • It's perfectly expected, a case statement is not a SARGable expression, so no index seek possible.

    CREATE VIEW MyView

    AS

    SELECT

    CASE WHEN (Col2 > 0) THEN Col1 END AS Col1,

    Col2

    FROM MyTable

    The query

    SELECT * FROM MyView WHERE Col1 = 123

    Early in parsing, the name of the view is replaced by the defintion, so

    SELECT * FROM (

    SELECT

    CASE WHEN (Col2 > 0) THEN Col1 END AS Col1,

    Col2

    FROM MyTable) MyView

    WHERE Col1 = 123

    This simplifies to

    SELECT

    CASE WHEN (Col2 > 0) THEN Col1 END AS Col1,

    Col2

    FROM MyTable

    WHERE CASE WHEN (Col2 > 0) THEN Col1 END = 123

    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

Viewing post 1 (of 2 total)

You must be logged in to reply to this topic. Login to reply