Oracle 知識篇+分區表上的索引由global改為local注意事項

★ 知識點

二、知識點
?Local型索引有如下優點
1.Only one index partition must be rebuilt when a maintenance operation other than SPLIT PARTITION or ADD PARTITION is performed on an underlying table partition.
2.The duration of a partition maintenance operation remains proportional to partition size if the partitioned table has only local indexes.
3.Local indexes support partition independence.
4.Local indexes support smooth roll-out of old data and roll-in of new data in historical tables.
5.Oracle can take advantage of the fact that a local index is equipartitioned with the underlying table to generate better query access plans.
6.Local indexes simplify the task of tablespace incomplete recovery. To recover a partition or subpartition of a table to a point in time, you must also recover the corresponding index entries to the same point in time. The only way to accomplish this is with a local index. Then you can recover the corresponding table and index partitions or subpartitions.
?對local型索引的分區表執行truncate/drop操作會級聯維護相關分區上的索引并釋放其空間
?改造后對單獨分區的truncate/drop等部分操作不會影響其他分區
?改造后相關表上分區數據的維護/操作會更加方便快捷,如整理分區碎片/清空分區數據/刪除分區數據
?重建索引可以消除索引級的碎片

★ 注意事項

1.分區表中分區的drop/truncate操作會造成global型(非分區)索引變為不可用狀態,但可通過重建索引恢復
2.分區表中分區的drop/truncate操作會造成global型(分區)索引變為不可用狀態,但可通過重建索引恢復
3.分區表中分區的drop/truncate操作不會造成local型(分區)索引變為不可用狀態
4.Oracle要求分區表上的local型唯一/主鍵約束索引必須包含分區鍵,且分區鍵上不容許使用函數
5.唯一/主鍵約束索引由global改local型須添加分區鍵,但這會改變其唯一邏輯,因此需要考慮是否滿足業務。推薦從從業務邏輯層控制數據唯一性,或對該表唯一性無要求的情況下再進行global改local操作
global型單列主鍵		local型組合主鍵		對比說明
id(int)		sj(date)	     id(int)	local型索引須包含分區鍵(本例為sj列date類型)
1		  2023-01-01 00:00:01	1		global型主鍵id列全表只能有一個1
2		  2023-01-01 00:00:02	1		local型主鍵id列全表可以有多個1
3		  2023-01-01 00:00:02	2		local型主鍵的唯一性體現在組合的最小粒度上
6.若表中數據從來源可保證數據唯一性或對唯一性無要求時,推薦用常規索引代替唯一/主鍵約束索引
7.唯一/主鍵約束索引的狀態為UNUSABLE/INVALID等異常狀態時,一般會阻止表上數據的DML,索引重建后恢復
8.索引的修改屬于DDL操作,會對表加不同粒度的鎖,推薦在非業務期間執行,如果是7*24的業務需要酌情使用適當的方式
9.索引狀態列status有3類值:可用(USABLE/VALID)、不可用(UNUSABLE/INVALID)、未知(N/A)
10.索引相關視圖:dba_ind_subpartitions(子分區信息)、dba_ind_partitions(分區信息)、dba_indexes(總信息)
11.Oracle為local型索引創建與分區表相同數量的分區或子分區,并為它們提供與表相應分區相同的分區范圍
12.當基礎表中的分區added, dropped, merged, or split時,或hash partitions or subpartitions被添加或合并時,Oracle 還會自動維護索引分區
13.如果在分區和索引級別沒有用戶指定的TABLESPACE值,則使用基礎表的相應分區的物理屬性的值。
14.唯一約束索引升級為主鍵約束時,Oracle會給相關列設置not null約束,如果相關列不全有not null約束,Oracle會給該表加獨占鎖且鎖定時長和數據量成正比,鎖持續期間表無法增刪改查
15.重建索引時可以酌情考慮開啟并行加快重建速度,重建完成后須關閉并行
16.重建索引時可以酌情考慮是否使用online方式(適合業務繁忙的場景,速度慢但對表的鎖影響教小:僅DDL始末加一小會鎖),非online方式(適合業務不忙的場景,速度快但對表的鎖影響較大:重建索引期間一直加獨占鎖)
17.所有改造操作強烈建議進行充分評估和測試,減少投產風險
18.創建/重建索引會消耗歸檔空間,建議提前排查歸檔空間是否足夠(建議預留超過新索引大小的歸檔空間,該值為預估值,實際可能有出入,建議預留充足的空間)
19.當基礎表分區中的數據被移動或刪除(SPLIT、MOVE、DROP或TRUNCATE)時,全局索引的所有分區都會受到影響。因此,全局索引不支持分區獨立性。
20.分區表上的普通索引由global改為local型時,需要先刪除舊global索引然后新建local索引,期間表上會暫時缺少相關列的索引,如果表上有業務運行可能會因為索引缺失影響性能,建議在非相關業務期間刪除+新建。或在相關列上先建立一個臨時多列組合索引,再刪除舊global索引,然后新建local索引,最后刪除臨時索引
21.

