Last active
February 13, 2025 14:00
-
-
Save rmorenobello/dab7a2d127bfed4860f2d959c6738e7c to your computer and use it in GitHub Desktop.
OracleDB - Info on constraints, indexs, partitions, subpartitions, ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ******* | |
-- Taules | |
-- ******* | |
SELECT * FROM ALL_TABLES WHERE OWNER = 'BI_JUS' AND TABLE_NAME = 'JUS_MO_OF_TRA_TRAMITS'; | |
-- ***************** | |
-- COLUMN STATISTICS | |
-- ***************** | |
-- Martin Widlake mdw 21/03/2003 | |
-- MDW 11/12/09 enhanced to include more translations of low_value/high_value | |
-- pilfered from Gary Myers blog | |
col owner form a6 word wrap | |
col table_name form a15 word wrap | |
col column_name form a22 word wrap | |
col data_type form a12 | |
col M form a1 | |
col num_vals form 99999,999 | |
col dnsty form 0.9999 | |
col num_nulls form 99999,999 | |
col low_v form a18 | |
col hi_v form a18 | |
col data_type form a10 | |
set lines 110 | |
break on owner nodup on table_name nodup | |
spool col_stats.lst | |
select --owner | |
-- ,table_name | |
column_name | |
,data_type | |
,decode (nullable,'N','Y','N') M | |
,num_distinct num_vals | |
,num_nulls | |
,density dnsty | |
,decode(data_type | |
,'NUMBER' ,to_char(utl_raw.cast_to_number(low_value)) | |
,'VARCHAR2' ,to_char(utl_raw.cast_to_varchar2(low_value)) | |
,'NVARCHAR2' ,to_char(utl_raw.cast_to_nvarchar2(low_value)) | |
,'BINARY_DOUBLE',to_char(utl_raw.cast_to_binary_double(low_value)) | |
,'BINARY_FLOAT' ,to_char(utl_raw.cast_to_binary_float(low_value)) | |
,'DATE',to_char(1780+to_number(substr(low_value,1,2),'XX') | |
+to_number(substr(low_value,3,2),'XX'))||'-' | |
||to_number(substr(low_value,5,2),'XX')||'-' | |
||to_number(substr(low_value,7,2),'XX')||' ' | |
||(to_number(substr(low_value,9,2),'XX')-1)||':' | |
||(to_number(substr(low_value,11,2),'XX')-1)||':' | |
||(to_number(substr(low_value,13,2),'XX')-1) | |
, low_value | |
) low_v | |
,decode(data_type | |
,'NUMBER' ,to_char(utl_raw.cast_to_number(high_value)) | |
,'VARCHAR2' ,to_char(utl_raw.cast_to_varchar2(high_value)) | |
,'NVARCHAR2' ,to_char(utl_raw.cast_to_nvarchar2(high_value)) | |
,'BINARY_DOUBLE',to_char(utl_raw.cast_to_binary_double(high_value)) | |
,'BINARY_FLOAT' ,to_char(utl_raw.cast_to_binary_float(high_value)) | |
,'DATE',to_char(1780+to_number(substr(high_value,1,2),'XX') | |
+to_number(substr(high_value,3,2),'XX'))||'-' | |
||to_number(substr(high_value,5,2),'XX')||'-' | |
||to_number(substr(high_value,7,2),'XX')||' ' | |
||(to_number(substr(high_value,9,2),'XX')-1)||':' | |
||(to_number(substr(high_value,11,2),'XX')-1)||':' | |
||(to_number(substr(high_value,13,2),'XX')-1) | |
, high_value | |
) hi_v | |
from all_tab_columns -- dba_tab_columns | |
where owner like upper('&tab_own') | |
and table_name like upper(nvl('&tab_name','WHOOPS')||'%') | |
ORDER BY owner,table_name,COLUMN_ID | |
/ | |
clear colu | |
spool off | |
clear breaks | |
-- ******************** | |
-- Comments de columnas | |
-- ******************** | |
select c.column_name, case when data_type = 'CHAR' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
when data_type = 'VARCHAR' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
when data_type = 'VARCHAR2' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
when data_type = 'NCHAR' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
when data_type = 'NUMBER' then | |
case when c.data_precision is null and c.data_scale is null then 'NUMBER' | |
when c.data_precision is null and c.data_scale is not null then 'NUMBER(38,'||c.data_scale||')' | |
else data_type||'('||c.data_precision||','||c.data_SCALE||')' end | |
when data_type = 'NVARCHAR' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
when data_type = 'NVARCHAR2' then data_type||'('||c.char_length||decode(char_used,'B',' BYTE','C',' CHAR',null)||')' | |
else data_type end data_type, | |
decode(nullable,'Y','Yes','No') nullable, | |
c.DATA_DEFAULT,column_id, com.comments | |
from sys.all_tab_Columns c, | |
sys.all_col_comments com | |
where c.owner = :OBJECT_OWNER /* <---- OWNER */ | |
and c.table_name = :OBJECT_NAME /* <---- TABLE */ | |
and c.table_name = com.table_name | |
and c.owner = com.owner | |
and c.column_name = com.column_name | |
order by column_id; | |
-- ********** | |
-- Particions | |
-- ********** | |
-- tipus de particionament, subparticionament, nombre de particions, etc.: | |
SELECT -- * | |
OWNER, TABLE_NAME, PARTITIONING_TYPE, SUBPARTITIONING_TYPE, PARTITION_COUNT, | |
PARTITIONING_KEY_COUNT, SUBPARTITIONING_KEY_COUNT, STATUS, "INTERVAL", INTERVAL_SUBPARTITION | |
FROM ALL_PART_TABLES WHERE "OWNER" = 'BISLT_INT_H' and TABLE_NAME = 'FT_HC3_LMS'; | |
-- per quins camps esta particionada: | |
select * from ALL_PART_KEY_COLUMNS where OBJECT_TYPE = 'TABLE' | |
and "OWNER" = 'BISLT_INT_H' and "NAME" = 'FT_HC3_LMS'; | |
SELECT TABLE_OWNER, TABLE_NAME, PARTITION_NAME, HIGH_VALUE, PARTITION_POSITION | |
FROM ALL_TAB_PARTITIONS WHERE TABLE_OWNER = 'BISLT_INT_H' and TABLE_NAME = 'FT_HC3_LMS'; | |
-- Per obtenir el HIGH_VALUE de cada partició com a CHAR o DATE: | |
WITH date_partition AS ( | |
SELECT | |
partition_name, | |
extractvalue(dbms_xmlgen.getxmltype('select high_value | |
FROM ALL_TAB_PARTITIONS WHERE | |
table_owner = ''' | |
|| t.table_owner | |
|| ''' and table_name = ''' | |
|| t.table_name | |
|| ''' and PARTITION_NAME = ''' | |
|| t.partition_name | |
|| ''''),'//text()') AS high_value | |
FROM ALL_TAB_PARTITIONS t | |
WHERE TABLE_OWNER = 'BISLT_INT_H' | |
and TABLE_NAME = 'FT_HC3_LMS' | |
) | |
,final_result AS ( | |
SELECT | |
partition_name, | |
substr(high_value,11,10) as high_value_char, -- para formatearlo, mejor hacer TO_CHAR al high_value_date siguiente | |
TO_DATE(substr(high_value,11,10),'YYYY-DD-MM') as high_value_date | |
FROM date_partition | |
) | |
SELECT partition_name, high_value_char, high_value_date | |
FROM final_result | |
ORDER BY high_value_date | |
; | |
-- ************* | |
-- Subparticions | |
-- ************* | |
SELECT S.TABLE_OWNER, S.TABLE_NAME, S.PARTITION_NAME, S.SUBPARTITION_NAME, P.PARTITION_POSITION, S.SUBPARTITION_POSITION | |
FROM ALL_TAB_SUBPARTITIONS S, ALL_TAB_PARTITIONS P | |
WHERE S.TABLE_OWNER = P.TABLE_OWNER | |
AND S.TABLE_NAME = P.TABLE_NAME | |
AND S.PARTITION_NAME = P.PARTITION_NAME | |
AND P.PARTITION_NAME IN ( | |
SELECT PARTITION_NAME FROM ALL_IND_PARTITIONS | |
WHERE INDEX_OWNER = 'BI_JUS' | |
AND INDEX_NAME IN (SELECT INDEX_NAME FROM ALL_INDEXES WHERE OWNER='BI_JUS' AND TABLE_NAME = 'JUS_MO_OF_TRA_TRAMITS') | |
); | |
-- *********** | |
-- constraints | |
-- *********** | |
SELECT OWNER, CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME, STATUS, DEFERRABLE, DEFERRED, VALIDATED, RELY,INDEX_OWNER,INDEX_NAME | |
FROM ALL_CONSTRAINTS WHERE OWNER = 'BI_JUS' AND TABLE_NAME = 'JUS_MO_OF_TRA_TRAMITS' AND CONSTRAINT_TYPE = 'P'; | |
SELECT cons.owner, cols.table_name, cols.column_name, cols.position, cons.status | |
FROM all_constraints cons INNER JOIN all_cons_columns cols on (cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner) | |
WHERE 1=1 | |
AND cons.constraint_type = 'P' | |
-- and cons.owner like '%INT%' | |
-- AND upper(cols.table_name) like upper('%DELTA') | |
-- AND position = 1 and column_name <> 'DATA_CARREGA' | |
ORDER BY cols.table_name, cols.position; | |
-- **************** | |
-- indexos de taula | |
-- **************** | |
SELECT * FROM ALL_INDEXES WHERE OWNER='BI_JUS' AND TABLE_NAME = 'JUS_MO_OF_TRA_TRAMITS'; | |
-- Índexos i seves columnes | |
select i.index_owner, i.index_name, i.table_owner, i.table_name, LISTAGG(i.column_name, ', ') WITHIN GROUP (order by i.column_position) IDX | |
FROM ALL_IND_COLUMNS i where table_name IN ('FT_HC3_ACC_WS') | |
GROUP BY i.index_owner, i.index_name, i.table_owner, i.table_name | |
order by index_owner, table_name, INDEX_NAME, IDX; | |
-- ***************************** | |
-- indexos si hi ha a particions | |
-- ***************************** | |
SELECT * FROM ALL_IND_PARTITIONS WHERE INDEX_OWNER = 'BI_JUS' | |
AND INDEX_NAME IN (SELECT INDEX_NAME FROM ALL_INDEXES WHERE OWNER='BI_JUS' AND TABLE_NAME = 'JUS_MO_OF_TRA_TRAMITS'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment