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

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

關(guān)于數(shù)據(jù)庫優(yōu)化問題收集匯總

2024-08-31 00:45:04
字體:
供稿:網(wǎng)友


下面將從這三個(gè)方面分別進(jìn)行總結(jié):

 

select count(*) from record where date >'19991201' and date < '19991214'and amount >2000 (25秒)

select date ,sum(amount) from record group by date(55秒)

select count(*) from record where date >'19990901' and place in ('BJ','SH') (27秒)

select count(*) from record where date >'19991201' and date < '19991214' and amount >2000 (14秒)

select date,sum(amount) from record group by date(28秒)

select count(*) from record where date >'19990901' and place in ('BJ','SH')(14秒)

select count(*) from record where date >'19991201' and date < '19991214' and amount >2000 (26秒)

select date,sum(amount) from record group by date(27秒)

select count(*) from record where date >'19990901' and place in ('BJ, 'SH')(< 1秒)

select count(*) from record where date >'19991201' and date < '19991214' and amount >2000(< 1秒)

select date,sum(amount) from record group by date(11秒)

select count(*) from record where date >'19990901' and place in ('BJ','SH')(< 1秒)

---- 5.總結(jié):----

 

select sum(a.amount) from account a,card b where a.card_no = b.card_no(20秒)

select sum(a.amount) from account a,card b where a.card_no = b.card_no and a.account_no=b.account_no(< 1秒)

總結(jié):

 

select * from record wheresubstring(card_no,1,4)='5378'(13秒)

select * from record whereamount/30< 1000(11秒)

select * from record whereconvert(char(10),date,112)='19991201'(10秒)

分析:

select * from record where card_no like'5378%'(< 1秒)

select * from record where amount< 1000*30(< 1秒)

select * from record where date= '1999/12/01'(< 1秒)

select count(*) from stuff where id_no in('0','1')(23秒)

select count(*) from stuff where id_no='0'select count(*) from stuff where id_no='1'

create proc count_stuff asdeclare @a intdeclare @b intdeclare @c intdeclare @d char(10)beginselect @a=count(*) from stuff where id_no='0'select @b=count(*) from stuff where id_no='1'endselect @c=@a+@bselect @d=convert(char(10),@c)print @d

 

---- 總結(jié):

 

1、開發(fā)人員如果用到其他庫的Table或View,務(wù)必在當(dāng)前庫中建立View來實(shí)現(xiàn)跨庫操作,最好不要直接使用“databse.dbo.table_name”,因?yàn)閟p_depends不能顯示出該SP所使用的跨庫table或view,不方便校驗(yàn)。

2、開發(fā)人員在提交SP前,必須已經(jīng)使用set showplan on分析過查詢計(jì)劃,做過自身的查詢優(yōu)化檢查。

3、高程序運(yùn)行效率,優(yōu)化應(yīng)用程序,在SP編寫過程中應(yīng)該注意以下幾點(diǎn):

a) SQL的使用規(guī)范:

i. 盡量避免大事務(wù)操作,慎用holdlock子句,提高系統(tǒng)并發(fā)能力。

ii. 盡量避免反復(fù)訪問同一張或幾張表,尤其是數(shù)據(jù)量較大的表,可以考慮先根據(jù)條件提取數(shù)據(jù)到臨時(shí)表中,然后再做連接。

iii. 盡量避免使用游標(biāo),因?yàn)橛螛?biāo)的效率較差,如果游標(biāo)操作的數(shù)據(jù)超過1萬行,那么就應(yīng)該改寫;如果使用了游標(biāo),就要盡量避免在游標(biāo)循環(huán)中再進(jìn)行表連接的操作。

iv. 注意where字句寫法,必須考慮語句順序,應(yīng)該根據(jù)索引順序、范圍大小來確定條件子句的前后順序,盡可能的讓字段順序與索引順序相一致,范圍從大到小。

