Why is some sql query much slower when used with SqlCommand?

Another thing that can be important is the SET options that are enabled. Some of these options change the query plan sufficiently to change the profile. Some can have a huge impact if you are looking at (for example) a calculated + persisted (and possibly indexed) column: if the SET options aren’t compatible, it can be forced to re-calculate the values, rather than using the indexed value – which can change an index seek into a table scan + calculation.

Try using the profiler to see what SET options are “in play”, and see if using those options changes things.

Another impact is the connection string; for example, if you enable MARS that can change the behaviour in subtle ways.

Finally, transactions (implicit (TransactionScope) or explicit) can have a huge impact, depending on the isolation level.

Leave a Comment