SQL Server自動索引和統計資訊管理

2021-09-10 05:32:10 字數 4201 閱讀 1640

微軟在github上提供了乙個指令碼,用於自動管理索引和統計資訊。位址

支援sql server 2005 sp2及之後的版本。

注:不保證可以在sql server 2005版本中正常執行。

安裝

直接在ssms中執行指令碼即可。如:

由於指令碼太長,需要看指令碼內容的朋友自己去網上檢視:

輸出資訊:

droping existing objects

preserving historic data

tbl_adaptiveindexdefrag_log table created

tbl_adaptiveindexdefrag_analysis_log table created

tbl_adaptiveindexdefrag_exceptions table created

tbl_adaptiveindexdefrag_working table created

tbl_adaptiveindexdefrag_stats_working table created

tbl_adaptiveindexdefrag_stats_log table created

tbl_adaptiveindexdefrag_ixdisablestatus table created

copying old data..

.done copying old data..

.removed old tables..

.procedure usp_adaptiveindexdefrag created

reporting views created

procedure usp_adaptiveindexdefrag_purgelogs created (

default

purge

is90 days old)

procedure usp_adaptiveindexdefrag_currentexecstats created (

use this to monitor defrag loop progress)

procedure usp_adaptiveindexdefrag_exceptions created (

if the defrag should not be daily,

use this to

seton which days to disallow it. it can be on entire dbs,

tables

and/

or indexes)

all done!

上面表示已經安裝成功了。

使用

儲存過程支援引數還是很多的,我這裡只介紹其中幾個常用的。其他引數,請檢視官方的文件:

下面結合幾個例子來說明一下:

注意:慎用。執行需要時間過長,而且預設情況下是離線重建索引。

exec dbo.usp_adaptiveindexdefrag @dbscope

='adventureworks2014'

exec dbo.usp_adaptiveindexdefrag @dbscope

='adventureworks2014'

,@tblname

='production.billofmaterials'

exec dbo.usp_adaptiveindexdefrag @exec_print=0

,@printcmds

=1

結果:

printing sql statements...

alter

index

[pk_warehouse_stockitems]

on[wideworldimporters]

.[warehouse]

.[stockitems] reorganize with

(lob_compaction =on)

;-- no need to update statistic [pk_warehouse_stockitems] on table or view [stockitems] of db [wideworldimporters]...

alter

index

[pk_purchasing_purchaseorders]

on[wideworldimporters]

.[purchasing]

.[purchaseorders] reorganize with

(lob_compaction =on)

;-- no need to update statistic [pk_purchasing_purchaseorders] on table or view [purchaseorders] of db [wideworldimporters]...

alter

index

on[wideworldimporters]..

[people] reorganize with

(lob_compaction =on)

;alter

index

[fk_purchasing_purchaseorderlines_purchaseorderid]

on[wideworldimporters]

.[purchasing]

.[purchaseorderlines] reorganize with

(lob_compaction =on)

;-- no need to update statistic [fk_purchasing_purchaseorderlines_purchaseorderid] on table or view [purchaseorderlines] of db [wideworldimporters]...

alter

index

[fk_purchasing_purchaseorderlines_stockitemid]

on[wideworldimporters]

.[purchasing]

.[purchaseorderlines] reorganize with

(lob_compaction =on)

;-- no need to update statistic [fk_purchasing_purchaseorderlines_stockitemid] on table or view [purchaseorderlines] of db [wideworldimporters]...

alter

index

[fk_purchasing_purchaseorderlines_packagetypeid]

on[wideworldimporters]

.[purchasing]

.[purchaseorderlines] reorganize with

(lob_compaction =on)

;-- no need to update statistic [fk_purchasing_purchaseorderlines_packagetypeid] on table or view [purchaseorderlines] of db [wideworldimporters]...

update

statistics

[msdb]

.[dbo]

.[tbl_adaptiveindexdefrag_stats_log]

([pk_adaptiveindexdefrag_stats_log]);

alter

index

[pk_adaptiveindexdefrag_stats_log]

on[msdb]

.[dbo]

.[tbl_adaptiveindexdefrag_stats_log] rebuild with

(data_compression = none,

fillfactor

=100

, sort_in_tempdb =

off)

;

SQL SERVER2008 自動生成索引

declare ix table index handle int,eq nvarchar 4000 included columns nvarchar 4000 name varchar 100 dbname varchar 30 with idx as select a.index handle...

SQL Server聚集索引和非聚焦索引

1 什麼是索引?索引在資料庫中的作用類似於目錄在書籍中的作用,用來提高查詢資訊的速度。使用索引查資料無需進行全表掃瞄,可以快速查詢所需的資料。2 聚集索引和非聚集索引的區別?乙個表只能有乙個聚集索引但可以有多個非聚集索引。聚集索引的葉節點就是最終的資料節點,而非聚集索引的葉節仍然是索引節點,但它有乙...

sql server 和mysql 建立索引

1.新增primary key 主鍵索引 alter table table name add primary key column 2.新增unique 唯一索引 alter table table name add unique column 3.新增index 普通索引 alter table...