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

首頁 > 數據庫 > SQL Server > 正文

SQL Server 性能調優1

2024-08-31 00:47:59
字體:
來源:轉載
供稿:網友

1、 用程序中,保證在實現功能的基礎上,盡量減少對數據庫的訪問次數;通過搜索參數,盡量減少對表的訪問行數,最小化結果集,從而減輕網絡負擔;能夠分開的操作盡量分開處理,提高每次的響應速度;在數據窗口使用sql時,盡量把使用的索引放在選擇的首列;算法的結構盡量簡單;在查詢時,不要過多地使用通配符如select * from t1語句,要用到幾列就選擇幾列如:select col1,col2 from t1;在可能的情況下盡量限制盡量結果集行數如:select top 300 col1,col2,col3 from t1,因為某些情況下用戶是不需要那么多的數據的。不要在應用中使用數據庫游標,游標是非常有用的工具,但比使用常規的、面向集的sql語句需要更大的開銷;按照特定順序提取數據的查找。

2、   避免使用不兼容的數據類型。例如float和int、char和varchar、binary和varbinary是不兼容的。數據類型的不兼容可能使優化器無法執行一些本來可以進行的優化操作。例如:
select name from employee where salary > 60000
在這條語句中,如salary字段是money型的,則優化器很難對其進行優化,因為60000是個整型數。我們應當在編程時將整型轉化成為錢幣型,而不要等到運行時轉化。

3、   盡量避免在where子句中對字段進行函數或表達式操作,這將導致引擎放棄使用索引而進行全表掃描。如:
select * from t1 where f1/2=100
應改為:
select * from t1 where f1=100*2

select * from record where substring(card_no,1,4)=’5378’
應改為:
select * from record where card_no like ‘5378%’

select member_number, first_name, last_name  from members
where datediff(yy,datofbirth,getdate()) > 21
應改為:
select member_number, first_name, last_name  from members
where dateofbirth < dateadd(yy,-21,getdate())
即:任何對列的操作都將導致表掃描,它包括數據庫函數、計算表達式等等,查詢時要盡可能將操作移至等號右邊。

4、   避免使用!=或<>、is null或is not null、in ,not in等這樣的操作符,因為這會使系統無法使用索引,而只能直接搜索表中的數據。例如:
select id from employee where id != 'b%'
優化器將無法通過索引來確定將要命中的行數,因此需要搜索該表的所有行。
 
5、   盡量使用數字型字段,一部分開發人員和數據庫管理人員喜歡把包含數值信息的字段
設計為字符型,這會降低查詢和連接的性能,并會增加存儲開銷。這是因為引擎在處理查詢和連接回逐個比較字符串中每一個字符,而對于數字型而言只需要比較一次就夠了。

6、   合理使用exists,not exists子句。如下所示:
1.select sum(t1.c1)from t1 where(
(select count(*)from t2 where t2.c2=t1.c2>0)
2.select sum(t1.c1) from t1where exists(
  select * from t2 where t2.c2=t1.c2)
兩者產生相同的結果,但是后者的效率顯然要高于前者。因為后者不會產生大量鎖定的表掃描或是索引掃描。
如果你想校驗表里是否存在某條紀錄,不要用count(*)那樣效率很低,而且浪費服務器資源。可以用exists代替。如:
if (select count(*) from table_name where column_name = 'xxx')
可以寫成:
if exists (select * from table_name where column_name = 'xxx')

經常需要寫一個t_sql語句比較一個父結果集和子結果集,從而找到是否存在在父結果集中有而在子結果集中沒有的記錄,如:
1.select a.hdr_key  from hdr_tbl a---- tbl a 表示tbl用別名a代替
where not exists (select * from dtl_tbl b where a.hdr_key = b.hdr_key)

2.select a.hdr_key  from hdr_tbl a
left join dtl_tbl b on a.hdr_key = b.hdr_key  where b.hdr_key is null

3.select hdr_key  from hdr_tbl
where hdr_key not in (select hdr_key from dtl_tbl)
      三種寫法都可以得到同樣正確的結果,但是效率依次降低。

7、   盡量避免在索引過的字符數據中,使用非打頭字母搜索。這也使得引擎無法利用索引。 
見如下例子:
select * from t1 where name like ‘%l%’
select * from t1 where substing(name,2,1)=’l’
select * from t1 where name like ‘l%’
即使name字段建有索引,前兩個查詢依然無法利用索引完成加快操作,引擎不得不對全表所有數據逐條操作來完成任務。而第三個查詢能夠使用索引來加快操作。

8、   分利用連接條件,在某種情況下,兩個表之間可能不只一個的連接條件,這時在  where 子句中將連接條件完整的寫上,有可能大大提高查詢速度。
例:
select sum(a.amount) from account a,card b where a.card_no = b.card_no
select sum(a.amount) from account a,card b where a.card_no = b.card_no  and a.account_no=b.account_no
第二句將比第一句執行快得多。

9、    消除對大型表行數據的順序存取
      盡管在所有的檢查列上都有索引,但某些形式的where子句強迫優化器使用順序存取。如:
select * from orders where (customer_num=104  and order_num>1001) or
order_num=1008
解決辦法可以使用并集來避免順序存取:
select * from orders where customer_num=104 and order_num>1001
union
select * from orders where order_num=1008
這樣就能利用索引路徑處理查詢。

10、 避免困難的正規表達式
      like關鍵字支持通配符匹配,技術上叫正規表達式。但這種匹配特別耗費時間。例如:select * from customer where zipcode like “98_ _ _”
即使在zipcode字段上建立了索引,在這種情況下也還是采用順序掃描的方式。如
果把語句改為select * from customer where zipcode >“98000”,在執行查詢
時就會利用索引來查詢,顯然會大大提高速度。
11、 使用視圖加速查詢
把表的一個子集進行排序并創建視圖,有時能加速查詢。它有助于避免多重排序
操作,而且在其他方面還能簡化優化器的工作。例如:
select cust.name,rcvbles.balance,……other columns
from cust,rcvbles
where cust.customer_id = rcvlbes.customer_id
and rcvblls.balance>0
and cust.postcode>“98000”
order by cust.name
如果這個查詢要被執行多次而不止一次,可以把所有未付款的客戶找出來放在一個
視圖中,并按客戶的名字進行排序:
create view dbo.v_cust_rcvlbes
as
select cust.name,rcvbles.balance,……other columns
from cust,rcvbles
where cust.customer_id = rcvlbes.customer_id
and rcvblls.balance>0
order by cust.name

然后以下面的方式在視圖中查詢:
select * from  v_cust_rcvlbes
where postcode>“98000”
視圖中的行要比主表中的行少,而且物理順序就是所要求的順序,減少了磁盤
i/o,所以查詢工作量可以得到大幅減少。

12、 能夠用between的就不要用in
select * from t1 where id in (10,11,12,13,14)
改成:
select * from t1 where id between 10 and 14
因為in會使系統無法使用索引,而只能直接搜索表中的數據。

13、 distinct的就不用group by
      select orderid  from details where unitprice > 10 group by orderid
      可改為:
      select distinct orderid from details where unitprice > 10
     

14、   部分利用索引
      1.select employeeid, firstname, lastname
from names
where dept = 'prod' or city = 'orlando' or division = 'food'

      2.select employeeid, firstname, lastname from names where dept = 'prod'
union all
select employeeid, firstname, lastname from names where city = 'orlando'
union all
select employeeid, firstname, lastname from names where division = 'food'
如果dept 列建有索引則查詢2可以部分利用索引,查詢1則不能。

15、   能用union  all就不要用union
union  all不執行select distinct函數,這樣就會減少很多不必要的資源

16、   不要寫一些不做任何事的查詢
如:select col1 from t1 where 1=0
    select col1 from t1 where col1=1 and col1=2
這類死碼不會返回任何結果集,但是會消耗系統資源。

17、  盡量不要用select into語句。
select inot 語句會導致表鎖定,阻止其他用戶訪問該表。

18、 必要時強制查詢優化器使用某個索引
     select * from t1 where nextprocess = 1 and processid in (8,32,45)
改成:
select * from t1 (index = ix_processid) where nextprocess = 1 and processid in (8,32,45)
則查詢優化器將會強行利用索引ix_processid 執行查詢。
    
19、  雖然update、delete語句的寫法基本固定,但是還是對update語句給點建議:
a) 盡量不要修改主鍵字段。
b) 當修改varchar型字段時,盡量使用相同長度內容的值代替。
c) 盡量最小化對于含有update觸發器的表的update操作。
d) 避免update將要復制到其他數據庫的列。
e) 避免update建有很多索引的列。
f) 避免update在where子句條件中的列。


上面我們提到的是一些基本的提高查詢速度的注意事項,但是在更多的情況下,往往需要反復試驗比較不同的語句以得到最佳方案。最好的方法當然是測試,看實現相同功能的sql語句哪個執行時間最少,但是數據庫中如果數據量很少,是比較不出來的,這時可以用查看執行計劃,即:把實現相同功能的多條sql語句考到查詢分析器,按ctrl+l看查所利用的索引,表掃描次數(這兩個對性能影響最大),總體上看詢成本百分比即可。
簡單的存儲過程可以用向導自動生成:在企業管理器工具欄點擊運行向導圖標,點擊”數據庫”、”創建存儲過程向導”。復雜存儲過程的調試:在查詢分析器左邊的對象瀏覽器(沒有?按f8)選擇要調試的存儲過程,點右鍵,點調試,輸入參數執行,出現一個浮動工具條,上面有單步執行,斷點設置等。

 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 綦江县| 三门峡市| 澳门| 丹巴县| 灵川县| 彭州市| 南岸区| 光泽县| 海门市| 德阳市| 西丰县| 鹤岗市| 东宁县| 阜新市| 吕梁市| 平顶山市| 轮台县| 忻城县| 衢州市| 玛多县| 上蔡县| 苍梧县| 康平县| 简阳市| 松原市| 龙里县| 洪湖市| 平昌县| 南澳县| 和静县| 卓尼县| 仲巴县| 德钦县| 增城市| 泽州县| 临桂县| 彝良县| 拉萨市| 巴彦县| 修水县| 化州市|