-
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.
- Loading branch information
Showing
15 changed files
with
463 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,97 @@ | ||
/***************************************** | ||
Размер и использование файлов текущей БД | ||
*******************************************/ | ||
set nocount on | ||
create table #Data( | ||
FileID int NOT NULL, | ||
[FileGroupId] int NOT NULL, | ||
TotalExtents int NOT NULL, | ||
UsedExtents int NOT NULL, | ||
[FileName] sysname NOT NULL, | ||
[FilePath] nvarchar(MAX) NOT NULL, | ||
[FileGroup] varchar(MAX) NULL) | ||
|
||
create table #Results( | ||
db sysname NULL , | ||
FileType varchar(4) NOT NULL, | ||
[FileGroup] sysname not null, | ||
[FileName] sysname NOT NULL, | ||
TotalMB numeric(18,2) NOT NULL, | ||
UsedMB numeric(18,2) NOT NULL, | ||
PctUsed numeric(18,2) NULL, | ||
FilePath nvarchar(MAX) NULL, | ||
FileID int null) | ||
|
||
create table #Log( | ||
db sysname NOT NULL, | ||
LogSize numeric(18,5) NOT NULL, | ||
LogUsed numeric(18,5) NOT NULL, | ||
Status int NOT NULL, | ||
[FilePath] nvarchar(MAX) NULL) | ||
|
||
INSERT #Data (FileID, [FileGroupId], TotalExtents, UsedExtents, [FileName], [FilePath]) | ||
EXEC ('DBCC showfilestats WITH NO_INFOMSGS') | ||
|
||
update #Data | ||
set #Data.FileGroup = sysfilegroups.groupname | ||
from #Data, sysfilegroups | ||
where #Data.FileGroupId = sysfilegroups.groupid | ||
|
||
INSERT INTO #Results (db, [FileGroup], FileType, [FileName], TotalMB, UsedMB, PctUsed, FilePath, FileID) | ||
SELECT DB_NAME() db, | ||
[FileGroup], | ||
'Data' FileType, | ||
[FileName], | ||
TotalExtents * 64./1024. TotalMB, | ||
UsedExtents *64./1024 UsedMB, | ||
UsedExtents*100. /TotalExtents UsedPct, | ||
[FilePath], | ||
FileID | ||
FROM #Data | ||
order BY --1,2 | ||
DB_NAME(), [FileGroup] | ||
|
||
insert #Log (db,LogSize,LogUsed,Status) | ||
exec('dbcc sqlperf(logspace) WITH NO_INFOMSGS ') | ||
|
||
insert #Results(db, [FileGroup], FileType, [FileName], TotalMB,UsedMB, PctUsed, FilePath, FileID) | ||
select DB_NAME() db, | ||
'Log' [FileGroup], | ||
'Log' FileType, | ||
s.[name] [FileName], | ||
s.Size/128. as LogSize , | ||
FILEPROPERTY(s.name,'spaceused')/8.00 /16.00 As LogUsedSpace, | ||
((FILEPROPERTY(s.name,'spaceused')/8.00 /16.00)*100)/(s.Size/128.) UsedPct, | ||
s.FileName FilePath, | ||
s.FileID FileID | ||
from #Log l , master.dbo.sysaltfiles f , dbo.sysfiles s | ||
where f.dbid = DB_ID() | ||
and (s.status & 0x40) <> 0 | ||
and s.FileID = f.FileID | ||
and l.db = DB_NAME() | ||
|
||
SELECT r.db AS "Database", | ||
r.FileType AS "File type", | ||
CASE | ||
WHEN r.FileGroup = 'Log' Then 'N/A' | ||
ELSE r.FileGroup | ||
END "File group", | ||
r.FileName AS "Logical file name", | ||
r.TotalMB AS "Total size (MB)", | ||
r.UsedMB AS "Used (MB)", | ||
r.PctUsed AS "Used (%)", | ||
r.FilePath AS "File name", | ||
r.FileID AS "File ID", | ||
CASE WHEN s.maxsize = -1 THEN null | ||
ELSE CONVERT(decimal(18,2), s.maxsize /128.) | ||
END "Max. size (MB)", | ||
CONVERT(decimal(18,2), s.growth /128.) "Autogrowth increment (MB)" | ||
FROM #Results r | ||
INNER JOIN dbo.sysfiles s | ||
ON r.FileID = s.FileID | ||
ORDER BY 1,2,3,4,5 | ||
|
||
DROP TABLE #Data | ||
DROP TABLE #Results | ||
DROP TABLE #Log | ||
GO |
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,59 @@ | ||
/***************************************** | ||
Размер таблиц и индексов в мегабайтах | ||
*******************************************/ | ||
|
||
SELECT | ||
t.NAME AS TableName, | ||
i.name as indexName, | ||
sum(p.rows) as RowCounts, | ||
sum(a.total_pages) as TotalPages, | ||
sum(a.used_pages) as UsedPages, | ||
sum(a.data_pages) as DataPages, | ||
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, | ||
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, | ||
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB | ||
FROM | ||
sys.tables t | ||
INNER JOIN | ||
sys.indexes i ON t.OBJECT_ID = i.object_id | ||
INNER JOIN | ||
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id | ||
INNER JOIN | ||
sys.allocation_units a ON p.partition_id = a.container_id | ||
WHERE | ||
t.NAME NOT LIKE 'dt%' AND | ||
i.OBJECT_ID > 255 | ||
--AND i.index_id <= 1 | ||
GROUP BY | ||
t.NAME, i.object_id, i.index_id, i.name | ||
ORDER BY | ||
TotalSpaceMB DESC | ||
GO | ||
|
||
-- та же инфа, но по таблицам, без разбиения на индексы | ||
SELECT | ||
t.NAME AS TableName, | ||
sum(p.rows) as RowCounts, | ||
sum(a.total_pages) as TotalPages, | ||
sum(a.used_pages) as UsedPages, | ||
sum(a.data_pages) as DataPages, | ||
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, | ||
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, | ||
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB | ||
FROM | ||
sys.tables t | ||
INNER JOIN | ||
sys.indexes i ON t.OBJECT_ID = i.object_id | ||
INNER JOIN | ||
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id | ||
INNER JOIN | ||
sys.allocation_units a ON p.partition_id = a.container_id | ||
WHERE | ||
t.NAME NOT LIKE 'dt%' AND | ||
i.OBJECT_ID > 255 | ||
--AND i.index_id <= 1 | ||
GROUP BY | ||
t.NAME | ||
ORDER BY | ||
TotalSpaceMB DESC | ||
GO |
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,20 @@ | ||
/***************************************** | ||
Ввод-вывод в файлы баз | ||
*******************************************/ | ||
SELECT DB_NAME(database_id) AS [Database Name] , | ||
file_id AS [File ID], | ||
io_stall_read_ms AS [Total Read Waits (ms)], | ||
num_of_reads AS [Number of Reads], | ||
CAST(io_stall_read_ms / ( 1.0 + num_of_reads ) AS NUMERIC(10, 1)) | ||
AS [Average Read Wait (ms)] , | ||
io_stall_write_ms AS [Total Write Waits (ms)], | ||
num_of_writes AS [Number of Writes], | ||
CAST(io_stall_write_ms / ( 1.0 + num_of_writes ) AS NUMERIC(10, 1)) | ||
AS [Average Write Wait (ms)] , | ||
io_stall_read_ms + io_stall_write_ms AS [Total I/O Waits (ms)] , | ||
num_of_reads + num_of_writes AS [Number of I/O Operations] , | ||
CAST(( io_stall_read_ms + io_stall_write_ms ) / ( 1.0 + num_of_reads | ||
+ num_of_writes) | ||
AS NUMERIC(10,1)) AS [Average I/O Wait (ms)] | ||
FROM sys.dm_io_virtual_file_stats(NULL, NULL) | ||
ORDER BY [Average I/O Wait (ms)] DESC ; |
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,28 @@ | ||
/***************************************** | ||
Ввод/вывод в таблицы | ||
*******************************************/ | ||
|
||
SELECT OBJECT_SCHEMA_NAME(ddius.object_id) + '.' + OBJECT_NAME(ddius.object_id) AS [Object Name] , | ||
CASE | ||
WHEN ( SUM(user_updates + user_seeks + user_scans + user_lookups) = 0 ) | ||
THEN NULL | ||
ELSE CONVERT(DECIMAL(38,2), CAST(SUM(user_seeks + user_scans + user_lookups) AS DECIMAL) | ||
/ CAST(SUM(user_updates + user_seeks + user_scans | ||
+ user_lookups) AS DECIMAL) ) | ||
END AS [Proportion of Reads] , | ||
CASE | ||
WHEN ( SUM(user_updates + user_seeks + user_scans + user_lookups) = 0 ) | ||
THEN NULL | ||
ELSE CONVERT(DECIMAL(38,2), CAST(SUM(user_updates) AS DECIMAL) | ||
/ CAST(SUM(user_updates + user_seeks + user_scans | ||
+ user_lookups) AS DECIMAL) ) | ||
END AS [Proportion of Writes] , | ||
SUM(user_seeks + user_scans + user_lookups) AS [Total Read Operations] , | ||
SUM(user_updates) AS [Total Write Operations] | ||
FROM sys.dm_db_index_usage_stats AS ddius | ||
JOIN sys.indexes AS i ON ddius.object_id = i.object_id | ||
AND ddius.index_id = i.index_id | ||
WHERE i.type_desc IN ( 'CLUSTERED', 'HEAP') --only works in Current db | ||
GROUP BY ddius.object_id | ||
ORDER BY [Total Write Operations] DESC | ||
GO |
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,20 @@ | ||
/******************************************* | ||
Какие запросы используют tempdb из текущего инстанса | ||
********************************************/ | ||
|
||
SELECT es.host_name , es.login_name , es.program_name, | ||
st.dbid as QueryExecContextDBID, DB_NAME(st.dbid) as QueryExecContextDBNAME, st.objectid as ModuleObjectId, | ||
SUBSTRING(st.text, er.statement_start_offset/2 + 1,(CASE WHEN er.statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(max),st.text)) * 2 ELSE er.statement_end_offset | ||
END - er.statement_start_offset)/2) as Query_Text, | ||
tsu.session_id ,tsu.request_id, tsu.exec_context_id, | ||
(tsu.user_objects_alloc_page_count - tsu.user_objects_dealloc_page_count) as OutStanding_user_objects_page_counts, | ||
(tsu.internal_objects_alloc_page_count - tsu.internal_objects_dealloc_page_count) as OutStanding_internal_objects_page_counts, | ||
er.start_time, er.command, er.open_transaction_count, er.percent_complete, er.estimated_completion_time, er.cpu_time, er.total_elapsed_time, er.reads,er.writes, | ||
er.logical_reads, er.granted_query_memory | ||
FROM sys.dm_db_task_space_usage tsu inner join sys.dm_exec_requests er | ||
ON ( tsu.session_id = er.session_id and tsu.request_id = er.request_id) | ||
inner join sys.dm_exec_sessions es ON ( tsu.session_id = es.session_id ) | ||
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) st | ||
WHERE (tsu.internal_objects_alloc_page_count+tsu.user_objects_alloc_page_count) > 0 | ||
ORDER BY (tsu.user_objects_alloc_page_count - tsu.user_objects_dealloc_page_count)+(tsu.internal_objects_alloc_page_count - tsu.internal_objects_dealloc_page_count) | ||
DESC |
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,25 @@ | ||
/***************************************** | ||
Топ типов ожиданий в SQL Engine | ||
*******************************************/ | ||
|
||
WITH Waits | ||
AS ( SELECT wait_type , | ||
wait_time_ms / 1000. AS wait_time_sec , | ||
100. * wait_time_ms / SUM(wait_time_ms) OVER ( ) AS pct , | ||
ROW_NUMBER() OVER ( ORDER BY wait_time_ms DESC ) AS rn | ||
FROM sys.dm_os_wait_stats | ||
WHERE wait_type NOT IN ( 'CLR_SEMAPHORE', 'LAZYWRITER_SLEEP', | ||
'RESOURCE_QUEUE', 'SLEEP_TASK', | ||
'SLEEP_SYSTEMTASK', | ||
'SQLTRACE_BUFFER_FLUSH', 'WAITFOR', | ||
'LOGMGR_QUEUE', 'CHECKPOINT_QUEUE', | ||
'XE_TIMER_EVENT', 'XE_DISPATCHER_WAIT', | ||
'REQUEST_FOR_DEADLOCK_SEARCH', 'SQLTRACE_INCREMENTAL_FLUSH_SLEEP' ) | ||
) | ||
SELECT wait_type AS [Wait Type], | ||
CAST(wait_time_sec AS DECIMAL(12, 2)) AS [Wait Time (s)] , | ||
CAST(pct AS DECIMAL(12, 2)) AS [Wait Time (%)] | ||
FROM Waits | ||
WHERE pct > 0 | ||
ORDER BY wait_time_sec DESC | ||
GO |
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,25 @@ | ||
/***************************************** | ||
Фрагментированные индексы | ||
*******************************************/ | ||
SELECT '[' + DB_NAME() + '].[' + OBJECT_SCHEMA_NAME(ddips.[object_id], | ||
DB_ID()) + '].[' | ||
+ OBJECT_NAME(ddips.[object_id], DB_ID()) + ']' AS [Object] , | ||
i.[name] AS [Index] , | ||
ddips.[index_type_desc] AS [Index Type], | ||
ddips.[partition_number] AS [Partition Number], | ||
ddips.[alloc_unit_type_desc] AS [Allocation Unit Type], | ||
ddips.[index_depth] AS [Index Depth], | ||
ddips.[index_level] AS [Index Level], | ||
CAST(ddips.[avg_fragmentation_in_percent] AS SMALLINT) | ||
AS [Average Fragmentation (%)] , | ||
CAST(ddips.[avg_fragment_size_in_pages] AS SMALLINT) | ||
AS [Average Fragment Size (pages)] , | ||
ddips.[fragment_count] AS [Fragments], | ||
ddips.[page_count] AS [Pages] | ||
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, | ||
NULL, NULL, 'limited') ddips | ||
INNER JOIN sys.[indexes] i ON ddips.[object_id] = i.[object_id] | ||
AND ddips.[index_id] = i.[index_id] | ||
WHERE ddips.[avg_fragmentation_in_percent] > 5 | ||
ORDER BY ddips.[avg_fragmentation_in_percent] DESC | ||
GO |
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,20 @@ | ||
/***************************************** | ||
Генерация запросов на ребилд некластеризованных индексов | ||
*******************************************/ | ||
|
||
SELECT '[' + DB_NAME() + '].[' + OBJECT_SCHEMA_NAME(ddips.[object_id], | ||
DB_ID()) + '].[' | ||
+ OBJECT_NAME(ddips.[object_id], DB_ID()) + ']' AS [Object] , | ||
i.[name] AS [Index] , | ||
CAST(ddips.[avg_fragmentation_in_percent] AS SMALLINT) | ||
AS [Average Fragmentation (%)], | ||
'ALTER INDEX ' + i.[name] + ' ON ' + '[' + DB_NAME() + '].[' + OBJECT_SCHEMA_NAME(ddips.[object_id], | ||
DB_ID()) + '].[' | ||
+ OBJECT_NAME(ddips.[object_id], DB_ID()) + '] REBUILD;' AS [Rebuild Query] | ||
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, | ||
NULL, NULL, 'limited') ddips | ||
INNER JOIN sys.[indexes] i ON ddips.[object_id] = i.[object_id] | ||
AND ddips.[index_id] = i.[index_id] | ||
WHERE ddips.[avg_fragmentation_in_percent] > 30 AND ddips.[index_type_desc] = 'NONCLUSTERED INDEX' | ||
ORDER BY ddips.[avg_fragmentation_in_percent] DESC | ||
GO |
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,10 @@ | ||
/***************************************** | ||
Таблицы без кластизованных индексов (кучи) | ||
*******************************************/ | ||
|
||
SELECT o.name | ||
FROM sys.objects o | ||
WHERE o.type='U' | ||
AND NOT EXISTS(SELECT 1 FROM sys.indexes i | ||
WHERE o.object_id = i.object_id | ||
AND i.type_desc = 'CLUSTERED') |
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,28 @@ | ||
/***************************************** | ||
Топ-25 индексов, которые реже всего используются для поиска | ||
*******************************************/ | ||
SELECT TOP 25 | ||
o.name AS ObjectName | ||
, i.name AS IndexName | ||
, i.index_id AS IndexID | ||
, dm_ius.user_seeks AS UserSeek | ||
, dm_ius.user_scans AS UserScans | ||
, dm_ius.user_lookups AS UserLookups | ||
, dm_ius.user_updates AS UserUpdates | ||
, p.TableRows | ||
, 'DROP INDEX ' + QUOTENAME(i.name) | ||
+ ' ON ' + QUOTENAME(s.name) + '.' + QUOTENAME(OBJECT_NAME(dm_ius.OBJECT_ID)) AS 'drop statement' | ||
FROM sys.dm_db_index_usage_stats dm_ius | ||
INNER JOIN sys.indexes i ON i.index_id = dm_ius.index_id AND dm_ius.OBJECT_ID = i.OBJECT_ID | ||
INNER JOIN sys.objects o ON dm_ius.OBJECT_ID = o.OBJECT_ID | ||
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id | ||
INNER JOIN (SELECT SUM(p.rows) TableRows, p.index_id, p.OBJECT_ID | ||
FROM sys.partitions p GROUP BY p.index_id, p.OBJECT_ID) p | ||
ON p.index_id = dm_ius.index_id AND dm_ius.OBJECT_ID = p.OBJECT_ID | ||
WHERE OBJECTPROPERTY(dm_ius.OBJECT_ID,'IsUserTable') = 1 | ||
AND dm_ius.database_id = DB_ID() | ||
AND i.type_desc = 'nonclustered' | ||
AND i.is_primary_key = 0 | ||
AND i.is_unique_constraint = 0 | ||
ORDER BY (dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups) ASC | ||
GO |
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,30 @@ | ||
/***************************************** | ||
Какие БД в текущем инстансе занимают память в буфере | ||
*******************************************/ | ||
|
||
DECLARE @total_buffer INT; | ||
|
||
SELECT @total_buffer = cntr_value | ||
FROM sys.dm_os_performance_counters | ||
WHERE RTRIM([object_name]) LIKE '%Buffer Manager' | ||
AND counter_name = 'Total Pages'; | ||
|
||
WITH src AS | ||
( | ||
SELECT | ||
database_id, db_buffer_pages = COUNT_BIG(*) | ||
FROM sys.dm_os_buffer_descriptors | ||
--WHERE database_id BETWEEN 5 AND 32766 | ||
GROUP BY database_id | ||
) | ||
SELECT | ||
[db_name] = CASE [database_id] WHEN 32767 | ||
THEN 'Resource DB' | ||
ELSE DB_NAME([database_id]) END, | ||
db_buffer_pages, | ||
db_buffer_MB = db_buffer_pages / 128, | ||
db_buffer_percent = CONVERT(DECIMAL(6,3), | ||
db_buffer_pages * 100.0 / @total_buffer) | ||
FROM src | ||
ORDER BY db_buffer_MB DESC; | ||
GO |
Oops, something went wrong.