Skip to content

Instantly share code, notes, and snippets.

@rmorenobello
Last active June 5, 2020 09:39
Show Gist options
  • Save rmorenobello/a3e3eb6ed852ddef46b36ef319525e26 to your computer and use it in GitHub Desktop.
Save rmorenobello/a3e3eb6ed852ddef46b36ef319525e26 to your computer and use it in GitHub Desktop.
Oracle Database - DB Administration
-- Script per obtenir les sentencies que donarien tots els grants sobre cada objecte
-- de l'esquema indicat &&schema_name a l'usuari indicat &&grantee_name:
set heading off pagesize 0 feedback off echo off verify off linesize 200
-- Getting the user schema we want privileges from
accept schema_name char prompt 'Application Schema Name:'
-- Getting the user schema we want to grant privileges to
accept grantee_name char prompt 'Grants to user :'
spool /tmp/givinggrants.sql
select (CASE
WHEN
object_type in ('VIEW','MATERIALIZED VIEW') THEN 'GRANT SELECT, INSERT, DELETE, UPDATE on '
WHEN
OBJECT_TYPE IN ('SEQUENCE') THEN 'GRANT SELECT ON '
WHEN
OBJECT_TYPE IN ('PACKAGE','PROCEDURE','FUNCTION','TYPE') THEN 'GRANT EXECUTE ON '
WHEN
OBJECT_TYPE LIKE 'DIRECTORY' THEN 'GRANT READ ON '
ELSE 'NOT AN APPROPRIATE OBJECT'
END ) ||OWNER||'.'||OBJECT_NAME||' TO &&grantee_name ;'
from all_objects
where owner='&&schema_name'
AND OBJECT_TYPE IN ('DIRECTORY','VIEW','MATERIALIZED VIEW','SEQUENCE','PACKAGE','PROCEDURE','FUNCTION','TYPE')
;
-- Now we do tables...we did not do tables from all objects because we need to
-- limit it to tables and not external tables or nested tables
select 'grant select, insert, delete, update on '||all_tables.owner||'.'||all_tables.table_name||' to &&grantee_name ;'
from all_tables
where owner='&&schema_name' and all_tables.table_name not in
(select axt.table_name from all_external_tables axt
where axt.owner=all_tables.owner)
and all_tables.table_name not in
(select nst.table_name from all_nested_tables nst
where nst.owner=all_tables.owner)
;
-- Now we do the external tables
select 'grant select on '||owner||'.'||table_name||' to &&grantee_name ;'
from all_external_tables where owner='&&schema_name'
;
undef schema_name
undef grantee_name
spool off
Hidden Virtual Columns - Extended Statistics
https://blogs.oracle.com/sql/ora-54033-and-the-hidden-virtual-column-mystery
Para detectarlas
select column_name, data_default, hidden_column
from user_tab_cols
where table_name = 'TAB' and HIDDEN_COLUMN = 'YES';
Si queremos hacer cambios, drop y recrearlas.
- DROP extended stats:
exec dbms_stats.drop_extended_stats(user, 'tab', '(x, y)');
- CREATE extended stats:
select dbms_stats.create_extended_stats(user, 'tab', '(x, y)')
from dual;
-- ***** ADMINISTRATION *****
-- https://oracle-base.com/dba/scripts
-- NLS parameters (Natural Language Specification)
SELECT * FROM V$NLS_PARAMETERS;
-- NLS parameters of Oracle server and the current session:
SELECT DB.PARAMETER, DB.VALUE "DATABASE", I.VALUE "INSTANCE", S.VALUE "SESSION"
FROM NLS_DATABASE_PARAMETERS DB, NLS_INSTANCE_PARAMETERS I, NLS_SESSION_PARAMETERS S
WHERE DB.PARAMETER=I.PARAMETER(+) AND DB.PARAMETER=S.PARAMETER(+)
ORDER BY 1;
Trick to get 1-7 Monday-Sunday independent of session Territory:
WEEKDAY = 1 + TRUNC (dt) - TRUNC (dt, 'IW')
where TRUNC (dt, 'IW') returns the number in that NLS territory for the first day of the week.
-- **********************************************
-- ***************** SESSIONS *******************
-- **********************************************
select s.sid,"SERIAL#","USER#",username,command,lockwait,status,server,schemaname,osuser,process,machine,port,program, sql_id,prev_sql_id, prev_exec_start,module, blocking_session_status, final_blocking_session_status,event, wait_class, wait_time, seconds_in_wait, state
FROM v$session s where osuser = 'raulmoreno' order by MACHINE, prev_exec_start;
-- **********************************************
-- ***************** TABLESPACES ****************
-- **********************************************
-- Check TABLESPACE
SELECT
a.tablespace_name,
a.file_name,
a.bytes allocated_bytes,
b.free_bytes
FROM
dba_data_files a,
(SELECT file_id, SUM(bytes) free_bytes
FROM dba_free_space b GROUP BY file_id) b
WHERE
a.file_id=b.file_id
-- and TABLESPACE_NAME=''
ORDER BY
a.tablespace_name;
-- tablespace file allocated free
-- D_DPF_DATA /CatSalud/datafiles/D_DPF_DATA.dbf 209715200 262144
SELECT
TABLESPACE_NAME TABLESPACE,
FILE_ID,
COUNT(*) PIECES,
SUM(BYTES) FREE_BYTES,
SUM(BLOCKS) FREE_BLOCKS,
MAX(BYTES) LARGEST_BYTES,
MAX(BLOCKS) LARGEST_BLKS
FROM
SYS.DBA_FREE_SPACE
--Where TABLESPACE_NAME='D_DPF_DATA'
GROUP BY TABLESPACE_NAME, FILE_ID;
/*
"TABLESPACE" "FILE_ID" "PIECES" "FREE_BYTES" "FREE_BLOCKS" "LARGEST_BYTES" "LARGEST_BLKS"
"D_DPF_DATA" "73" "1" "262144" "32" "262144" "32"
*/
-- ******************
-- *** TABLE SIZE ***
-- ******************
select segment_name, segment_type, bytes/1024/1024 as size_in_mb
from user_segments
where segment_name = :YOUR_TABLE
or segment_name in (select segment_name from user_lobs where table_name = :YOUR_TABLE)
or segment_name in (select index_name from user_indexes where table_name = :YOUR_TABLE);
-- ******************************************************
-- ******** alter table xxx disable table lock **********
-- ******************************************************
-- The "alter table xxx disable table lock" command is used with the "revoke DBA" privilege command to ensure
-- that only the DBA can change a production table. The disable table lock command is an excellent safeguard
-- for production tables.
-- The following script will disable all production tables for the SCOTT schema:
connect scott/tiger;
spool /tmp/disable_table_lock.sql
select 'alter table scott.'||table_name||' disable table lock;' from user_tables;
spool off;
@/tmp/disable_table_lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment