Why does a parameterized query produces vastly slower query plan vs non-parameterized query

后端 未结 4 1432
萌比男神i
萌比男神i 2020-11-30 05:53

In a SQL Server 2005 database I\'m working on this query:

select *
from foo
join bar on bar.x = foo.x
join baz on baz.y = foo.

4条回答
  •  萌比男神i
    2020-11-30 06:42

    Your starting point should be the SQL profiler. Run both through the profiler, and see what the query plan is in each case... then update the question to describe the two plans.

    One thing which I believe can be a problem is that if you have a parameterised query with one set of values, the optimiser may look at some of the stats/indexes and pick one way of doing it, then reuse that plan for all queries - despite it not being particularly appropriate for a different set of values. Likewise if the plan is determined when there's one set of data (e.g. when one table is tiny, encouraging a table scan) and then you add bucket-loads of data, the plan may not be appropriate. Neither of these would affect a query which was bad as the very first query for the prepared statement though.

提交回复
热议问题