常用SQL語句優(yōu)化技巧有哪些-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)常用SQL語句優(yōu)化技巧有哪些,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為秦皇島企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),秦皇島網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

具體如下:

除了建立索引之外,保持良好的SQL語句編寫習(xí)慣將會(huì)降低SQL性能問題發(fā)生。

①通過變量的方式來設(shè)置參數(shù)

好:

stringsql = "select * from people p where p.id = ? ";

壞:

stringsql = "select * from people p where p.id = "+id;

數(shù)據(jù)庫的SQL文解析和執(zhí)行計(jì)劃會(huì)保存在緩存中,但是SQL文只要有變化,就得重新解析。

“…where p.id = ”+id的方式在id值發(fā)生改變時(shí)需要重新解析,這會(huì)耗費(fèi)時(shí)間。

②不要使用select *

好:

stringsql = "select people_name,pepole_age from people ";

壞:

stringsql = "select * from people ";

使用select *的話會(huì)增加解析的時(shí)間,另外會(huì)把不需要的數(shù)據(jù)也給查詢出來,數(shù)據(jù)傳輸也是耗費(fèi)時(shí)間的,

比如text類型的字段通常用來保存一些內(nèi)容比較繁雜的東西,如果使用select *則會(huì)把該字段也查詢出來。

③謹(jǐn)慎使用模糊查詢

好:

stringsql = "select * from people p where p.id like 'parm1%' ";

壞:

stringsql = "select * from people p where p.id like '%parm1%' ";

當(dāng)模糊匹配以%開頭時(shí),該列索引將失效,若不以%開頭,該列索引有效。

④不要使用列號(hào)

好:

stringsql = "select people_name,pepole_age from people order by name,age";

壞:

stringsql = "select people_name,pepole_age from people order by 6,8";

使用列號(hào)的話,將會(huì)增加不必要的解析時(shí)間。

⑤優(yōu)先使用UNION ALL,避免使用UNION

好:

stringsql = "select name from student union all select name from teacher";

壞:

stringsql = "select name from student union select name from teacher";

UNION 因?yàn)闀?huì)將各查詢子集的記錄做比較,故比起UNION ALL ,通常速度都會(huì)慢上許多。一般來說,如果使用UNION ALL能滿足要求的話,務(wù)必使用UNION ALL。還有一種情況,如果業(yè)務(wù)上能夠確保不會(huì)出現(xiàn)重復(fù)記錄。

⑥在where語句或者order by語句中避免對(duì)索引字段進(jìn)行計(jì)算操作

好:

stringsql = "select people_name,pepole_age from people where create_date=date1 ";

壞:

stringsql = "select people_name,pepole_age from people where trunc(create_date)=date1";

當(dāng)在索引列上進(jìn)行操作之后,索引將會(huì)失效。正確做法應(yīng)該是將值計(jì)算好再傳入進(jìn)來。

⑦使用not exist代替not in

好:

stringsql = "select * from orders where customer_name not exist (select customer_name from customer)";

壞:

stringsql = "select * from orders where customer_name not in(select customer_name from customer)";

如果查詢語句使用了not in 那么內(nèi)外表都進(jìn)行全表掃描,沒有用到索引;而not extsts 的子查詢依然能用到表上的索引。

⑧ exist和in的區(qū)別

in 是把外表和內(nèi)表作hash 連接,而exists是對(duì)外表作loop循環(huán),每次loop循環(huán)再對(duì)內(nèi)表進(jìn)行查詢。因此,in用到的是外表的索引, exists用到的是內(nèi)表的索引。

如果查詢的兩個(gè)表大小相當(dāng),那么用in和exists差別不大。

如果兩個(gè)表中一個(gè)較小,一個(gè)是大表,則子查詢表大的用exists,子查詢表小的用in:

例如:表A(小表),表B(大表)

1:

select * from A where cc in (select cc from B)

效率低,用到了A表上cc列的索引;

select * from A where exists(select cc from B where cc=A.cc)

效率高,用到了B表上cc列的索引。

2:

select * from B where cc in (select cc from A)

效率高,用到了B表上cc列的索引;

select * from B where exists(select cc from A where cc=B.cc)

效率低,用到了A表上cc列的索引。

⑨避免在索引列上做如下操作:

◆避免在索引字段上使用<>,!=
◆避免在索引列上使用IS NULL和IS NOT NULL
◆避免在索引列上出現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換(比如某字段是String類型,參數(shù)傳入時(shí)是int類型)

當(dāng)在索引列上使用如上操作時(shí),索引將會(huì)失效,造成全表掃描。

⑩復(fù)雜操作可以考慮適當(dāng)拆成幾步

有時(shí)候會(huì)有通過一個(gè)SQL語句來實(shí)現(xiàn)復(fù)雜業(yè)務(wù)的例子出現(xiàn),為了實(shí)現(xiàn)復(fù)雜的業(yè)務(wù),嵌套多級(jí)子查詢。造成SQL性能問題。對(duì)于這種情況可以考慮拆分SQL,通過多個(gè)SQL語句實(shí)現(xiàn),或者把部分程序能完成的工作交給程序完成。

關(guān)于“常用SQL語句優(yōu)化技巧有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)站欄目:常用SQL語句優(yōu)化技巧有哪些-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://bm7419.com/article46/cechhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、企業(yè)建站、網(wǎng)站收錄微信小程序、營銷型網(wǎng)站建設(shè)靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)