A DBA should be able to review every statement SQLInsight sends to a monitored server before granting it access. "SQL Scripts/<rule-id>.sql" holds, per rule (82 built-in, 16 community), a Danish header (what and why, how C# judges the data, thresholds with config keys, severity, exact GRANTs, versions, collector file and member) and the collector's SQL verbatim, every version variant as its own batch. "_collectors/<Collector>.sql" holds each collector's queries once. The only statement that is not strictly read-only is documented in the scripts: the deadlock collector's session temp table (#system_health). Inline SQL in the collectors moved to internal constants and Build*Sql methods (no text changed), so SqlScriptTests can check that the scripts still contain the SQL the code sends, that every rule has a script, that every collector query is registered, and that nothing writes. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
103 lines
6.6 KiB
Transact-SQL
103 lines
6.6 KiB
Transact-SQL
-- ===================================================================================================================
|
|
-- SQLI-TEMP-003: Session bruger meget tempdb i lang tid
|
|
-- ===================================================================================================================
|
|
-- Regel-id: SQLI-TEMP-003
|
|
-- Titel: Session bruger meget tempdb i lang tid
|
|
-- Alvor: Medium
|
|
-- Versioner: SQL Server 2005 og nyere.
|
|
--
|
|
-- Hvad tjekkes, og hvorfor:
|
|
-- Om en session har brugt meget tempdb i lang tid.
|
|
--
|
|
-- Vurdering (sker i C# ud fra de indsamlede data, ikke i SQL'en, medmindre andet er nævnt):
|
|
-- SQL'en finder de 5 brugersessioner, der bruger mest tempdb (bruger- + interne objekter, allokeret - deallokeret,
|
|
-- fra session- og task-space-usage). C# husker, hvornår hver session (session_id + login_time) første gang var over
|
|
-- grænsen; fund når den har været over i mindst 15 minutter.
|
|
--
|
|
-- Grænseværdier (konfigurationsnøgle i agentens appsettings.json og standardværdi):
|
|
-- SQLInsight:Tempdb:SessionMegabytes, standard 1024 (MB); 15 minutter (TempdbAnalysis.HeavySessionMinutes, fast i
|
|
-- koden).
|
|
--
|
|
-- Rettigheder til agentens login (se db/permissions/grant-least-privilege.sql; [agent] = agentens login):
|
|
-- GRANT VIEW SERVER STATE TO [agent]; -- SQL Server 2005-2019
|
|
-- GRANT VIEW SERVER PERFORMANCE STATE TO [agent]; -- SQL Server 2022+ (erstatter VIEW SERVER STATE)
|
|
-- GRANT VIEW SERVER SECURITY STATE TO [agent]; -- SQL Server 2022+
|
|
--
|
|
-- Data kommer fra (collector, fil : medlemmer, interval):
|
|
-- TempdbCollector
|
|
-- Fil: src/SQLInsight.Collectors/Instance/TempdbCollector.cs
|
|
-- Medlemmer: TempdbCollector.SpaceSql, TempdbCollector.SessionsSql, TempdbCollector.OldestSnapshotSql,
|
|
-- TempdbCollector.GeneratorsSql
|
|
-- Interval: 5 min som standard (SQLInsight:CollectorIntervals:tempdb)
|
|
-- Delt script: SQL Scripts/_collectors/TempdbCollector.sql
|
|
-- Regelkode: src/SQLInsight.Rules/Instance/TempdbRules.cs : TempdbSessionRule.Evaluate
|
|
--
|
|
-- Read-only: Ja: kun SELECT samt SET-sessionsindstillinger. Intet ændrer data, indstillinger eller objekter.
|
|
-- Kørsel: Kør i SSMS som agentens login (eller et login med samme rettigheder). Hver variant er sin egen batch
|
|
-- (GO); en variant for en anden SQL Server-version end din kan fejle - det er forventet.
|
|
-- Per-database-forespørgsler er vist for databasen msdb, som findes overalt: ret navnet til den
|
|
-- database, du vil se.
|
|
-- ===================================================================================================================
|
|
|
|
-- -------------------------------------------------------------------------------------------------------------------
|
|
-- Forbindelsen: Sessionsindstillinger, agenten sætter på hver forbindelse
|
|
-- -------------------------------------------------------------------------------------------------------------------
|
|
|
|
-- Variant: Alle versioner: sættes på hver forbindelse, agenten åbner
|
|
-- Kilde: SqlConnectionFactory.SessionSettings
|
|
-- Kode: src/SQLInsight.Connectivity/SqlConnectionFactory.cs : SqlConnectionFactory.SessionSettings
|
|
SET LOCK_TIMEOUT 5000; SET DEADLOCK_PRIORITY LOW; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
|
|
GO
|
|
|
|
-- -------------------------------------------------------------------------------------------------------------------
|
|
-- TempdbCollector: tempdb: fri plads, version store, bruger- og interne objekter, de tungeste sessioner, ældste
|
|
-- snapshot-transaktion og hvilke databaser der laver rækkeversioner.
|
|
-- -------------------------------------------------------------------------------------------------------------------
|
|
|
|
-- Variant: SQL Server 2005 og nyere
|
|
-- Kilde: TempdbCollector.SpaceSql
|
|
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.SpaceSql
|
|
SELECT SUM(CAST(u.unallocated_extent_page_count AS bigint)), SUM(CAST(u.version_store_reserved_page_count AS bigint)),
|
|
SUM(CAST(u.user_object_reserved_page_count AS bigint)), SUM(CAST(u.internal_object_reserved_page_count AS bigint)),
|
|
(SELECT SUM(CAST(f.size AS bigint)) FROM tempdb.sys.database_files f WHERE f.type = 0),
|
|
(SELECT MAX(CASE WHEN f.growth > 0 AND (f.max_size = -1 OR f.size < f.max_size) THEN 1 ELSE 0 END) FROM tempdb.sys.database_files f WHERE f.type = 0),
|
|
(SELECT MAX(CAST(p.cntr_value AS bigint)) FROM sys.dm_os_performance_counters p
|
|
WHERE p.counter_name = N'Version Store Size (KB)' AND p.object_name LIKE N'%Transactions%')
|
|
FROM tempdb.sys.dm_db_file_space_usage u
|
|
GO
|
|
|
|
-- Variant: SQL Server 2005 og nyere
|
|
-- Kilde: TempdbCollector.SessionsSql
|
|
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.SessionsSql
|
|
SELECT TOP 5 u.session_id, DATEADD(minute, DATEDIFF(minute, GETDATE(), GETUTCDATE()), s.login_time), u.pages, s.login_name, s.program_name, s.host_name, s.status
|
|
FROM (SELECT a.session_id, SUM(a.uo_alloc - a.uo_dealloc + a.io_alloc - a.io_dealloc) AS pages
|
|
FROM (SELECT session_id, CAST(user_objects_alloc_page_count AS bigint) AS uo_alloc, CAST(user_objects_dealloc_page_count AS bigint) AS uo_dealloc,
|
|
CAST(internal_objects_alloc_page_count AS bigint) AS io_alloc, CAST(internal_objects_dealloc_page_count AS bigint) AS io_dealloc
|
|
FROM sys.dm_db_session_space_usage
|
|
UNION ALL
|
|
SELECT session_id, CAST(user_objects_alloc_page_count AS bigint), CAST(user_objects_dealloc_page_count AS bigint),
|
|
CAST(internal_objects_alloc_page_count AS bigint), CAST(internal_objects_dealloc_page_count AS bigint)
|
|
FROM sys.dm_db_task_space_usage) a
|
|
GROUP BY a.session_id) u
|
|
JOIN sys.dm_exec_sessions s ON s.session_id = u.session_id
|
|
WHERE s.is_user_process = 1 AND u.pages > 0
|
|
ORDER BY u.pages DESC
|
|
GO
|
|
|
|
-- Variant: SQL Server 2005 og nyere
|
|
-- Kilde: TempdbCollector.OldestSnapshotSql
|
|
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.OldestSnapshotSql
|
|
SELECT TOP 1 t.session_id, t.elapsed_time_seconds, s.login_name, s.program_name
|
|
FROM sys.dm_tran_active_snapshot_database_transactions t
|
|
LEFT JOIN sys.dm_exec_sessions s ON s.session_id = t.session_id
|
|
ORDER BY t.elapsed_time_seconds DESC
|
|
GO
|
|
|
|
-- Variant: SQL Server 2005 og nyere, kun når version store er over halvdelen af grænsen, højst én gang i timen
|
|
-- Kilde: TempdbCollector.GeneratorsSql
|
|
-- Kode: src/SQLInsight.Collectors/Instance/TempdbCollector.cs : TempdbCollector.GeneratorsSql
|
|
SELECT TOP 10 DB_NAME(database_id), SUM(CAST(aggregated_record_length_in_bytes AS bigint))
|
|
FROM sys.dm_tran_top_version_generators
|
|
GROUP BY database_id
|
|
ORDER BY SUM(CAST(aggregated_record_length_in_bytes AS bigint)) DESC
|
|
GO
|