資料庫學習 寫mysql語句經驗談

2021-06-25 10:19:35 字數 3996 閱讀 8839



1.

不論乙個

sql中涉及到多個表,每次都用兩個表(結果集)操作,得到新的結果後,再和下乙個表(結果集)操作。

2.避免在

select

f1,(

select

f2 from

tableb

)....

from

tablea

這樣得到欄位列。直接用

tablea

和tableb

關聯得到a.

f1,b.

f2就可以了。

3.避免隱含的型別轉換

如select

id from

employee

where

emp_id

='8'

(錯)select

id from

employee

where

emp_id=8

(對)emp_id

是整數型,用

'8'會預設啟動型別轉換,增加查詢的開銷。

4.儘量減少使用正規表示式,盡量不使用萬用字元。

5.

使用關鍵字代替函式

如:select

id from

employee

where

upper

(dept

)like

'tech_db'

(錯)select

id from

employee

where

substr

(dept,1

,4)='tech'

(錯)select

id from

employee

where

dept like

'tech%'

(對)6.

不要在字段上用轉換函式,盡量在常量上用

如:select

id from

employee

where

to_char

(create_date

,'yyyy-mm-dd'

)='2012-10-31'

(錯)select

id from

employee

where

create_date

=to_date

('2012-10-31'

,'yyyy-mm-dd'

)(對)

7.不使用聯接做查詢

如:select

id from

employee

where

first_name

||last_name like

'jo%'

(錯)8.

盡量避免前後都用萬用字元

如:select

id from

employee

where

dept like

'%tech%'

(錯)select

id from

employee

where

dept like

'tech%'

(對)9.

判斷條件順序

如:select

id from

employee

where

creat_date-30

>

to_date

('2012-10-31'

,'yyyy-mm-dd'

)(錯)

select

id from

employee

where

creat_date

>

to_date

('2012-10-31'

,'yyyy-mm-dd'

)+30

(對)10.

盡量使用

exists

而非in

當然這個也要根據記錄的情況來定用

exists

還是用in

,通常的情況是用

exists

select

id from

employee

where

salary in(

select

salary

from

emp_level

where

....)

(錯)select

id from

employee

where

salary exists

(select

'x'from

emp_level

where

....)

(對)11.

使用not

exists

而非not

in和上面的類似

12.減少查詢表的記錄數範圍

13.正確使用索引

索引可以提高速度,一般來說,選擇度越高,索引的效率越高。

14.索引型別

唯一索引,對於查詢用到的字段,盡可能使用唯一索引。

還有一些其他型別,如位圖索引,在性別字段,只有男女的字段上用。

15.在經常進行連線,但是沒有指定為外來鍵的列上建立索引

16.在頻繁進行排序會分組的列上建立索引,如經常做

groupby或

order

by操作的字段。

17.在條件表示式中經常用到的不同值較多的列上建立檢索,在不同值少的列上不建立索引。如性別列上只有男,女兩個不同的值,就沒必要建立索引(或建立位圖索引)。如果建立索引不但不會提高查詢效率,反而會嚴重降低更新速度。

18.在值比較少的字段做

order

by時,翻頁會出現記錄紊亂問題,要帶上

id欄位一起做

order by.

19.不要使用空字串進行查詢

如:select

id from

employee

where

emp_name like

'%%'

(錯)20.

盡量對經常用作

group

by的關鍵字段做索引。

21.正確使用表關聯

利用外連線替換效率十分低下的

notin

運算,大大提高執行速度。

如:selecta.

id from

employee a

wherea.

emp_no

notin

(select

emp_no

from

employee1

where

job

='sale'

)(錯)

22.使用臨時表

在必要的情況下,為減少讀取次數,可以使用經過索引的臨時表加快速度。

如:selecte.

id from

employee e

,dept d

wheree.

dept_id=d

.id

ande

.empno

>

1000

order bye

.id

(錯)selectid,

empno

from

employee

into

temp_empl

where

empno

>

1000

order

byid

selectm.

id from

temp_emp1 m

,dept d

wherem.

empno=d

.id

(對)

資料庫學習 mysql 分頁查詢語句

單條 sql 語句的分頁 sql 方法1 適用於 sql server 2000 2005 select top 頁大小 from table1 where id not in select top 頁大小 頁數 1 id from table1 order by id order by id 方法...

MySQL資料庫查詢語句記錄學習

以上圖資料庫表為例,表名moncenter data air,表結構如下圖 1.查詢今天資料指令 select datatime from moncenter data air where to days datatime to days now 2.查詢昨天資料指令 select datatime...

mysql資料庫索引語句 MySQL資料庫之索引

一 什麼是索引 索引是一種用於快速查詢到匹配條件的資料的資料結構,是用來加快查詢的技術。索引對良好的資料庫效能來說,是乙個非常重要的指標。當表中的資料量越來越大的時,其索引就越來越重要。基本法則 索引應該構建在被用作 查詢條件 的字段上 索引型別 1 b tree索引 btree樹的特性 多路平衡樹...