国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

如何提高SQL SERVER的性能

2024-08-31 00:48:27
字體:
來源:轉載
供稿:網(wǎng)友
 
如何提高sql server的性能

 

第一篇:通過維護表的索引來提高數(shù)據(jù)的訪問速度

 

大多數(shù)sql server表需要索引來提高數(shù)據(jù)的訪問速度,如果沒有索引,sql server要進行表格掃描讀取表中的每一個記錄才能找到索要的數(shù)據(jù)。索引可以分為簇索引和非簇索引,簇索引通過重排表中的數(shù)據(jù)來提高數(shù)據(jù)的訪問速度,而非簇索引則通過維護表中的數(shù)據(jù)指針來提高數(shù)據(jù)的索引。

 

   索引的體系結構:

 

為什么要不斷的維護表的索引?首先,簡單介紹一下索引的體系結構。sql server在硬盤中用8kb頁面在數(shù)據(jù)庫文件內存放數(shù)據(jù)。缺省情況下這些頁面及其包含的數(shù)據(jù)是無組織的。為了使混亂變?yōu)橛行颍鸵伤饕I伤饕螅陀辛怂饕摵蛿?shù)據(jù)頁,數(shù)據(jù)頁保存用戶寫入的數(shù)據(jù)信息。索引頁存放用于檢索列的數(shù)據(jù)值清單(關鍵字)和索引表中該值所在紀錄的地址指針。索引分為簇索引和非簇索引,簇索引實質上是將表中的數(shù)據(jù)排序,就好像是字典的索引目錄。非簇索引不對數(shù)據(jù)排序,它只保存了數(shù)據(jù)的指針地址。向一個帶簇索引的表中插入數(shù)據(jù),當數(shù)據(jù)頁達到100%時,由于頁面沒有空間插入新的的紀錄,這時就會發(fā)生分頁,sql server 將大約一半的數(shù)據(jù)從滿頁中移到空頁中,從而生成兩個半的滿頁。這樣就有大量的數(shù)據(jù)空間。簇索引是雙向鏈表,在每一頁的頭部保存了前一頁、后一頁地址以及分頁后數(shù)據(jù)移動的地址,由于新頁可能在數(shù)據(jù)庫文件中的任何地方,因此頁面的鏈接不一定指向磁盤的下一個物理頁,鏈接可能指向了另一個區(qū)域,這就形成了分塊,從而減慢了系統(tǒng)的速度。對于帶簇索引和非簇索引的表來說,非簇索引的關鍵字是指向簇索引的,而不是指向數(shù)據(jù)頁的本身。

為了克服數(shù)據(jù)分塊帶來的負面影響,需要重構表的索引,這是非常費時的,因此只能在需要時進行。可以通過dbcc showcontig來確定是否需要重構表的索引。下面舉例來說明dbcc showcontig和dbcc redbindex的使用方法。以sql server自帶的northwind數(shù)據(jù)作為例子

  帶開sql server的query analyzer輸入命令:

use northwind

declare @table_id int

set @table_id=object_id('orders')

dbcc showcontig(@table_id)

這個命令顯示northwind數(shù)據(jù)庫中的orders表的分塊情況,結果如下:

 

 

 

dbcc showcontig scanning 'orders' table...

table: 'orders' (21575115); index id: 1, database id: 6

table level scan performed.

- pages scanned................................: 20

- extents scanned..............................: 5

- extent switches..............................: 4

- avg. pages per extent........................: 4.0

- scan density [best count:actual count].......: 60.00% [3:5]

- logical scan fragmentation ..................: 0.00%

- extent scan fragmentation ...................: 40.00%

- avg. bytes free per page.....................: 146.5

- avg. page density (full).....................: 98.19%

dbcc execution completed. if dbcc printed error messages, contact your system administrator.

通過分析這些結果可以知道該表的索引是否需要重構。表1.1描述了每一行的意義

 

 

 

 

 

 

 

 信息                          描述

pages scanned                    表或索引中的長頁數(shù)

extents scanned                               表或索引中的長區(qū)頁數(shù)

extent switches                                dbcc遍歷頁時從一個區(qū)域到另

一個區(qū)域的次數(shù)

avg. pages per extent                       相關區(qū)域中的頁數(shù)

scan density                                    best count是連續(xù)鏈接時的理想?yún)^(qū)

[best count:actual count]                域改變數(shù),actual count是實際區(qū)

域改變數(shù),scan density為100%

表示沒有分塊。

logical scan fragmentation            掃描索引頁中失序頁的百分比

extent scan fragmentation               不實際相鄰和包含鏈路中所有鏈

接頁的區(qū)域數(shù)

avg. bytes free per page                  掃描頁面中平均自由字節(jié)數(shù)

avg. page density (full)                     平均頁密度,表示頁有多滿

 

從上面命令的執(zhí)行結果可以看的出來,best count為3 而actual count為5這表明orders表有分塊需要重構表索引。下面通過dbcc dbreindex來重構表的簇索引。

同樣在query analyzer中輸入命令:

use northwind

dbcc dbreindex('northwind.dbo.orders',pk_orders,90)

執(zhí)行結果:

dbcc execution completed. if dbcc printed error messages, contact your system administrator.

dbcc dbreindex參數(shù)說明:第一個參數(shù)為要重構的表明。第二個參數(shù)為需要重構的索引表識,‘’表示所有的索引。第三個參數(shù)為頁的填充因子,填充因子越大,頁越滿。

然后再用dbcc showcontig查看重構簇索引后的結果:

use northwind

declare @table_id int

set @table_id=object_id('orders')

dbcc showcontig(@table_id)

返回結果如下:

dbcc showcontig scanning 'orders' table...

table: 'orders' (21575115); index id: 1, database id: 6

table level scan performed.

- pages scanned................................: 22

- extents scanned..............................: 3

- extent switches..............................: 2

- avg. pages per extent........................: 7.3

- scan density [best count:actual count].......: 100.00% [3:3]

- logical scan fragmentation ..................: 0.00%

- extent scan fragmentation ...................: 33.33%

- avg. bytes free per page.....................: 869.2

- avg. page density (full).....................: 89.26%

dbcc execution completed. if dbcc printed error messages, contact your system administrator.

通過結果我們可以看到scan denity為100%表沒有分塊不需要重構表索引了。如果重構表的簇索引scan denity還小于100%的話可以重構表的全部索引。命令如下:

--use northwind

--dbcc dbreindex('northwind.dbo.orders',’’,90)

 

使用作業(yè)定時重構索引:

 

如果你的數(shù)據(jù)庫訪問非常頻繁的話,非常容易出現(xiàn)數(shù)據(jù)分塊的現(xiàn)象,因此你可以利用作業(yè)來幫你在系統(tǒng)相對空閑的時候重構你的索引。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大新县| 常熟市| 拜城县| 砀山县| 仁寿县| 唐河县| 海兴县| 边坝县| 宜章县| 全南县| 滨州市| 综艺| 汾阳市| 开封市| 岑溪市| 湛江市| 揭西县| 赞皇县| 涟水县| 海口市| 琼结县| 宝坻区| 天镇县| 仁怀市| 临洮县| 金山区| 邯郸市| 拜泉县| 丰台区| 芜湖市| 福鼎市| 松滋市| 青州市| 广元市| 万源市| 深水埗区| 达州市| 和田县| 肃宁县| 东乌| 达孜县|