Skip to content

Commit

Permalink
Fix text collection for Running Queries on SQL 2005
Browse files Browse the repository at this point in the history
SQL 2005 doesn't support table-valued constructors.
Added OPTION(RECOMPILE) hint to avoid polluting the plan cache.
#524
  • Loading branch information
DavidWiseman committed Feb 5, 2023
1 parent ef1067b commit b5fa8b4
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions DBADash/DBCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public class DBCollector
private readonly MemoryCache cache = MemoryCache.Default;
private readonly Stopwatch swatch = new();
private CollectionType currentCollection;
private Version SQLVersion = new();

private bool IsTableValuedConstructorsSupported => SQLVersion.Major > 9;

public int Job_instance_id
{
Expand Down Expand Up @@ -337,6 +340,7 @@ public void GetInstance()
dbName = (string)dt.Rows[0]["DBName"];
instanceName = (string)dt.Rows[0]["Instance"];
productVersion = (string)dt.Rows[0]["ProductVersion"];
Version.TryParse(productVersion, out SQLVersion);
string hostPlatform = (string)dt.Rows[0]["host_platform"];
engineEdition = (DatabaseEngineEdition)Convert.ToInt32(dt.Rows[0]["EngineEdition"]);
string containedAGName = Convert.ToString(dt.Rows[0]["contained_availability_group_name"]);
Expand Down Expand Up @@ -512,7 +516,14 @@ public void Collect(CollectionType collectionType)
}
if (collectionType == CollectionType.RunningQueries)
{
CollectText();
try
{
CollectText();
}
catch (Exception ex)
{
LogError(new Exception("Error collecting text for Running Queries", ex), "RunningQueries");
}
try
{
CollectPlans();
Expand Down Expand Up @@ -768,19 +779,32 @@ public void CacheCollectedText()
private string GetTextFromHandlesSQL()
{
var handles = RunningQueriesHandles();
Int32 cnt = 0;
Int32 cacheCount = 0;
int cnt = 0;
int cacheCount = 0;
var sb = new StringBuilder();
sb.Append(@"DECLARE @handles TABLE(sql_handle VARBINARY(64))
INSERT INTO @handles(sql_handle)
VALUES
");
INSERT INTO @handles(sql_handle)");

if (IsTableValuedConstructorsSupported)
{
sb.Append("\nVALUES");
}

foreach (string strHandle in handles)
{
if (!cache.Contains(strHandle))
{
cnt += 1;
sb.Append(string.Format("(0x{0}),", strHandle));
if (IsTableValuedConstructorsSupported)
{
if (cnt > 0) sb.Append(",");
sb.Append($"(0x{strHandle})");
}
else
{
if (cnt > 0) sb.Append("\nUNION ALL");
sb.Append($"\nSELECT 0x{strHandle}");
}
cnt++;
}
else
{
Expand All @@ -790,7 +814,7 @@ INSERT INTO @handles(sql_handle)

LogInternalPerformanceCounter("DBADash", "Distinct count of text (sql_handle)", "", handles.Count); // Total number of distinct sql_handles
LogInternalPerformanceCounter("DBADash", "Count of text (sql_handle) to collect", "", cnt); // Count of sql_handles we need to collect
LogInternalPerformanceCounter("DBADash", "Count of text (sql_handle) from cache", "", handles.Count - cnt); // Count of sql_handles we didn't need to collect becasue they were collected previously and we cached the sql_handle.
LogInternalPerformanceCounter("DBADash", "Count of text (sql_handle) from cache", "", handles.Count - cnt); // Count of sql_handles we didn't need to collect because they were collected previously and we cached the sql_handle.

if ((cnt + cacheCount) > 0)
{
Expand All @@ -802,15 +826,17 @@ INSERT INTO @handles(sql_handle)
}
else
{
sb.Remove(sb.Length - 1, 1);
sb.AppendLine("OPTION(RECOMPILE)"); // Plan caching is not beneficial. RECOMPILE hint to avoid polluting the plan cache
sb.AppendLine();
sb.AppendLine();
sb.Append(@"SELECT H.sql_handle,
txt.dbid,
txt.objectid as object_id,
txt.encrypted,
txt.text
FROM @handles H
CROSS APPLY sys.dm_exec_sql_text(H.sql_handle) txt");
CROSS APPLY sys.dm_exec_sql_text(H.sql_handle) txt
OPTION(RECOMPILE)"); // Plan caching is not beneficial. RECOMPILE hint to avoid polluting the plan cache
return sb.ToString();
}
}
Expand Down

0 comments on commit b5fa8b4

Please sign in to comment.