Nauwkeurigheid van statistieken voor een toegewezen SQL-pool controleren
Van toepassing op: Azure Synapse Analytics
Actuele statistieken zijn essentieel voor het genereren van een optimaal uitvoeringsplan. Er zijn twee verschillende perspectieven die u moet evalueren voor het bepalen van de nauwkeurigheid van statistieken:
Stap 1: controleer de nauwkeurigheid van het aantal rijen van besturingsknooppunten
In de toegewezen SQL-pool moet de primaire engine voor het maken van gedistribueerde queryplannen worden bijgewerkt over het aantal rijen op de rekenknooppunten. Voer de volgende query uit om tabellen te identificeren die verschillen hebben tussen het aantal rijen:
SELECT objIdsWithStats.[object_id]
,actualRowCounts.[schema]
,actualRowCounts.logical_table_name
,statsRowCounts.stats_row_count
,actualRowCounts.actual_row_count
,row_count_difference = CASE
WHEN actualRowCounts.actual_row_count >= statsRowCounts.stats_row_count
THEN actualRowCounts.actual_row_count - statsRowCounts.stats_row_count
ELSE statsRowCounts.stats_row_count - actualRowCounts.actual_row_count
END
,percent_deviation_from_actual = CASE
WHEN actualRowCounts.actual_row_count = 0
THEN statsRowCounts.stats_row_count
WHEN statsRowCounts.stats_row_count = 0
THEN actualRowCounts.actual_row_count
WHEN actualRowCounts.actual_row_count >= statsRowCounts.stats_row_count
THEN CONVERT(NUMERIC(18, 0), CONVERT(NUMERIC(18, 2), (actualRowCounts.actual_row_count - statsRowCounts.stats_row_count)) / CONVERT(NUMERIC(18, 2), actualRowCounts.actual_row_count) * 100)
ELSE CONVERT(NUMERIC(18, 0), CONVERT(NUMERIC(18, 2), (statsRowCounts.stats_row_count - actualRowCounts.actual_row_count)) / CONVERT(NUMERIC(18, 2), actualRowCounts.actual_row_count) * 100)
END
,'UPDATE STATISTICS ' + quotename(actualRowCounts.[schema]) + '.' + quotename(actualRowCounts.logical_table_name) + ';' as update_stats_stmt
FROM (
SELECT DISTINCT object_id
FROM sys.stats
WHERE stats_id > 1
) objIdsWithStats
LEFT JOIN (
SELECT object_id
,sum(rows) AS stats_row_count
FROM sys.partitions
GROUP BY object_id
) statsRowCounts ON objIdsWithStats.object_id = statsRowCounts.object_id
LEFT JOIN (
SELECT sm.name [schema]
,tb.name logical_table_name
,tb.object_id object_id
,SUM(rg.row_count) actual_row_count
FROM sys.schemas sm
INNER JOIN sys.tables tb ON sm.schema_id = tb.schema_id
INNER JOIN sys.pdw_table_mappings mp ON tb.object_id = mp.object_id
INNER JOIN sys.pdw_nodes_tables nt ON nt.name = mp.physical_name
INNER JOIN sys.dm_pdw_nodes_db_partition_stats rg ON rg.object_id = nt.object_id
AND rg.pdw_node_id = nt.pdw_node_id
AND rg.distribution_id = nt.distribution_id
INNER JOIN sys.indexes ind on tb.object_id = ind.object_id
WHERE rg.index_id < 2 -- In case this condition removed the number of rows will gets duplicated based on the number of index.
AND ind.type_desc IN ('CLUSTERED COLUMNSTORE', 'HEAP') -- Switch between the CCI (Column store) and HEAP, You should at least keep one value or else the total number of rows will gets duplicated based on the number of indexes.
GROUP BY sm.name
,tb.name
,tb.object_id
) actualRowCounts ON objIdsWithStats.object_id = actualRowCounts.object_id
Stap 2: zorg dat de statistieken up-to-date zijn
Het bijwerken van gegevens kan van invloed zijn op de statistieken histogrammen die worden gebruikt om effectieve uitvoeringsplannen te genereren. Voer de volgende query uit om te bepalen of de laatste bijwerkingsdatum van uw statistieken overeenkomt met de wijzigingspatronen van de tabel:
SELECT ob.[object_id],max(sm.[name]) AS [schema_name]
,max(tb.[name]) AS [table_name]
,st.[stats_id]
,max(st.[name]) AS [stats_name]
,string_agg(co.[name], ',') AS [stats_column_names]
,STATS_DATE(ob.[object_id], st.[stats_id]) AS [stats_last_updated_date]
,'UPDATE STATISTICS ' + quotename(max(sm.[name])) + '.' + quotename(max(tb.[name])) + ';' as [update_stats_stmt]
FROM sys.objects ob
JOIN sys.stats st ON ob.[object_id] = st.[object_id]
JOIN sys.stats_columns sc ON st.[stats_id] = sc.[stats_id]
AND st.[object_id] = sc.[object_id]
JOIN sys.columns co ON sc.[column_id] = co.[column_id]
AND sc.[object_id] = co.[object_id]
JOIN sys.types ty ON co.[user_type_id] = ty.[user_type_id]
JOIN sys.tables tb ON co.[object_id] = tb.[object_id]
JOIN sys.schemas sm ON tb.[schema_id] = sm.[schema_id]
WHERE st.[stats_id] > 1
GROUP BY ob.[object_id], st.[stats_id]
ORDER BY stats_last_updated_date
Stap 3: werk de statistieken voor geïdentificeerde tabellen bij
Nadat u de kandidaattabellen in de vorige stappen hebt geïdentificeerd, voert u de instructie(s) uit die zijn gegenereerd in de update_stats_stmt
kolom van de query's om de statistieken bij te werken.
Notitie
Het is niet raadzaam om afzonderlijke statistieken bij te werken, zelfs niet wanneer de gebruiker is gemaakt. Door UPDATE STATISTICS
uit te voeren zonder een statistieknaam op te geven, worden alle statistieken die zijn gekoppeld aan de tabel, evenals het aantal besturingsknooppuntrijen, bijgewerkt. U kunt overwegen om het standaardscanpercentage te overschrijven door WITH FULLSCAN
de juiste nauwkeurigheid voor grote tabellen te gebruiken of WITH SAMPLE <SamplePercent> PERCENT
te bereiken. Zie Statistieken bijwerken (Transact-SQL) voor de volledige syntaxis.
Bijvoorbeeld:
UPDATE STATISTICS [dbo].[MyAwesomeTable];
Nadat de statistieken zijn bijgewerkt, voert u de probleemquery opnieuw uit om te bepalen of het bijwerken van de statistieken de uitvoeringsduur heeft verbeterd.