-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determines "popularity" of different kind of adhoc queries like if th…
…ey are already parameterized.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/***************************************** | ||
Проводит анализ на возможную параметризацию кеша планов | ||
1) выбирает все Adhoc планы из кеша | ||
2) пытается их параметризовать и сохраняет параметризованный вариант (если не получается, удаляет) | ||
3) группирует, чтобы определить количество вхождений параметризованного запроса | ||
*******************************************/ | ||
|
||
SELECT st.text INTO query_plan_cache | ||
FROM sys.dm_exec_cached_plans AS cp | ||
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st | ||
WHERE cp.objtype = 'AdHoc' | ||
|
||
DECLARE @txt NVARCHAR(MAX); | ||
DECLARE @par NVARCHAR(MAX); | ||
DECLARE @id INT; | ||
DECLARE @q NVARCHAR(MAX); | ||
DECLARE db_cursor CURSOR FOR | ||
OPEN db_cursor | ||
FETCH NEXT FROM db_cursor INTO @id, @q | ||
|
||
WHILE @@FETCH_STATUS = 0 | ||
BEGIN | ||
BEGIN TRY | ||
EXEC sp_get_query_template @q, @txt OUTPUT, @par OUTPUT | ||
UPDATE query_plan_cache SET [text] = @txt WHERE id = @id | ||
END TRY | ||
BEGIN CATCH | ||
DELETE query_plan_cache WHERE id = @id | ||
END CATCH | ||
FETCH NEXT FROM db_cursor INTO @id, @q | ||
END | ||
|
||
CLOSE db_cursor | ||
DEALLOCATE db_cursor | ||
|
||
SELECT [text], COUNT(*) | ||
FROM query_plan_cache WITH(NOLOCK) | ||
GROUP BY text | ||
ORDER BY COUNT(*) DESC |