★ 相關SQL

1.查看分區表上的global型索引
select a.owner, a.table_name, a.index_namefrom dba_part_indexes awhere a.owner = 'USER_NAME' --用戶名and a.table_name = 'TABLE_NAME' --表名and a.locality = 'GLOBAL'
union
select b.owner, b.table_name, b.index_namefrom dba_indexes bwhere b.owner = 'USER_NAME'  --用戶名and b.table_name = 'TABLE_NAME' --表名and b.partitioned = 'NO';
2.查看索引狀態(status列USABLE/VALID代表索引可用/正常)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'INDEX_NAME' --索引名
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'INDEX_NAME' --索引名
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'INDEX_NAME' --索引名
order by 1, 2, 3,4;

★ 測試案例

1.創建/重置測試表和數據
--刪表
drop table P;
--建表
create table p (id number,name varchar2(20)) partition by range (id)
(
partition p1 values less than (10) ,
partition p2 values less than (20) ,
partition p3 values less than (30) ,
partition pmax values less than (maxvalue) 
);
--插入實驗數據
insert into p values(1,'p1');
insert into p values(11,'p2');
insert into p values(21,'p3');
insert into p values(91,'pmax');
commit; 
2.分區表上global型(非分區)索引的DDL
(1)測試表重置
(2)創建索引
drop index p_global_notpartition;
create index p_global_notpartition on p(id) global online;
(3)查看索引狀態(status列應該是:VALID可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_NOTPARTITION'
order by 1, 2, 3,4;
(4)執行分區DDL操作
alter table p truncate partition p1;
(5)查看索引狀態(status列應該是:UNUSABLE不可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_NOTPARTITION'
order by 1, 2, 3,4;
(6)索引重建
alter index p_global_notpartition rebuild online;
(7)查看索引狀態(status列應該是:VALID可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_NOTPARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_NOTPARTITION'
order by 1, 2, 3,4;
(8)小結
分區表中分區的drop/truncate操作會造成global型(非分區)索引變為不可用狀態
3.分區表上global型(分區)索引的DDL
(1)測試表重置
(2)創建索引
drop index p_global_partition;
create index p_global_partition on p(id) global partition by range (id)
(
partition p1 values less than (10) ,
partition p2 values less than (20) ,
partition p3 values less than (30) ,
partition pmax values less than (maxvalue) 
) online;
(3)查看索引狀態(status列應該是:USABLE可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_PARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_PARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_PARTITION'
order by 1, 2, 3,4;
(4)執行分區DDL操作
alter table p truncate partition p1;
(5)查看索引狀態(status列應該是:UNUSABLE不可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_PARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_PARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_PARTITION'
order by 1, 2, 3,4;
(6)索引重建(分區索引的重建需要在最小粒度上執行,如分區/子分區級)
alter index P_GLOBAL_PARTITION rebuild partition p1 online;
alter index P_GLOBAL_PARTITION rebuild partition p2 online;
alter index P_GLOBAL_PARTITION rebuild partition p3 online;
alter index P_GLOBAL_PARTITION rebuild partition pmax online;
(7)查看索引狀態(status列應該是:USABLE可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_GLOBAL_PARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_GLOBAL_PARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_GLOBAL_PARTITION'
order by 1, 2, 3,4;
(8)小結
分區表中分區的drop/truncate操作會造成global型(分區)索引整體變為不可用狀態
4.分區表上local型(分區)索引的DDL
(1)測試表重置
(2)創建索引
drop index p_local_partition;
create index p_local_partition on p(id) local online;
(3)查看索引狀態(status列應該是:USABLE可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_LOCAL_PARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_LOCAL_PARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_LOCAL_PARTITION'
order by 1, 2, 3,4;
(4)執行分區DDL操作
alter table p truncate partition p1;
(5)查看索引狀態(status列應該是:USABLE可用狀態)
select s.index_owner,s.index_name,s.partition_name,s.subpartition_name,s.statusfrom dba_ind_subpartitions s where s.index_name = 'P_LOCAL_PARTITION'
union all
select p.index_owner, p.index_name, p.partition_name, 'NULL', p.statusfrom dba_ind_partitions p where p.index_name = 'P_LOCAL_PARTITION'
union all
select i.owner, i.index_name, 'NULL', 'NULL', i.statusfrom dba_indexes i where i.index_name = 'P_LOCAL_PARTITION'
order by 1, 2, 3,4;
(6)小結
分區表中分區的drop/truncate操作不會造成local型(分區)索引整體變為不可用狀態
5.


※ 如果您覺得文章寫的還不錯,?別忘了在文末給作者點個贊哦 ~

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/36197.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/36197.shtml
英文地址,請注明出處:http://en.pswp.cn/news/36197.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【uniapp】使用Vs Code開發uniapp:

文章目錄 一、使用命令行創建uniapp項目:二、安裝插件與配置:三、編譯和運行:四、修改pinia: 一、使用命令行創建uniapp項目: 二、安裝插件與配置: 三、編譯和運行: 該項目下的dist》dev》mp-weixin文件導入微信開發者…

unity vscode 代碼關聯 跳轉 BUG

一早打開電腦發現代碼關聯失效了,目測可能跟昨天一些插件更新有關 結論 就這貨,開了就沒法提示代碼關聯,估計預覽版全是BUG。 另一個坑 同期有個unity插件也是預覽版,“非常好使”,當場去世。評論點開有好幾個人說用…

替代阿托斯DLKZOR-T/DLHZO-TES直動式伺服閥比例閥

DLKZOR-T/DLKZOR-TES直動式伺服閥比例閥結構: 1,LVDT傳感器 2,比例電磁鐵 3,閥體 4,閥套 5,閥芯 6,復位彈簧 7,集成數字放大器 8,七芯插頭 9,RS232通…

[保研/考研機試] 楊輝三角形 西北工業大學復試上機題 C++實現

題目描述 Time Limit: 1000 ms Memory Limit: 256 mb 輸入n值,使用遞歸函數,求楊輝三角形中各個位置上的值。 輸入描述: 一個大于等于2的整型數n 輸出描述: 題目可能有多組不同的測試數據,對于每組輸入數據, 按題目的要求輸…

15.3.2 【Linux】系統的配置文件:/etc/crontab,/etc/cron.d/*

這個“ crontab -e ”是針對使用者的 cron 來設計的,如果是“系統的例行性任務”時, 該怎么辦呢?是否還是需要以 crontab -e 來管理你的例行性工作調度呢?當然不需要,你只要編輯/etc/crontab 這個文件就可以。有一點需…

arcpy創建基本要素:點和多點

目錄 創建Point點要素步驟一:創建空間參考步驟二:創建要素類步驟三:創建字段步驟四:創建記錄并插入位置信息 創建Multipoint多點要素步驟一:創建空間參考(同上)步驟二:創建要素類步驟…

機器學習終極指南:特征工程(01/2) — 第 -2 部分

西姆蘭吉特辛格 一、介紹 歡迎來到“機器學習終極指南”的第二部分。在第一部分中,我們討論了探索性數據分析 (EDA),這是機器學習管道中的關鍵步驟。在這一部分中,我們將深入研究特征工程,這是機器學習過程…

使用shift關鍵字,寫一個帶二級命令的腳本(如:docker run -a -b -c中的run)

省流:shift關鍵字 探索思路 最近有一個小小的需求,寫一個類似于docker run -a -b -c這樣的腳本,這個腳本名為doline,它本身可以執行(doline -a -b -c),同時又帶有幾個如run、init、start這樣的…

Qt 之 QWidget QLabel

文章目錄 前言一、Qt 工程介紹二、窗口組件1. QWidget 組件2. QLabel組件 總結 前言 一、Qt 工程介紹 Qt Creator 以工程項目的方式對源碼進行管理一個Qt Creator工程包含不同類型的文件 。 .pro 項目描述文件.pro.user用戶配置描述文件- .h頭文件.cpp源文件.ui界面描述文件資…

verilog 實現異步fifo

理論知識參考 異步FIFO_Verilog實現_verilog實現異步fifo_Crazzy_M的博客-CSDN博客 代碼 /* 位寬8bit, 位深8 */ module async_fifo#(parameter FIFO_DEPTH 8,parameter FIFO_WIDTH 8 ) (input rst_n,input wr_clk,input wr_en,input [FIFO_WIDTH - 1:0…

Chrome小技巧---多用戶登錄同一網站不串信息

測試中經常需要用到瀏覽器需要登錄多個賬號 但是有一個問題就是會串號 通過添加不同的用戶再用這用戶登錄,就不串號了: 還可以在瀏覽器的偏好設置中添加啟動地址 這樣每次打開,就進到設置的地址中了

el-table自適應縮放大小

安裝依賴 npm install --save vue-draggable-resizable //或 cnpm install --save vue-draggable-resizablemain.js引入依賴 import VueDraggableResizable from vue-draggable-resizable import "vue-draggable-resizable/dist/VueDraggableResizable.css"; Vue.c…

基于Redis實現全局唯一Id

微信公眾號訪問地址:基于Redis實現全局唯一Id 推薦文章: 1、使用原生Redis命令實現分布式鎖 ? 2、為什么引入Redisson分布式鎖? 3、SpringBoot整合多數據源,并支持動態新增與切換(詳細教程) 4、Sprin…

JVM 查看配置 jinfo 及使用 jstat,查看堆棧jstack及GC

1. Jinfo 查看正在運行的Java應用程序的擴展參數: 包含 JVM 參數與 java 系統參數 命令: jinfo pid 2 jstat 查看堆內存使用情況及 GC 回收頻率等: jstat [-命令選項] [vmid] [間隔時間(毫秒)] [查詢次數] 2.1 jstat -gc pid 最常用,可…

C語言必會題目(2)

W...Y的主頁 😊 代碼倉庫分享💕 今天繼續分享C語言必會的題目,上一篇文章主要是一些選擇題,而今天我們主要內容為編程題的推薦與講解 準備好迎接下面的題了嗎?開始發車了!!! 輸入…

OSI參考模型及TCP/IP協議棧

一、網絡概述 1.1、什么是網絡? 1、網絡的本質就是實現資源共享 2、將各個系統聯系到一起,形成信息傳遞、接收、共享的信息交互平臺 1.2、典型的園區網拓撲 1.3、網絡歷史發展,ARPA和ARPANET 1、1969年,美國國防部高級研究計…

前后端分離------后端創建筆記(03)前后端對接(下)

本文章轉載于【SpringBootVue】全網最簡單但實用的前后端分離項目實戰筆記 - 前端_大菜007的博客-CSDN博客 僅用于學習和討論,如有侵權請聯系 源碼:https://gitee.com/green_vegetables/x-admin-project.git 素材:https://pan.baidu.com/s/…

數據結構--棧和隊列3.1(棧-順序結構)

目錄 棧(Stack)棧頂(top)棧底(bottom)空棧(不含任何元素) 創建棧 入棧操作 出棧操作 銷毀一個棧 計算棧的當前容量 實例分析 棧的插入操作叫做進棧(Push&#xf…

基于Mybatis Plus的SQL輸出攔截器。完美的輸出打印 SQL 及執行時長、statement

我們需要想辦法打印出完成的SQL,Mybatis為我們提供了 org.apache.ibatis.plugin.Interceptor接口,我們來實現該接口做一些打印SQL的工作 package org.springjmis.core.mp.plugins;import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; impor…

創新零售,京東重新答題?

繼新一輪組織架構調整后,京東從低價到下沉動作不斷。 新成立的創新零售部在京東老將閆小兵的帶領下悄然完成了整合。近日,京喜拼拼已改名為京東拼拼,與七鮮、前置倉等業務共同承載起京東線上線下加速融合的夢想。 同時,拼拼的更…