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

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

常用SQL語句優化技巧總結【經典】

2020-10-29 21:47:48
字體:
來源:轉載
供稿:網友

本文實例總結了常用SQL語句優化技巧。分享給大家供大家參考,具體如下:

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

①通過變量的方式來設置參數

好:

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

壞:

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

數據庫的SQL文解析和執行計劃會保存在緩存中,但是SQL文只要有變化,就得重新解析。

“…where p.id = ”+id的方式在id值發生改變時需要重新解析,這會耗費時間。

②不要使用select *

好:

stringsql = "select people_name,pepole_age from people ";

壞:

stringsql = "select * from people ";

使用select *的話會增加解析的時間,另外會把不需要的數據也給查詢出來,數據傳輸也是耗費時間的,

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

③謹慎使用模糊查詢

好:

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

壞:

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

當模糊匹配以%開頭時,該列索引將失效,若不以%開頭,該列索引有效。

④不要使用列號

好:

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

壞:

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

使用列號的話,將會增加不必要的解析時間。

⑤優先使用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 因為會將各查詢子集的記錄做比較,故比起UNION ALL ,通常速度都會慢上許多。一般來說,如果使用UNION ALL能滿足要求的話,務必使用UNION ALL。還有一種情況,如果業務上能夠確保不會出現重復記錄。

⑥在where語句或者order by語句中避免對索引字段進行計算操作

好:

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";

當在索引列上進行操作之后,索引將會失效。正確做法應該是將值計算好再傳入進來。

⑦使用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 那么內外表都進行全表掃描,沒有用到索引;而not extsts 的子查詢依然能用到表上的索引。

⑧ exist和in的區別

in 是把外表和內表作hash 連接,而exists是對外表作loop循環,每次loop循環再對內表進行查詢。因此,in用到的是外表的索引, exists用到的是內表的索引。

如果查詢的兩個表大小相當,那么用in和exists差別不大。

如果兩個表中一個較小,一個是大表,則子查詢表大的用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
◆避免在索引列上出現數據類型轉換(比如某字段是String類型,參數傳入時是int類型)

當在索引列上使用如上操作時,索引將會失效,造成全表掃描。

⑩復雜操作可以考慮適當拆成幾步

有時候會有通過一個SQL語句來實現復雜業務的例子出現,為了實現復雜的業務,嵌套多級子查詢。造成SQL性能問題。對于這種情況可以考慮拆分SQL,通過多個SQL語句實現,或者把部分程序能完成的工作交給程序完成。

PS:這里再為大家推薦2款SQL在線工具供大家參考使用:

SQL在線壓縮/格式化工具:
http://tools.VeVB.COm/code/sql_format_compress

sql代碼在線格式化美化工具:
http://tools.VeVB.COm/code/sqlcodeformat

希望本文所述對大家數據庫程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汨罗市| 工布江达县| 侯马市| 平湖市| 威海市| 峡江县| 雅江县| 惠安县| 兰溪市| 工布江达县| 桃江县| 敦化市| 三明市| 石嘴山市| 汕头市| 湖南省| 莆田市| 晋江市| 泸水县| 屏东市| 邳州市| 永兴县| 长海县| 高平市| 彩票| 桐柏县| 太仆寺旗| 灯塔市| 固原市| 修武县| 大安市| 永城市| 若尔盖县| 合肥市| 中西区| 永和县| 鲁山县| 利川市| 通辽市| 上饶县| 革吉县|