This slows down the query from one run to another because when statistics are out of date, its likely the query plan is not optimal for the newly inserted or changed data for the same query (explained more later below). You can call Update Statistics on the entire database or just certain tables or indexes. This helps the query engine to develop a "Plan" of attack for how it will do the query, for example the type of method it will use to match keys between tables using a hash or looking through the entire set. A database stores statistics on the range and distribution of the types of values in various column on tables and indexes. Often when there is a drastic difference from run to run of a query I find that it is often one of 5 issues. If you are still having problems after updating your stats, I would suggest posting both execution plans.Īnd to answer your question - yes, I would say it is highly unusual for your best option to be recompiling the execution plan every time you execute the query. Rebuilding statistics can be an essential part of DBA work depending on your situation. I suspect you just need to update your statistics and your execution plan. In summary - I don't see any reason that OPTION(RECOMPILE) would be a benefit here. This means that if you create the query where there are 10 records in your database and then execute it when there are 100,000,000 records the cached execution plan may no longer be the most effective. NET but if you are using a parameterized query then this ends up being a stored proc call) SQL Server attempts to determine the most effective execution plan for this query based on the data in your database and the parameters passed in ( parameter sniffing), and then caches this plan. When a stored procedure is created (I suspect you are calling ad-hoc sql from. I have never heard that described as creates a new lookup strategy but maybe we are just using different terms for the same thing. This will ensure that when your execution plan is created it will be using the latest information.Īdding OPTION(RECOMPILE) rebuilds the execution plan every time that your query executes. This can be done by running the following: EXEC sp_updatestatsĪnd then recreating your execution plan. Before you explore whether this makes sense in your situation I would recommend rebuilding your statistics. In my experience the only time this is a viable option is when you are using dynamic SQL. There are times that using OPTION(RECOMPILE) makes sense.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |