NC3開發(fā)的環(huán)境培訓(xùn)_第1頁
NC3開發(fā)的環(huán)境培訓(xùn)_第2頁
NC3開發(fā)的環(huán)境培訓(xùn)_第3頁
NC3開發(fā)的環(huán)境培訓(xùn)_第4頁
NC3開發(fā)的環(huán)境培訓(xùn)_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、sql效率經(jīng)驗總結(jié)sql 開發(fā)觀點對于客戶-服務(wù)器結(jié)構(gòu)的數(shù)據(jù)庫應(yīng)用程序來說,減少網(wǎng)絡(luò)傳輸?shù)臄?shù)據(jù)量直接影響到應(yīng)用程序的性能。在編程時應(yīng)注意盡量減少網(wǎng)絡(luò)流量,避免不必要的數(shù)據(jù)傳輸。另外,數(shù)據(jù)庫的加鎖機制和事務(wù)處理也會直接影響到一個應(yīng)用程序性能的好壞。在這里提供一些建議供大家參考。合理使用存儲過程將完成一個功能的sql語句寫成存儲過程,不但可以減少網(wǎng)絡(luò)流量,而且由于存儲過程是預(yù)編譯的,能進(jìn)一步提高響應(yīng)速度。where子句在select語句中通過使用where子句來減少返回的記錄數(shù)。去掉不需要的字段避免使用 select * from 語句,要使用 select f1,f2 from 語句,去掉不需要

2、的字段。避免顯式或隱含的類型轉(zhuǎn)換避免顯式或隱含的類型轉(zhuǎn)換,如在where 子句中numeric 型和 int型的列的比較。sql server在select 語句中,如果表中的大部分記錄符合查詢條件,盡管where子句中的字段上有索引,但sql server不會使用索引,而是順序掃描該表。復(fù)合索引對于復(fù)合索引要注意,例如在建立復(fù)合索引時列的順序是f1,f2,f3,則在where 或order by子句中這些字段出現(xiàn)的順序要與建立索引時的字段順序一致,可以是f1或f1,f2 或f1,f2,f3。否則sql server不會用到該索引。盡快地提交事務(wù)sql server為了支持事務(wù)一致性,對共享的

3、資源上保留鎖直至事務(wù)被提交。其他要使用相同資源的用戶必須要等待。如果一個事務(wù)變長的話,鎖的隊列以及等待鎖的用戶隊列將會變長,這最終導(dǎo)致系統(tǒng)吞吐量的降低。長的事務(wù)還增加了出現(xiàn)死鎖的可能性。具體包括在事務(wù)中不能包含用戶交互,避免更新同一數(shù)據(jù)兩次,大批量的數(shù)據(jù)更新放在事物的后面部分等。盡量減少對列的四則運算 在where 子句中,盡量減少對列的四則運算。例如: select cola from tablea where salary * 12 12000 應(yīng)該用如下語句代替:select cola from tablea where salary 1000 在where 子句中,盡量用= 代替。例如

4、: select f1 from table1 where a3 (其中a 為int 型) 在該例中,a列上是有索引的,sql server掃描索引頁,直到a=3的頁,然后順序掃描,直到a=4,如果a=3的記錄很多,會有很多無效的i/o操作。 應(yīng)該用如下語句代替:elect f1 from table1 where a=4避免在if exists 和 in 操作符中使用notl因為不使用not,sql server在得到滿足條件的第一條記錄后返回,而使用not操作符,sql server 可能要掃描整個表。例如:if not exists (select * from tablea where

5、)begin statement group oneendelsebegin statement group twoendl應(yīng)該用如下語句代替:if exists (select * from tablea where)begin statement group twoendelsebegin statement group oneendif exists 語句在判斷有無符合條件的記錄時不要用select count (*) 語句,而是要用if exists 語句:例如:declare var intselect var = count(*) from employee where emp_i

6、d = 123if var != 0. more sql code .應(yīng)該用如下語句代替:if exists (select 1 from employee where emp_id = 123)begin. more sql code .endwhere子句中的等于列(1) 在where子句中如果對于某列有等于的條件,則在select子句中不應(yīng)該出現(xiàn)該列,例如:select cust_number, cust_namefrom customer where cust_number = 612 應(yīng)該用如下語句代替:select cust_namefrom customer where cust

7、_number = 612where子句中的等于列(2) 在where子句中如果對于某列有等于的條件,則在order by子句中不應(yīng)該出現(xiàn)該列,例如:select cust_number, cust_name, cust_stfrom customer where cust_st = lorder by cust_st, cust_name應(yīng)該用如下語句代替:select cust_number, cust_namefrom customer where cust_st = lorder by cust_number同一個列的多個or條件 在where子句中,對于同一個列的多個or條件,用in

8、 操作符來代替,例如:select cust_number, cust_name, cust_st from customerwhere cust_st = l or cust_st = a or cust_st = iorder by cust_st, cust_number 應(yīng)該為:select cust_number, cust_name , cust_st from customerwhere cust_st in (l, a, i)order by cust_st, cust_number求最大值和最小值的列 對于要求最大值和最小值的列,如果在該列上有索引,則要分開用兩個sql語句來

9、求, 例如:select max(price), min(price) from titles (price上有索引) 應(yīng)該用如下語句代替:select max(price) from titlesselect min(price) from titles 對于能用聯(lián)結(jié)實現(xiàn)的功能,不要用子查詢實現(xiàn)例如:select name from customer where customer_id in ( select customer_id from order where money1000) 應(yīng)該用如下語句代替:select name from customer inner join order

10、 on customer.customer_id=order.customer_idwhere order.money100檢查被更新的行數(shù)時 在檢查被更新的行數(shù)時用rowcount,而不用select count(*). 例如:select count(*) from customerwhere cust_st = 1 and cust_code = 2update customer set cust_discount = .15where cust_st =1 and cust_code = 2 應(yīng)該用如下語句代替:update customer set cust_discount = .15where cust_st = 1 and cust_code = 2select update_count = rowcount通配符”_”的

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論