【发布时间】:2020-01-17 03:34:12
【问题描述】:
如果删除了一个对象(表/索引..),与该对象相关的统计信息是否仍然可用,或者与被删除的对象一起被删除?
【问题讨论】:
标签: sql sql-server statistics
如果删除了一个对象(表/索引..),与该对象相关的统计信息是否仍然可用,或者与被删除的对象一起被删除?
【问题讨论】:
标签: sql sql-server statistics
来自 virtual-dba.com
删除表/索引时请注意:如果有任何统计类型 与表关联,然后数据库解除关联 统计类型并删除收集的任何用户定义的统计信息 使用统计类型。
【讨论】:
Since auto-stats are created implicitly and quietly behind the scenes, most DBAs are unaware of how many really exist. For every query that any client ever issued against the database, there may be a remaining statistics object created just for it. Even if it was a test query, issued once many years ago and never again. Auto Statistics objects are persistent and will remain in the database forever until explicitly dropped.
什么是默认跟踪?
默认跟踪是 SQL Server 2005 首次包含的一项新功能,它提供对架构修改(例如表创建和存储过程删除等)的审核。它默认运行,但你可以打开它..
欲了解更多信息:https://www.mssqltips.com/sqlservertip/1739/using-the-default-trace-in-sql-server/
在此查询中.. 使用EventClass 92 和 93 来跟踪数据库自动增长事件。这是查找谁在数据库或数据库本身中删除/创建或更改对象的查询。
DECLARE @current VARCHAR(500);
DECLARE @start VARCHAR(500);
DECLARE @indx INT;
SELECT @current = path
FROM sys.traces
WHERE is_default = 1;
SET @current = REVERSE(@current)
SELECT @indx = PATINDEX('%\%', @current)
SET @current = REVERSE(@current)
SET @start = LEFT(@current, LEN(@current) - @indx) + '\log.trc';
-- Change filter as needed
SELECT
CASE EventClass
WHEN 46 THEN 'Object:Created'
WHEN 47 THEN 'Object:Deleted'
WHEN 164 THEN 'Object:Altered'
END,
DatabaseName, ObjectName, HostName, ApplicationName, LoginName,
StartTime
FROM
::fn_trace_gettable(@start, DEFAULT)
WHERE
EventClass IN (46,47,164) AND EventSubclass = 0 AND DatabaseID <> 2
ORDER BY
StartTime DESC
您可以使用此查询来查找创建、删除、更改表信息的所有详细信息...
这里是 SQL Server 中的一些不同的默认跟踪......
SELECT DISTINCT Trace.EventID, TraceEvents.NAME AS Event_Desc
FROM ::fn_trace_geteventinfo(1) Trace,
sys.trace_events TraceEvents
WHERE Trace.eventID = TraceEvents.trace_event_id
上面的查询用于事件类,也在上面的例子中使用 (Object:Created, Object:Deleted, Object:Altered);您可以根据自己的要求进行更改。
【讨论】: