DEFINITIONS

Definitions More Info.
Definition ID982
TitleSQL
CategoryNOTES
Definitionupdate statistics oguz
Definition Description
--exec dpAudit.[dbo].dbStatsUpdateFullScan @Databases = 'AtlantisDB', @execute=1

SELECT RowID, DbName, Fark, [Table],
Statistic, --updStat, updStatFull,
[Stats Last Updated],
Rows, [Rows Sampled], [Unfiltered Rows],
[Row Modifications], [Histogram Steps], startDate, endDate
FROM StaticUpdate WITH (nolock)
-- WHERE startDate IS NULL
ORDER BY startDate DESC



USE [DPAudit]
GO
/****** Object: StoredProcedure [dbo].[dbStatsUpdateFullScan] Script Date: 10/2/2019 4:37:58 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER
procedure [dbo].[dbStatsUpdateFullScan]
-- exec dpAudit.[dbo].dbStatsUpdateFullScan @Databases = 'ORKA', @execute=0

@Databases nvarchar(max) = NULL, -- ALL (Tüm veritabanlari), dpAudit (bir Database adi)
@execute int=0,
@SortLevel int = 99

as
set nocount on
DECLARE @cmd1 nvarchar(max)
DECLARE @cmd2 nvarchar(max)

declare @productversion nvarchar(20)
select @productversion = convert(nvarchar, SERVERPROPERTY('productversion') )


SET @cmd2 =
+ ' insert into [dpAudit].[dbo].[StaticUpdate]'
+ ' SELECT '
+ ' DB_NAME() as DbName, '
+ ' datediff (MINUTE, sp.last_updated, getdate()),'
+ ' sch.name + ''.'' + so.name, '
+ ' ss.name, '
IF (LEFT(@productversion, 2) = 14)
BEGIN
set @cmd2 = @cmd2 + ' concat(''UPDATE STATISTICS ['' + db_name() + ''].['', sch.name, ''].['', so.name, ''] ['', ss.name , ''] WITH MAXDOP=8;''),'
+ ' concat(''UPDATE STATISTICS ['' + db_name() + ''].['', sch.name, ''].['', so.name, ''] ['', ss.name , ''] WITH FULLSCAN, MAXDOP=8;''),'
END
ELSE
BEGIN
set @cmd2 = @cmd2 + ' concat(''UPDATE STATISTICS ['' + db_name() + ''].['', sch.name, ''].['', so.name, ''] ['', ss.name , '']; ''),'
+ ' concat(''UPDATE STATISTICS ['' + db_name() + ''].['', sch.name, ''].['', so.name, ''] ['', ss.name , ''] WITH FULLSCAN;''),'
END

set @cmd2 = @cmd2
+ ' sp.last_updated,'
+ ' sp.rows,'
+ ' sp.rows_sampled,'
+ ' sp.unfiltered_rows,'
+ ' sp.modification_counter,'
+ ' sp.steps,'
+ ' NULL,'
+ ' NULL,'
+ '
CASE
WHEN sp.modification_counter >= sp.rows THEN ''99''
WHEN sp.unfiltered_rows > [rows] THEN ''90''
WHEN round( sp.modification_counter*1.0 / [rows]*100, 2) >= 50 THEN 88
WHEN sp.modification_counter > 0 THEN ''77''
ELSE 1
END Sorting '


+ ' FROM sys.stats ss'
+ ' JOIN sys.objects so ON ss.object_id = so.object_id'
+ ' JOIN sys.schemas sch ON so.schema_id = sch.schema_id'
+ ' OUTER APPLY sys.dm_db_stats_properties(so.object_id, ss.stats_id) AS sp'
+ ' WHERE '
+ ' so.TYPE IN (''U'', ''V'') and'
+ ' so.TYPE = so.TYPE '
+ ' and (sp.rows != sp.rows_sampled or sp.modification_counter > sp.rows)'
+ ' ORDER BY Sorting desc, [Rows] -- sp.last_updated;'
+ ' --and sp.last_updated < getdate() - 1'
+ ' --and [sp].[modification_counter] > 0 '
+ ' --and modification_counter > 1000'
+ ' --and ss.user_created = 0 '
+ ' --and [sp].[modification_counter]/[sp].[rows] > 0.01'
+ ' --and ss.name LIKE ''_W%'''
+ ' --and sp.last_updated IS NULL'
+ ' --and sp.last_updated IS NOT NULL'
+ ' --and datediff (MINUTE, sp.last_updated, getdate()) > 35'



IF @Databases != 'ALL'
BEGIN

SET @cmd1 = 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'
+ 'USE ' + QUOTENAME(@Databases) + ';'

set @cmd1 = @cmd1 + @cmd2

exec (@cmd1)

END
ELSE
BEGIN

CREATE TABLE #dbSize
(
[rowID] int identity (1,1),
[DbName] [nvarchar](256) NULL,
[DbSize] [int] NULL
)

INSERT INTO #dbSize
select
DATABASE_NAME = db_name(s_mf.database_id),
DATABASE_SIZE = convert(int,
case -- more than 2TB(maxint) worth of pages (by 8K each) can not fit an int...
when sum(convert(bigint,s_mf.size)) >= 268435456
then null
else sum(convert(bigint,s_mf.size))*8 -- Convert from 8192 byte pages to Kb
end)
from
sys.master_files s_mf
where
s_mf.state = 0 and -- ONLINE
has_dbaccess(db_name(s_mf.database_id)) = 1 and -- Only look at databases to which we have access
db_name(s_mf.database_id) NOT IN ('master','msdb','tempdb','model')
group by s_mf.database_id
order by 2

DECLARE @Sayac INT = 1;

WHILE @Sayac <= (select count(*) from #dbSize)
BEGIN
select @Databases = [DbName] from #dbSize WHERE [rowID] = @Sayac

SET @cmd1 = 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'
+ 'USE ' + QUOTENAME(@Databases) + ';'

set @cmd1 = @cmd1 + @cmd2

exec (@cmd1)

SET @Sayac += 1;

END;
drop table #dbSize
END
---staticupdate tablosuna eklenilen kayitlari dönerek execute edildigi yer


SET @Sayac =(select min(RowID) from StaticUpdate where startDate is null)
declare @Sorting int = 99

WHILE @Sayac<= (select max(RowID) from [dpAudit].[dbo].[StaticUpdate] where startDate is null)
BEGIN

select @cmd1 = [updStatFull], @Sorting = Sorting
from [dpAudit].[dbo].[StaticUpdate] WHERE [RowID] = @Sayac
if @execute=1
BEGIN
update [dpAudit].[dbo].[StaticUpdate]
set startDate=Getdate() WHERE [RowID] = @Sayac;

IF @Sorting >= @SortLevel
exec (@cmd1)

update [dpAudit].[dbo].[StaticUpdate]
set endDate=Getdate() WHERE [RowID] = @Sayac;
END

else
BEGIN
print @cmd1
END
SET @Sayac += 1;

END;

SELECT RowID, DbName, Fark, [Table], Statistic, updStat, updStatFull, [Stats Last Updated], Rows, [Rows Sampled], [Unfiltered Rows], [Row Modifications], [Histogram Steps], startDate, endDate
--,round( [Row Modifications]*1.0 / [Rows]*100, 2) as Oran,
,CASE
WHEN [Row Modifications] >= [Rows] THEN '99'
WHEN [Unfiltered Rows] > [Rows] THEN '90'
WHEN round( [Row Modifications]*1.0 / [Rows]*100, 2) >= 50 THEN 88
WHEN [Row Modifications] > 0 THEN '77'
ELSE 1
END Sorting
FROM StaticUpdate
WHERE (startDate IS NULL)
ORDER BY Sorting desc, [Rows]

-- and sp.modification_counter > sp.rows

delete from StaticUpdate
where endDate is null
RecordBycunay
Record Date02-10-2019 16:38:12
Düzenle
Kopyala
Sil