Home Forums SQL Server 7,2000 General Prepared vs dynamic SQL odd execution plan RE: Prepared vs dynamic SQL odd execution plan

  • John Rowan (6/11/2008)


    In your first example, the optimizer can use the value you've passed in (1001) and actually look at the distribution of the index to determine if an index seek could be used. When you run the same query and pass it a parameter instead of an explicit value, the Query Optimizer must look at all of the values in the query and create an execution plan based off of the selectivity of all of the index values. In this case, the Optimizer determines that an index scan would be more efficient. I would guess that your value of 1001 is fairly selective with regards to the other values in the index.

    Does that help?

    Thanks for your response...

    Yes, the value of 1001 is the primary key, so the selectivity should be one-to-one. That is part of what confuses me. Regardless of the value of @P29, if it is matching on the PK, why would there be any question on selectivity? Is there a hint, or some other method of getting the optimizer to use a seek?