Oracle查詢優化改寫 抄書學習02

2021-10-02 19:52:04 字數 3254 閱讀 7311

3.10 聚集和內連線

員工獎金根據type計算,type =1 的為10%,type=2 的wei20%,以此類推

先把獎金跟員工彙總,在於員工表關聯。

select e.deptno

sum(e.sal) as total_sal

sum(e.sal * eb2.rate)as total_bonus

from e

inner join (select eb.empno,

sum(

case when eb.type =1 then 0.1

when eb.type =2 then 0.2

when eb,type= 3 then 0.3

end )

as rate

from emp_bonus eb group by eb.empno )

eb2 on eb2.empno =e.empno

where e.deptno =10

group by e.deptno;

3.11 聚集和外連線

3.12 從多個表返回多餘的資料

4.3複製表的定義及資料

create table test2 as select * from test;

也可以想複製表的定義,在新增資料;

create table test2 asselect * from test where 1=2;

(注意;複製的表不包含預設值等約束資訊,是用這種方式複製表後,需要重建預設值急索引和約束等資訊)

4.4用with check option 限制資料錄入

當約束比較簡單時,可以直接在表裡新增約束條件如

alter table emp add constraints ch_sal check(sal>0);

但是有些複雜或特殊的約束條件是不能讓這樣放在表裡的。

4.5多表插入語句

create table t2 (d varchar2(10),des varchar2(50));

create table t1 as select

『1, 2323』 as d1,

『2,2223』 as d2,

from dual

/轉置insert/

insert all (把不同列的資料插入到同乙個表的不同行中)

into t2 (d.des) values (『周一』,d1)

into t2 (d.des) values (『周二』,d2)

4.7merge into 用其他表裡的值更新

merge into emp

using (select danme ,deptno from dept where dept.name in (『accounting』,

『research』)) dept

on (dept.deptno =emp.deptno)

when match then

update set emp.name = dept.name

4.8 刪除重複資料的記錄

三種方式

第一種;通過name相同,id不同的方式來判斷,利用這種方式刪除書庫時需要建立組合索引

create index idx_name_id on dupes (name,id)

delete from dupes a where exists (select null from dupes b where b.name =a.name and b.id>a.id);

方法二: 用rowid來代替其中的id

因為不需要關聯id列,只需要建立單列索引。

creat index idx_name on dups (name);

delete from dupes a where exists (select null from dupes b where b.name =a.name and b.id>a.id);

第三種根據name分組生成序號,刪除序號大於一的資料

select level from dual connect by level <=4;

select v.漢字,v.手拼,level from v connect by level <= length(v.漢字);

所以完全可以實現對資料庫的迴圈connect by

5.3計算字元在字串**現的次數

怎麼計算在『,』號的個數在後面加1就可以

學習新函式 regexp_count 我們可以直接用

select refexp_count(str,』,』)+1 as cnt from v;

注意萬用字元轉義正則函式表示式regexp_replace 可以直接把內的列舉的字元替換為空。

用法 select enmae, regexp_replace (/*列名 ename,』aeiou』) as stripped1 from emp

資料還原,正規表示式。

測試資料

date

accounting10

research20

sales30

operations40

還原開始

select regexp_replace (data,』[0-9]』,』』) dname,

regexp_replace (data,』[^0-9]』,』』) deptno from dept2

還原結果

dname deptno

accounting 10

research 20

sales 30

operations 40

regexp_like (data,』[0-9a-za-z]+』) 就相當於like 『%數字』 or like 『%大小寫字母%』

『1+′意思

是不在方

括號裡表

示字串

開始

,' 意思是^不在方括號裡表示字串開始,

′意思是不在

方括號裡

表示字元

串開始,

符號在括號外面表示字串結束

regexp_like (data,』^a』);表示從a開始

『akatex parse error: double superscript at position 22: …ike '%a' '̲^a』=like 『a』

『2』+$ 對應搜尋可能是1,也可能是2還有11根12 不包含空(』』)

『3』*$ 應搜尋可能是1,也可能是2還有11根12 包含空(』』)也可以為null

0-9a-za-z ↩︎

12 ↩︎

12 ↩︎

Oracle查詢優化改寫 1

對應的第一章 單錶查詢 1.將空值轉換為實際值 和nvl函式相比,coalesce函式更好用,後者支援多個引數。coalesce 語法 coalesce exp1,exp2,exp3,功能 9i新增,依次查詢各引數,遇到非null則返回,各引數或表示式資料型別必須一致,如果都為null則返回null...

Oracle查詢優化改寫 2

對應的第二章 給查詢結果排序 1.按指定列排序select ename,comm from emp order by comm asc select ename,comm from emp orderby2 asc 按照第二列排序2.translate函式語法 translate string,fr...

Oracle 查詢優化器 改寫查詢語句

start 當我們執行一條查詢語句的時候,我們只告訴 oracle 我們想要哪些資料,至於資料在 怎麼找,那是查詢優化器的事情,優化器需要改寫查詢語句,決定訪問路徑 如 全表掃瞄,快速全索引掃瞄,索引掃瞄 決定表聯接順序等。至於選擇哪種方式,優化器需要根據資料字典做出判斷。那優化器如何改寫查詢語句呢...