30.06.2013 Views

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Controlling <strong>Execution</strong> <strong>Plans</strong> with Hints<br />

While this will eliminate errors, it will almost certainly result in longer query times since<br />

the optimizer won't be able to choose the optimal plan over the "robust" plan.<br />

You should only ever use this hint if you have a set of wide rows that cause this rare<br />

error condition.<br />

KEEP PLAN<br />

As the data in a table changes, gets inserted, or deleted, the statistics describing the<br />

data also change. Once the volume of statistics changed passes a certain threshold, any<br />

queries referencing those statistics are marked for recompile in the plan cache, on the<br />

assumption that the optimizer might, based on the new statistics, be able to generate a<br />

more efficient plan.<br />

Setting the KEEP PLAN hint doesn't prevent recompiles, but it does cause the optimizer to<br />

use less stringent rules when determining the need for a recompile. This means that, with<br />

more volatile data, you can keep recompiles to a minimum. The hint causes the optimizer<br />

to treat temporary tables within the plan in the same way as permanent tables, reducing<br />

the number of recompiles caused by the temporary table. This reduces the time and cost<br />

of recompiling a plan, which can be quite large, depending on the query.<br />

However, problems may arise because the old plans may not be as efficient as newer plans<br />

could be. This happens because of the changing data; a plan that was previously adequate<br />

for a given data set is no longer appropriate because of the changes to that data set.<br />

209

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!