v. 不要在where子句中的“=”左邊進(jìn)行函數(shù)、算術(shù)運(yùn)算或其他表達(dá)式運(yùn)算,否則系統(tǒng)將可能無法正確使用索引。

vi. 盡量使用exists代替select count(1)來判斷是否存在記錄,count函數(shù)只有在統(tǒng)計(jì)表中所有行數(shù)時(shí)使用,而且count(1)比count(*)更有效率。

vii. 盡量使用“>=”,不要使用“>”。

viii. 注意一些or子句和union子句之間的替換

ix. 注意表之間連接的數(shù)據(jù)類型,避免不同類型數(shù)據(jù)之間的連接。

x. 注意存儲(chǔ)過程中參數(shù)和數(shù)據(jù)類型的關(guān)系。

xi. 注意insert、update操作的數(shù)據(jù)量,防止與其他應(yīng)用沖突。如果數(shù)據(jù)量超過200個(gè)數(shù)據(jù)頁面(400k),那么系統(tǒng)將會(huì)進(jìn)行鎖升級(jí),頁級(jí)鎖會(huì)升級(jí)成表級(jí)鎖。

b) 索引的使用規(guī)范:

i. 索引的創(chuàng)建要與應(yīng)用結(jié)合考慮,建議大的OLTP表不要超過6個(gè)索引。

ii. 盡可能的使用索引字段作為查詢條件,尤其是聚簇索引,必要時(shí)可以通過index index_name來強(qiáng)制指定索引

iii. 避免對(duì)大表查詢時(shí)進(jìn)行table scan,必要時(shí)考慮新建索引。

iv. 在使用索引字段作為條件時(shí),如果該索引是聯(lián)合索引,那么必須使用到該索引中的第一個(gè)字段作為條件時(shí)才能保證系統(tǒng)使用該索引,否則該索引將不會(huì)被使用。

v. 要注意索引的維護(hù),周期性重建索引,重新編譯存儲(chǔ)過程。

c) tempdb的使用規(guī)范:

i. 盡量避免使用distinct、order by、group by、having、join、***pute,因?yàn)檫@些語句會(huì)加重tempdb的負(fù)擔(dān)。

ii. 避免頻繁創(chuàng)建和刪除臨時(shí)表,減少系統(tǒng)表資源的消耗。

iii. 在新建臨時(shí)表時(shí),如果一次性插入數(shù)據(jù)量很大,那么可以使用select into代替create table,避免log,提高速度;如果數(shù)據(jù)量不大,為了緩和系統(tǒng)表的資源,建議先create table,然后insert。

iv. 如果臨時(shí)表的數(shù)據(jù)量較大,需要建立索引,那么應(yīng)該將創(chuàng)建臨時(shí)表和建立索引的過程放在單獨(dú)一個(gè)子存儲(chǔ)過程中,這樣才能保證系統(tǒng)能夠很好的使用到該臨時(shí)表的索引。

v. 如果使用到了臨時(shí)表,在存儲(chǔ)過程的最后務(wù)必將所有的臨時(shí)表顯式刪除,先truncate table,然后drop table,這樣可以避免系統(tǒng)表的較長(zhǎng)時(shí)間鎖定。

vi. 慎用大的臨時(shí)表與其他大表的連接查詢和修改,減低系統(tǒng)表負(fù)擔(dān),因?yàn)檫@種操作會(huì)在一條語句中多次使用tempdb的系統(tǒng)表。

d) 合理的算法使用:

 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 广州市| 青浦区| 左权县| 永登县| 二手房| 荔波县| 瑞丽市| 城口县| 分宜县| 承德县| 临沭县| 长宁县| 苗栗县| 沾化县| 仁化县| 横峰县| 海口市| 图木舒克市| 徐水县| 方正县| 时尚| 盐源县| 林口县| 揭阳市| 平陆县| 鸡西市| 五莲县| 龙州县| 黔江区| 华阴市| 和林格尔县| 延长县| 东源县| 上思县| 临清市| 东乡| 陆河县| 女性| 泉州市| 泾源县| 郓城县|