資料庫設計及開發規範,sql效能優化

2021-09-20 08:42:50 字數 4923 閱讀 8545

本文件用於規範資料庫設計、開發等方面的內容。

本文件的預期讀者為本專案組全體成員,以及其他與專案有關的管理人員。

omp:operation management platform

《資料庫編碼規範.pdf》

資料庫物件的命名規則的範圍為管理平台設計開發所涉及的表,對於其他外部系統所建立的表不在本規範約束範圍內,

資料庫物件如表、列、序列、過程、函式等在命名時要遵循如下規則:

圖例-1.png

2.1.2.1 建表的引數設計

2.1.2.2 主外來鍵設計

2.1.2.3 列設計

2.1.2.4 臨時表

分割槽規則圖

變數的命名體現其作用域和資料型別,規則如下:

2.2.2.1 sql書寫規範

begin

for l_count in 1..10 loop

x_result := x_result + l_count;

end loop;

end;

常數符號要大寫,比如:

constraint g_max_value number :=10;

... if(1_value > g_max_value)

then

...

2.2.2.2 索引與分割槽使用規範

2.2.2.3 select列和where條件規範

2.2.2.4 多表連線規範

#低效:

select distinct dept_no,dept_name

from dept d,emp e

where d.dept_no = e.dept_no

#高效:

select dept_no,dept_name

from dept d

where exists ( select 'x'

from emp e

where e.dept_no = d.dept_no);

2.2.2.5 巢狀查詢規範
#例如:

select …

from emp

where dept_no not in (select dept_no

from dept

where dept_cat='a');

#為了提高效率,改寫成:

select ….

from emp e

where not exists (select 'x'

from dept d

where d.dept_no = e.dept_no

and dept_cat = 'a');

2.2.3.1 包規範

2.2.3.2 游標規範

2.2.3.3 事務處理規範

2.2.3.4 資料封裝規範

2.2.3.5 資料訪問規範

2.2.3.6 日誌書寫規範

2.2.3.7 錯誤處理規範

2.2.3.8 書寫規範

2.2.3.9 書寫優化效能建議

2.2.3.10 其他經驗性規則

#錯誤寫法

select ......

from emp

where dept_no not in ( select dept_no

from dept

where dept_cat='a');

#正確寫法

select ......

from emp e

where not exists ( select 'x'

from dept

where dept_no=e.dept_no

and dept_cat='a');

#錯誤寫法

select ......

from emp

where exists ( select 'x'

from dept

where dept_no=e.dept_no

and dept_cat='a');

#正確寫法

select ......

from emp e,dept d

where e.dept_no=d.dept_no

and dept_cat='a';

#錯誤寫法

select distinct d.dept_code,d.dept_name

from dept d ,emp e

where e.dept_code=d.dept_code;

#正確寫法

select dept_code,dept_name

from dept d

where exists ( select 'x'

from emp e

where e.dept_code=d.dept_code);

select sys_guid() from dual
sys_guid(),是oracle 8i 後提供的函式。sys_guid產生並返回乙個全球唯一的識別符號(原始值)由16個位元組組成,在oracle 9i和oracle 10g生成的是32個位元組。在大多數平台,生成的識別符號由主機標符,執行函式的程序或者執行緒識別符號,和程序或執行緒的乙個非重複的值(位元組序列)組成。

可以用來生成唯一標識id;

select uuid() from dual
mysql 實現了 uuid,並且提供 uuid() 函式方便使用者生成 uuid。在 mysql 的 uuid() 函式中,前三組數字從時間戳中生成,第四組數字暫時保持時間戳的唯一性,第五組數字是乙個 ieee 802 節點標點值,保證空間唯一。使用 uuid() 函式,可以生成時間、空間上都獨一無二的值。據說只要是使用了 uuid,都不可能看到兩個重複的 uuid 值。當然,這個只是在理論情況下。

比較:本質上都是方便使用者生成隨機的唯一索引,sys_guid()生成的是32位的位元組;uuid()生成的則是帶4根- 的36位的位元組。

在關係型資料庫中,左連線使用 left join ……on……,右連線使用 right join ……on……

select t1.* from table1 t1 left join table2 t2 on t1.id = t2.id
select t2.* from table1 t1 right join table2 t2 on t1.id = t2.id
但是在oracle中卻有一種簡化寫法,採用 += 、=+ 來替代左連線和右連線,寫法如下

select t1.* from table1 t1 , table2 t2 t1.id += t2.id
select t2.* from table1 t1 , table2 t2 t1.id =+ t2.id
在mysql中判斷null函式並且替換null的函式是ifnull(exp1,result)

select ifnull(t1.user_name,'張三') as user_name from table1 t1
在oracle中判斷null函式並且替換null的函式是nvl(exp1,result)

在mysql中條件判斷語句可以使用 if(exp1,exp2,exp3)函式,缺陷是只能判斷單重條件

select 

if(t1.user_name='',"zhang san",t1.user_name) as user_name

from table1 t1

在oracle中條件判斷語句可以使用 decode(exp1,result1,exp2,result2,result3)函式,可支援多重判斷

select 

decode(t1.user_name='',"zhang san",t1.user_name is null,'li si',t1.user_name) as user_name

from table1 t1

對於比較複雜的多重條件判斷,推薦使用通用的 case when 條件 then 結果 when 條件 then 結果 else 結果 end 這種形式

select 

case when t1.user_name=''then

"zhang san"

when t1.user_name is null then

"li si"

else t1.user_name end as user_name

from table1 t1

字串拼接是sql中常用到,在mysql中提供了concat(str1,str2,…strn)和concat_ws(separator,str1,str2,...)

#無新增任何字元拼接

select concat(column1,column2,……) as column_sttr from table1;

#各個字串之間以'-'拼接

select concat_ws('-',column1,column2,……) as column_sttr from table1;

在oracle中,字元拼接是以 "||" 方式,此種方式顯得更靈活點

select column1 ||column2 || column3 as column_sttr from table1;

資料庫SQL開發規範

1 mysql中,varchar n 中的n代表的是字元數,而不是位元組數。例如varchar 255 表示可以儲存255的中文 2 過大的長度會消耗更多的記憶體。varchar n 儲存時是按照資料實際長度儲存的。當把資料讀入到記憶體時,為了提高效率,是按照n的長度分配記憶體的。3 盡可能將所有列...

資料庫設計及日常使用規範 資料庫設計

以下內容 於公司dba的分享,做個記錄,便於日後自己翻閱,也分享出來共同學習,如果有不合理的或不完善,也請指出。例 alter table table a add a alter table table a add b 合併 alter table table a 下圖為日期型別所占用位元組數 例如...

資料庫命名及設計規範

1.資料庫涉及字元規範 我們約定 採用26個英文本母 區分大小寫 和0 9這十個自然數,加上下劃線 組成,共63個字元。不能出現其他字元 注釋除外 2.資料庫物件命名規範 我們約定,資料庫物件包括表 檢視 查詢 儲存過程 引數查詢 函式 約束。物件名字由字首和實際名字組成,長度不超過30。字首 使用...