oracle分区和锁的难,oracle使用三(锁和表分区)
行級(jí)鎖只對(duì)用戶正在訪問(wèn)的行進(jìn)行鎖定
授權(quán)用戶:
connect??yyaccp/acccp;
create?user?user2?identified?by?chenmp;
grant?create?session?to?chenmp;
grant?select?,insert,update,delete?on?order?testlock?to?chenmp;
以下代碼演示鎖定表中的記錄:
connect?chenmp/chenmp?;
update?yyaccp.testlock?set?name=''wrong''?where?id=8;
重新打開(kāi)?SQL/PLUS
connect?yyaccp/yyaccp?;
update?testlock?set?set?name=’jeff?wrong’?where?id?=8
Wait?子句使用方法
Connect??chenmp/chenmp;
select?*?from?yyaccp.testlock?where?id=8?for?update;
重新打開(kāi)SQL/PLUS
Connect??yyaccp/accp;
select?*?from?testlock?where?id=8?for?update?wait?5;
***?注意?wait?只能用在?select?語(yǔ)句上
FOR?UPDATE?NOWAIT?使用
select?*?from?testlock?where?id=8?for?update?nowait;
2.表級(jí)鎖
以下代碼演示了鎖定?testlock?表?:
Connect??chenmp/chenmp;
lock?table?yyaccp.testlock?in?share?mode;
重新打開(kāi)SQL/PLUS
Connect??yyaccp/accp;
delete?from?testlock;
SQL/PLUS?停止響應(yīng)(在第一個(gè)?SQL/PLUS?中?commit;)
如果多個(gè)用戶鎖定一個(gè)表那么所有的用戶都不能在此表上進(jìn)行更新操作,只有當(dāng)所有用戶執(zhí)行?commit命令或者?rollback?命令結(jié)束事務(wù)時(shí),鎖才會(huì)被釋放。
例如:?connect?yyaccp/accp;
Lock?table?testlock?in?share?mode;
Connect?chenmp/chenmp;
Lock?table?testlock?in?share?mode;
Connect?user1/user1;
Update?testlock?set?name?=’ping’?where?id=8;
當(dāng)user1?執(zhí)行操作時(shí),sql/plus?沒(méi)有響應(yīng),必須要把?yyaccp?用戶和?chenmp?用戶解鎖后才能進(jìn)行操作?。
以下代碼演示了以行共享的模式鎖定?order_master?表
Connect??chenmp/chenmp;
lock?table?yyaccp.testlock?in?row?share?mode;
輸入以下命令更新?testlock?表
update?yyaccp.testlock?set?name=?''ping''?where?id=8;
以上語(yǔ)句成功執(zhí)行更新。
重新打開(kāi)一個(gè)?sqlplus?窗口
Connect?yyaccp/accp;
update?yyaccp.testlock?set?name=?''ping''?where?id=8;
以上代碼不能做更新操作.
update?yyaccp.testlock?set?name=?''wrong''?where?id=7;
以上代碼可以做更新操作
獨(dú)占鎖:
lock?table?yyaccp.testlock?in?exclusive?mode?nowait;
:范圍分區(qū)根據(jù)表的某個(gè)列或一組列的值范圍,決定將該數(shù)據(jù)存儲(chǔ)在哪個(gè)分區(qū)上。可以根據(jù)序號(hào)分區(qū),根據(jù)業(yè)務(wù)數(shù)據(jù)的產(chǎn)生日期分區(qū)等。
以下代碼創(chuàng)建了sales表并且對(duì)數(shù)據(jù)進(jìn)行了分區(qū)
drop?table?employeeinfo?;
Create?table?employeeinfo?(
empid?number(5),
empName?varchar2(20)?not?null,
age?number(10)
)
Partition?by?range?(age)
(
Partition??p1?values?less?than?(20),
Partition??p2?values?less?than?(30),
Partition??p3?values?less?than?(40),
);
以下代碼演示了根據(jù)年份進(jìn)行分區(qū)
drop?table?personinfo?cascade;
Create?table?personinfo?(
personid??number(5),
personName?varchar2(20),
Birthday??date?not?null
)
partition?by?rang(Birthday?)
(
Partition??p1?values?less?than(to_date(‘1978-10-10’,’yyyy-mm-dd’)),
Partition??p2?values?less?than(to_date(‘1980-5-1’,’yyyy-mm-dd’)),
Partition??p3?values?less?than(MAXVALUE)
);
散列分區(qū)通過(guò)在分區(qū)鍵值上執(zhí)行一個(gè)散列函數(shù)來(lái)決定數(shù)據(jù)的物理位置。連續(xù)的分區(qū)鍵不必存儲(chǔ)在相同的分區(qū)中。散列分區(qū)把記錄平均地分布到不同地分區(qū),減少了磁盤(pán)I/O爭(zhēng)用地可能性。
以下代碼創(chuàng)建了名為MY_EMP表,并分為2個(gè)散列分區(qū)。
drop?table?my_emp?cascade;
create?table?my_emp(
Empno?number(4),
Ename?varchar2(15)
)
Partition??by??hash(empno)
(
Partition??part_1,
Partition??part_2
);
復(fù)合分區(qū)是散列分區(qū)和范圍分區(qū)地結(jié)合,在創(chuàng)建復(fù)合分區(qū)時(shí),先根據(jù)范圍對(duì)數(shù)據(jù)進(jìn)行分區(qū),然后在這些分區(qū)內(nèi)創(chuàng)建散列子分區(qū)。復(fù)合分區(qū)既具備范圍分區(qū)便于管理地優(yōu)點(diǎn),又具備散列分區(qū)在數(shù)據(jù)放置和并行操作方面地優(yōu)點(diǎn)。
drop?table?employees?cascade;
create?table?employees(
emp_id?varchar2(5),
work_date?date?not?null,
salary?number(10)
)
Partition?by?range?(salary)
Subpartition?by?hash?(emp_id)
Subpartitions?5
(
Partition??p1?values?less?than?(1500),
Partition??p2?values?less?than?(2000),
Partition??p3?values?less?than?(maxvalue)
);
列表分區(qū)允許用戶明確地控制行到分區(qū)地映射,列表分區(qū)允許按自然方式對(duì)無(wú)序和不相關(guān)的數(shù)據(jù)集進(jìn)行分組和組織。
drop?table?product?cascade;
Create?table?product
(
product_id??number?(4)?,
product_type?varchar2?(20)?,
product_name??varchar2?(50)
)
Partition?by?list?(product_type)
(
Partition??p1?values(''book''),
Partition??p2?values(''cd'',''dvd'')
)
分區(qū)維護(hù)操作包括:添加分區(qū),刪除分區(qū),截?cái)喾謪^(qū),合并分區(qū),拆分分區(qū)。
添加分區(qū):
以下代碼演示了向sales?表添加一個(gè)分區(qū)。
Alter?table?my_emp??add?partition??part_3?;
以下代碼演示了刪除?sales?表中的p4?分區(qū)
Alter?table?sales?drop?partition??p4;
***?注意:分區(qū)刪除后數(shù)據(jù)庫(kù)表中對(duì)應(yīng)的數(shù)據(jù)也刪除了。
截?cái)喾謪^(qū)將刪除表分區(qū)中的所有記錄,以下代碼演示了如何刪除sales?表中p3分區(qū)的記錄
Alter?table?sales?truncate?partition?p3;
合并分區(qū)可以將范圍分區(qū)表或復(fù)合分區(qū)表的兩個(gè)相鄰分區(qū)連接起來(lái),結(jié)果分區(qū)將繼承被合并的兩個(gè)分區(qū)的較高上界。下列代碼演示了將sales?表中的P1和P2兩個(gè)分區(qū)合并成一個(gè)P2?分區(qū)。
alter?table?employeeinfo?merge?partitions?p1,p2?into?partition?p2;
拆分分區(qū)允許用戶將一個(gè)分區(qū)拆分為兩個(gè)。當(dāng)分區(qū)過(guò)大,可以對(duì)分區(qū)進(jìn)行拆分。以下代碼演示了將?sales?表中的?P2?分區(qū)拆分為?P1和P2兩個(gè)分區(qū)。
alter?table?employeeinfo?split?partition?p2?at(20)?into?(partition?p1,partition?p2);
重命名分區(qū):以下代碼演示了將sales?表中的?p1?分區(qū)重命名為:p5
Alter??table??salse??rename??partition??p1??to??p5;
表分區(qū)數(shù)據(jù)字典:
User_tab_partitions;
User_ind_partitions;
總結(jié)
以上是生活随笔為你收集整理的oracle分区和锁的难,oracle使用三(锁和表分区)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle自带调优,oracle 参数
- 下一篇: oracle语句求保有率,Oracle之