1.多where,少having
where用來過濾行,having用來過濾組
2.多union all,少union
union刪除了重復(fù)的行,因此花費(fèi)了一些時(shí)間
3.多Exists,少in
Exists只檢查存在性,性能比in強(qiáng)很多,有些朋友不會(huì)用Exists,就舉個(gè)例子
例,想要得到有電話號(hào)碼的人的基本信息,table2有冗余信息
select * from table1;--(id,name,age)
select * from table2;--(id,phone)
in:
select * from table1 t1 where t1.id in (select t2.id from table2 t2 where t1.id=t2.id);
Exists:
select * from table1 t1 where Exists (select 1 from table2 t2 where t1.id=t2.id);
4.使用綁定變量
Oracle數(shù)據(jù)庫軟件會(huì)緩存已經(jīng)執(zhí)行的sql語句,復(fù)用該語句可以減少執(zhí)行時(shí)間。
復(fù)用是有條件的,sql語句必須相同
問:怎樣算不同?
答:隨便什么不同都算不同,不管什么空格啊,大小寫什么的,都是不同的
想要復(fù)用語句,建議使用PreparedStatement
將語句寫成如下形式:
insert into XXX(pk_id,column1) values(?,?);
update XXX set column1=? where pk_id=?;
delete from XXX where pk_id=?;
select pk_id,column1 from XXX where pk_id=?;
5.少用*
很多朋友很喜歡用*,比如:select * from XXX;
一般來說,并不需要所有的數(shù)據(jù),只需要一些,有的僅僅需要1個(gè)2個(gè),
拿5W的數(shù)據(jù)量,10個(gè)屬性來測試:
(這里的時(shí)間指的是PL/SQL Developer顯示所有數(shù)據(jù)的時(shí)間)
使用select * from XXX;平均需要20秒,
使用select column1,column2 from XXX;平均需要12秒
(我的機(jī)子不是很好。。。)
對(duì)于開發(fā)來說,這一條是個(gè)災(zāi)難,知道是一回事,做就是另一回事了
6.分頁sql
一般的分頁sql如下所示:
sql1:select * from (select t.*,rownum rn from XXX t)where rn>0 and rn <10;
sql2:select * from (select t.*,rownum rn from XXX t where rownum <10)where rn>0;
乍看一下沒什么區(qū)別,實(shí)際上區(qū)別很大...125萬條數(shù)據(jù)測試,
sql1平均需要1.25秒(咋這么準(zhǔn)呢? )
sql2平均需要... 0.07秒
原因在于,子查詢中,sql2排除了10以外的所有數(shù)據(jù)
當(dāng)然了,如果查詢最后10條,那效率是一樣的
7.能用一句sql,千萬別用2句sql
不解釋
where用來過濾行,having用來過濾組
2.多union all,少union
union刪除了重復(fù)的行,因此花費(fèi)了一些時(shí)間
3.多Exists,少in
Exists只檢查存在性,性能比in強(qiáng)很多,有些朋友不會(huì)用Exists,就舉個(gè)例子
例,想要得到有電話號(hào)碼的人的基本信息,table2有冗余信息
select * from table1;--(id,name,age)
select * from table2;--(id,phone)
in:
select * from table1 t1 where t1.id in (select t2.id from table2 t2 where t1.id=t2.id);
Exists:
select * from table1 t1 where Exists (select 1 from table2 t2 where t1.id=t2.id);
4.使用綁定變量
Oracle數(shù)據(jù)庫軟件會(huì)緩存已經(jīng)執(zhí)行的sql語句,復(fù)用該語句可以減少執(zhí)行時(shí)間。
復(fù)用是有條件的,sql語句必須相同
問:怎樣算不同?
答:隨便什么不同都算不同,不管什么空格啊,大小寫什么的,都是不同的
想要復(fù)用語句,建議使用PreparedStatement
將語句寫成如下形式:
insert into XXX(pk_id,column1) values(?,?);
update XXX set column1=? where pk_id=?;
delete from XXX where pk_id=?;
select pk_id,column1 from XXX where pk_id=?;
5.少用*
很多朋友很喜歡用*,比如:select * from XXX;
一般來說,并不需要所有的數(shù)據(jù),只需要一些,有的僅僅需要1個(gè)2個(gè),
拿5W的數(shù)據(jù)量,10個(gè)屬性來測試:
(這里的時(shí)間指的是PL/SQL Developer顯示所有數(shù)據(jù)的時(shí)間)
使用select * from XXX;平均需要20秒,
使用select column1,column2 from XXX;平均需要12秒
(我的機(jī)子不是很好。。。)
對(duì)于開發(fā)來說,這一條是個(gè)災(zāi)難,知道是一回事,做就是另一回事了
6.分頁sql
一般的分頁sql如下所示:
sql1:select * from (select t.*,rownum rn from XXX t)where rn>0 and rn <10;
sql2:select * from (select t.*,rownum rn from XXX t where rownum <10)where rn>0;
乍看一下沒什么區(qū)別,實(shí)際上區(qū)別很大...125萬條數(shù)據(jù)測試,
sql1平均需要1.25秒(咋這么準(zhǔn)呢? )
sql2平均需要... 0.07秒
原因在于,子查詢中,sql2排除了10以外的所有數(shù)據(jù)
當(dāng)然了,如果查詢最后10條,那效率是一樣的
7.能用一句sql,千萬別用2句sql
不解釋
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
