SQL 大全(四)|数据库迁移升级时常用 SQL 语句

news2024/11/15 23:45:21

cc6675943b77422ca1599dfcf0ce6444.gif

作者 | JiekeXu

来源 |公众号 JiekeXu DBA之路(ID: JiekeXu_IT)

如需转载请联系授权 | (个人微信 ID:JiekeXu_DBA)

大家好,我是 JiekeXu,很高兴又和大家见面了,今天和大家一起来看看SQL 大全(四)|数据库迁移升级时常用 SQL 语句,欢迎点击上方蓝字“JiekeXu DBA之路”关注我的公众号,标星或置顶,更多干货第一时间到达

在做 Oracle 升级或者迁移的时候,例如从 Oracle 10g 升级到 11g,从 11g 升级到 19c,亦或者是刚入职刚换项目接触一个数据库的时候,需要查看数据库中的一些信息以便对它有一个更加深入的了解,所以就需要通过一些 SQL 语句来了解他,如下整理了一些相关的常用 SQL 分享给有需要的朋友,可在本公众号后台回复【SQL大全四】获取另外:近期新建一个微信交流群,现 100 多人了,算比较活跃,也会在群中不定期举行抽奖、发红包福利,如有需要的可添加我个人微信【JiekeXu_DBA】,备注:加群。

SQL 大全一 https://www.modb.pro/doc/22598
SQL 大全二 https://www.modb.pro/db/45337
SQL 大全三 https://www.modb.pro/doc/91589
SQL 大全四 https://www.modb.pro/doc/103483
Oracle DBA 日常维护 SQL 脚本大全(收藏版) https://www.modb.pro/db/44364

本文 SQL 均是在运维工作中总结整理而成的,部分 SQL 来源于互联网,但现在已经不知道具体是来源哪个网站,如有侵权,可联系我及时删除,谢谢!

1f715bc27778ea9491adb877faaf667b.png

1、查看业务用户相关信息

set line  240
col profile for a20
set pages 999
col username for a25
col ACCOUNT_STATUS for a18
select USERNAME,ACCOUNT_STATUS,CREATED,PROFILE,DEFAULT_TABLESPACE 
from dba_users where account_status='OPEN' 
and DEFAULT_TABLESPACE not in ('SYSTEM','USERS','OGG_TBS') 
order by CREATED asc;


USERNAME                  ACCOUNT_STATUS     CREATED   PROFILE              DEFAULT_TABLESPACE


------------------------- ------------------ --------- -------------------- ------------------------------


TEST_PY                   OPEN               25-NOV-20 DEFAULT              TEST_PY_DATA
DBA_BAK                   OPEN               08-JUN-22 DEFAULT
DBA_BAK_DATA

ac1b058826aee22d65ad390cc718ed9e.png

select USERNAME,ACCOUNT_STATUS,CREATED,PROFILE,DEFAULT_TABLESPACE from dba_users 
where account_status='OPEN' 
and DEFAULT_TABLESPACE!='USERS' order by CREATED asc;

2、用户权限收集

DROP TABLE SCOTT.T_TMP_USER_JIEKE;
CREATE TABLE   SCOTT.T_TMP_USER_JIEKE(ID NUMBER, USERNAME VARCHAR2(50), EXEC_SQL VARCHAR2(4000),CREATE_TYPE VARCHAR2(20) ); 


DROP  SEQUENCE   SCOTT.S_T_TMP_USER_JIEKE;
CREATE SEQUENCE SCOTT.S_T_TMP_USER_JIEKE; 


BEGIN
  FOR CUR IN (SELECT D.USERNAME,
                     D.DEFAULT_TABLESPACE,
                     D.ACCOUNT_STATUS,
                     'create user ' || D.USERNAME || ' identified by ' ||
                     D.USERNAME || ' default tablespace ' ||
                     D.DEFAULT_TABLESPACE || '  TEMPORARY TABLESPACE  ' ||
                     D.TEMPORARY_TABLESPACE || ';' CREATE_USER,
                     REPLACE(TO_CHAR(DBMS_METADATA.GET_DDL('USER',D.USERNAME)),CHR(10),'') CREATE_USER1
                FROM DBA_USERS D
               --WHERE D.USERNAME NOT IN  ('ANONYMOUS','APEX_030200','APEX_PUBLIC_USER','APPQOSSYS','BI','CTXSYS','DBSNMP','DIP','DMSYS','DVSYS','EXFSYS','FLOWS_FILES','HR','IX','LBACSYS','MDDATA','MDSYS','MGMT_VIEW','OE','OLAPSYS','ORACLE_OCM','ORDDATA','ORDPLUGINS','ORDSYS','OUTLN','OWBSYS','OWBSYS_AUDIT','PM','REMOTE_SCHEDULER_AGENT','SCOTT','SH','SI_INFORMATN_SCHEMA','SI_INFORMTN_SCHEMA','SPATIAL_CSW_ADMIN_USR','SPATIAL_WFS_ADMIN_USR','SYS','SYSMAN','SYSTEM','TSMSYS','WK_TEST','WKPROXY','WKSYS','WMSYS','XDB','XS$NULL')) LOOP
               WHERE D.USERNAME IN  ('PROD_CC','PROD_CB','PROD_CF','PROD_CU')) LOOP


    --create user
    INSERT INTO SCOTT.T_TMP_USER_JIEKE
      (ID, USERNAME, EXEC_SQL, CREATE_TYPE)
    VALUES
      (SCOTT.S_T_TMP_USER_JIEKE.NEXTVAL, CUR.USERNAME, CUR.CREATE_USER, 'USER');


    ---system privilege 
    INSERT INTO SCOTT.T_TMP_USER_JIEKE
      (ID, USERNAME, EXEC_SQL, CREATE_TYPE)
      SELECT SCOTT.S_T_TMP_USER_JIEKE.NEXTVAL,
             CUR.USERNAME,
             CASE
               WHEN D.ADMIN_OPTION = 'YES' THEN
                'GRANT ' || D.PRIVILEGE || ' TO ' || D.GRANTEE ||
                ' WITH GRANT OPTION ;'
               ELSE
                'GRANT ' || D.PRIVILEGE || ' TO ' || D.GRANTEE || ';'
             END PRIV,
             'DBA_SYS_PRIVS'
        FROM DBA_SYS_PRIVS D
       WHERE D.GRANTEE = CUR.USERNAME;


    ---role privilege 
    INSERT INTO SCOTT.T_TMP_USER_JIEKE
      (ID, USERNAME, EXEC_SQL, CREATE_TYPE)
      SELECT SCOTT.S_T_TMP_USER_JIEKE.NEXTVAL,
             CUR.USERNAME,
             CASE
               WHEN D.ADMIN_OPTION = 'YES' THEN
                'GRANT ' || D.GRANTED_ROLE || ' TO ' || D.GRANTEE ||
                ' WITH GRANT OPTION;'
               ELSE
                'GRANT ' || D.GRANTED_ROLE || ' TO ' || D.GRANTEE || ';'
             END PRIV,
             'DBA_ROLE_PRIVS'
        FROM DBA_ROLE_PRIVS D
       WHERE D.GRANTEE = CUR.USERNAME;


    ---objects privilege 
    INSERT INTO SCOTT.T_TMP_USER_JIEKE
      (ID, USERNAME, EXEC_SQL, CREATE_TYPE)
      SELECT SCOTT.S_T_TMP_USER_JIEKE.NEXTVAL,
             CUR.USERNAME,
             CASE
               WHEN D.GRANTABLE = 'YES' THEN
                'GRANT ' || D.PRIVILEGE || ' ON ' || D.OWNER || '.' ||
                D.TABLE_NAME || ' TO ' || D.GRANTEE ||
                '  WITH GRANT OPTION ;'
               ELSE
                'GRANT ' || D.PRIVILEGE || ' ON ' || D.OWNER || '.' ||
                D.TABLE_NAME || ' TO ' || D.GRANTEE || ';'
             END PRIV,
             'DBA_TAB_PRIVS'
        FROM DBA_TAB_PRIVS D
       WHERE D.GRANTEE = CUR.USERNAME;


    ---column privilege 
    INSERT INTO SCOTT.T_TMP_USER_JIEKE
      (ID, USERNAME, EXEC_SQL, CREATE_TYPE)
      SELECT SCOTT.S_T_TMP_USER_JIEKE.NEXTVAL,
             CUR.USERNAME,
             CASE
               WHEN D.GRANTABLE = 'YES' THEN
                'GRANT ' || D.PRIVILEGE || ' (' || D.COLUMN_NAME || ') ON ' ||
                D.OWNER || '.' || D.TABLE_NAME || ' TO ' || D.GRANTEE ||
                '  WITH GRANT OPTION ;'
               ELSE
                'GRANT ' || D.PRIVILEGE || ' (' || D.COLUMN_NAME || ') ON ' ||
                D.OWNER || '.' || D.TABLE_NAME || ' TO ' || D.GRANTEE || ';'
             END PRIV,
             'DBA_COL_PRIVS'
        FROM DBA_COL_PRIVS D
       WHERE D.GRANTEE = CUR.USERNAME ;
  END LOOP;
  COMMIT;
END;
/


--SELECT * FROM SCOTT.T_TMP_USER_JIEKE;
SELECT * FROM SCOTT.T_TMP_USER_JIEKE where username in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU');
可以直接运行EXEC_SQL列来创建用户并赋予相应的权限。另外,可以创建如下的视图:
CREATE OR REPLACE VIEW VW_USER_PRIVS_PROD_CC AS
SELECT D.GRANTEE,
       CASE
         WHEN D.ADMIN_OPTION = 'YES' THEN
          'GRANT ' || D.PRIVILEGE || ' TO ' || D.GRANTEE ||
          ' WITH GRANT OPTION ;'
         ELSE
          'GRANT ' || D.PRIVILEGE || ' TO ' || D.GRANTEE || ';'
       END PRIV,
       'SYSTEM_GRANT' TYPE,
       'DBA_SYS_PRIVS' FROM_VIEW
  FROM DBA_SYS_PRIVS D
UNION ALL
SELECT D.GRANTEE,
       CASE
         WHEN D.ADMIN_OPTION = 'YES' THEN
          'GRANT ' || D.GRANTED_ROLE || ' TO ' || D.GRANTEE ||
          ' WITH GRANT OPTION;'
         ELSE
          'GRANT ' || D.GRANTED_ROLE || ' TO ' || D.GRANTEE || ';'
       END PRIV,
       'SYSTEM_GRANT' TYPE,
       'DBA_SYS_PRIVS' FROM_VIEW
  FROM DBA_ROLE_PRIVS D
UNION ALL
SELECT D.GRANTEE,
       CASE
         WHEN D.GRANTABLE = 'YES' THEN
          'GRANT ' || D.PRIVILEGE || ' ON ' || D.OWNER || '.' ||
          D.TABLE_NAME || ' TO ' || D.GRANTEE || '  WITH GRANT OPTION ;'
         ELSE
          'GRANT ' || D.PRIVILEGE || ' ON ' || D.OWNER || '.' ||
          D.TABLE_NAME || ' TO ' || D.GRANTEE || ';'
       END PRIV,
       'SYSTEM_GRANT' TYPE,
       'DBA_SYS_PRIVS' FROM_VIEW
  FROM DBA_TAB_PRIVS D
UNION ALL
SELECT D.GRANTEE,
       CASE
         WHEN D.GRANTABLE = 'YES' THEN
          'GRANT ' || D.PRIVILEGE || ' (' || D.COLUMN_NAME || ') ON ' ||
          D.OWNER || '.' || D.TABLE_NAME || ' TO ' || D.GRANTEE ||
          '  WITH GRANT OPTION ;'
         ELSE
          'GRANT ' || D.PRIVILEGE || ' (' || D.COLUMN_NAME || ') ON ' ||
          D.OWNER || '.' || D.TABLE_NAME || ' TO ' || D.GRANTEE || ';'
       END PRIV,
       'COL_GRANT' TYPE,
       'DBA_COL_PRIVS' FROM_VIEW
  FROM DBA_COL_PRIVS D;
这样就可以直接查询某个用户的权限了:
set line 9999
SELECT * FROM VW_USER_PRIVS_PROD_CC D WHERE D.GRANTEE = 'PROD_CC';
通过系统包DBMS_METADATA.GET_DDL也可以获取用户的权限信息,如下所示:
SET LONG 9999 LINE 999 PAGES 999
SELECT DBMS_METADATA.GET_DDL('USER', 'PROD_CC') DDL_SQL FROM DUAL
UNION ALL
SELECT DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT', 'PROD_CC') FROM DUAL
UNION ALL
SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT', 'PROD_CC') FROM DUAL
UNION ALL
SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT', 'PROD_CC') FROM DUAL;
3、检查时区
select dbtimezone from dual;


DBTIME
------
+00:00
SQL> !date
Fri Apr 15 16:30:50 CST 2022

4、检查字符集

col PARAMETER for a30
col VALUE for a30 
select * from nls_database_parameters where parameter like '%CHARACTERSET%';
PARAMETER                      VALUE
------------------------------ ------------------------------
NLS_CHARACTERSET                AL32UTF8
NLS_NCHAR_CHARACTERSET         AL16UTF16


select userenv('language') from dual;


USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.AL32UTF8

5、查看补丁信息

col opatch for a30
col comments for a99
select 'opatch',comments from dba_registry_history;


OPATC COMMENTS
------ ---------------------------------------------------------------------------------------------------
opatch Patchset 11.2.0.2.0
opatch Patchset 11.2.0.2.0




$ORACLE_HOME/OPatch/opatch lspatches
29141201;OCW Patch Set Update : 11.2.0.4.190416 (29141201)
29141056;Database Patch Set Update : 11.2.0.4.190416 (29141056)

6、检查数据库组件的安装情况

set pages 345 line 456 
col  COMP_NAME for a40
select COMP_ID,COMP_NAME,VERSION,STATUS from dba_registry;
select COMP_ID, STATUS from DBA_REGISTRY;

13b50046c9da03bc1a7207db0a5029d2.png

7、检查是否使用索引压缩(keycompression)

select index_name,table_name from dba_indexes where compression='ENABLE';


select owner,table_name from dba_tables where  owner not in ('SYS','SYSTEM','GOLDENGATE','SYSMAN','EXFSYS','CTXSYS','WMSYS','APEX_030200','DBSNMP','OGG') and iot_type is not null;
8、检查是否存在同名数据文件
select substr(file_name,-6,2) from dba_data_files 
where tablespace_name='CC_DATA' order by 1;


select file_name from dba_data_files 
where tablespace_name='CC_DATA' order by 1;

9、检查永久表空间

select t.tablespace_name tablesapce_name,count(f.file_id),sum(f.bytes/1024/1024/1024) GB 
from dba_tablespaces t,dba_data_files f 
where t.tablespace_name=f.tablespace_name 
and t.contents='PERMANENT' 
and t.tablespace_name in ('CC_DATA','CC_INDEX','CB_DATA','PROD_CF_TBS','PROD_CU_DATA','PROD_CO_DATA','CC_GP_DATA') 
group by t.tablespace_name order by 2;

10、检查表空间是否加密

select tablespace_name,encrypted from dba_tablespaces;
Select * from dba_encrypted_columns;

72967327236635d88dd8eb0a04fe096c.png

11、检查源端compatible参数

show parameter compatible 


NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
compatible                           string      11.2.0.4.0 --必须大于10.2
如何确定数据库的兼容性级别?
col value for a20
col description for a80
select * from  database_compatible_level;
col value clear
col description clear 
VALUE                DESCRIPTION
-------------------- -------------------------------------------------
11.2.0.4.0           Database will be completely compatible with this
                     software version

‍本文 PDF 电子版如有需要的朋友,可在本公众号后台回复【SQL大全四】获取。‍

12、检查业务用户视图

select owner,view_name from dba_views 
where owner in (select username from dba_users 
where account_status='OPEN' 
and DEFAULT_TABLESPACE not in ('USERS','SYSTEM'));
13、检查无效对象
select * from DBA_INVALID_OBJECTS;
select count(*) from DBA_INVALID_OBJECTS;
Create table scott.tmp_invalid_objects45 as select * from dba_invalid_objects;


--重新编译无效对象
@/u01/app/oracle/product/11.2.0/db_1/rdbms/admin/utlrp.sql


select owner,object_name,object_type,created,last_ddl_time,timestamp from DBA_INVALID_OBJECTS where owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU');


--如果有用户自定义对象,可使用 dbms_metdata.get_ddl 查看定义语句
set long 9999 pagesize 9999
select dbms_metadata.get_ddl('FUNCTION','SPLITSTR','PROD_CB') from dual;
14、统计源端需要迁移的表空间和数据文件大小
select distinct(tablespace_name) from dba_data_files d 
where d.tablespace_name not in ('SYSTEM','SYSAUX','UNDOTBS1','UNDOTBS2','UNDOTBS3','UNDOTBS4');


select d.file_id,
       d.tablespace_name,
       (select (sum(nb.bytes / 1024 / 1024))
          from dba_data_files nb
         where nb.tablespace_name = d.tablespace_name) ts_size_m,
       (d.user_bytes / 1024 / 1024) file_use_size_m
  from dba_data_files d
 where d.tablespace_name not in
       ('SYSTEM', 'SYSAUX', 'UNDOTBS1', 'UNDOTBS2', 'UNDOTBS3', 'UNDOTBS4')
 order by file_id;

15、统计用户表总大小

select d.owner,(sum(bytes)/1024/1024) sizes_m from dba_segments d 
where d.owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO')  
and not exists (select 1 from dba_recyclebin b 
where b.object_name=d.segment_name and d.owner=b.owner) 
group by d.owner order by sum(bytes) desc;


select d.owner,(sum(bytes)/1024/1024/1024) sizes_g from dba_segments d 
where d.owner in ('CC_SZ','CC_CB','CC_OP','PROD_CU','PROD_CO')  
and not exists (select 1 from dba_recyclebin b 
where b.object_name=d.segment_name and d.owner=b.owner) 
group by d.owner order by sum(bytes) desc;


OWNER                             SIZES_G
------------------------------ ----------
CC_SZ                             1392.85962
CC_CB                             105.377991
CC_OP                             7.37866211
PROD_CO                           .458251953
PROD_CU                           .004638672


select (sum(bytes)/1024/1024/1024) sizes_g from dba_segments;
   SIZES_G
----------
1590.34473

16、统计用户对象的个数和类型

对象总数

select d.owner,count(1) from dba_objects d 
where d.owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO') 
and d.owner not in ('PUBLIC') 
and not exists (select 1 from dba_recyclebin b where b.object_name=d.object_name and d.owner = b.owner) 
group by d.owner order by count(1) desc;


--查找使用自建函数的 SQL 
select distinct sql_id, sql_text, module
from V$SQL,
(select object_name
from DBA_OBJECTS O
where owner = 'PROD_CC'
and object_type in ('FUNCTION', 'PACKAGE'))
where (instr(upper(sql_text), object_name) > 0)
and plsql_exec_time > 0
and regexp_like(upper(sql_fulltext), '^[SELECT]')
and parsing_schema_name = 'PROD_CC';

对象类型汇总

select d.owner,d.object_type,count(1) from dba_objects d 
where d.owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO')  
and d.owner not in ('PUBLIC') 
and not exists (select 1 from dba_recyclebin b 
where b.object_name=d.object_name and d.owner = b.owner) 
group by d.owner,d.object_type 
order by count(1) desc;


OWNER                          OBJECT_TYPE           COUNT(1)
------------------------------ ------------------- ----------
PROD_CC                      INDEX                     7352
PROD_CB                      INDEX                     7125
PROD_CF                      INDEX                     4566
PROD_CC                      SEQUENCE                  1151
PROD_CC                      TABLE                     1144
PROD_CB                      SEQUENCE                  1115
PROD_CB                      TABLE                     1106
PROD_CF                      SEQUENCE                   676
PROD_CF                      TABLE                      668
PROD_CC                      LOB                        126
PROD_CB                      LOB                        118
PROD_CF                      LOB                         55
PROD_CC                      FUNCTION                    18
PROD_CB                      FUNCTION                    17
PROD_CU                      INDEX                       15
PROD_CC                      PROCEDURE                    3
PROD_CU                      TABLE                        3
PROD_CB                      PROCEDURE                    2
PROD_CU                      SEQUENCE                     2
PROD_CC                      TRIGGER                      1
PROD_CC                      TYPE                         1
PROD_CF                      FUNCTION                     1


22 rows selected.

检查业务用户自建对象

select OWNER,OBJECT_TYPE,OBJECT_NAME from dba_objects d 
where d.owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO') 
and OBJECT_TYPE not in ('INDEX','SEQUENCE','LOB','TABLE') 
order by 2,1;
17、检查无效索引
select owner,index_name,status from dba_indexes 
where status='UNUSABLE' order by 1,2;


select i.owner,i.index_name,p.partition_name,p.status from dba_ind_partitions p,dba_indexes i
where p.index_name=i.index_name and p.status='UNUSABLE' order by 1,2,3;


select i.owner,i.index_name,s.subpartition_name,s.status from dba_ind_subpartitions s,dba_indexes i 
where s.index_name=i.index_name and s.status='UNUSABLE' order by 1,2,3;
18、确认系统用户是否包含业务对象
--检查SYS和SYSTEM的重复对象,返回如下行则正常。


set line 345
col OBJECT_NAME for a40
select owner,object_name,object_type from dba_objects 
where (object_name,object_type) 
in (select object_name,object_type from dba_objects where owner='SYS') 
and owner='SYSTEM';


OWNER                          OBJECT_NAME                              OBJECT_TYPE
------------------------------ ---------------------------------------- -------------------
SYSTEM                         AQ$_SCHEDULES                     TABLE
SYSTEM                         AQ$_SCHEDULES_PRIMARY             INDEX
SYSTEM                         DBMS_REPCAT_AUTH                 PACKAGE BODY
SYSTEM                         DBMS_REPCAT_AUTH                 PACKAGE


select owner,segment_name,segment_type,tablespace_name from dba_segments 
where tablespace_name in('SYSTEM','SYSAUX') 
and owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO');
19、确认操作系统字节序
select PLATFORM_ID,PLATFORM_NAME,ENDIAN_FORMAT 
from v$transportable_platform 
where PLATFORM_NAME in ('Linux x86 64-bit','AIX-Based Systems (64-bit)');


PLATFORM_ID PLATFORM_NAME                  ENDIAN_FORMAT
----------- ------------------------------ --------------
          6 AIX-Based Systems (64-bit)      Big
         13 Linux x86 64-bit               Little
20、检查表空间是否具有自包含特性
SELECT a.tablespace_name,round(total/1024/1024/1024) "Total g", 
round(free/1024/1024/1024) "Free g",ROUND((total-free)/total,4)*100 "USED%" 
FROM (SELECT tablespace_name,SUM(bytes) free FROM DBA_FREE_SPACE 
GROUP BY tablespace_name ) a, 
(SELECT tablespace_name,SUM(bytes) total FROM DBA_DATA_FILES 
GROUP BY tablespace_name) b 
WHERE a.tablespace_name=b.tablespace_name 
ORDER BY 4; 


exec sys.dbms_tts.transport_set_check('CC_DATA,CCBSCF_INDEX,T4_CBMC_DATA,PROD_CF_TBS,PROD_CU_DATA,PROD_CO_DATA',true);


select * from sys.transport_set_violations;

d9ebacee2e77fe09d9a4b8939e8d59bd.png本文 PDF 电子版如有需要的朋友,可在本公众号后台回复【SQL大全四】获取。

21、检查是否存在用户使用 TSTZ 字段

select c.owner||'.'||c.table_name ||'('||c.column_name ||') -' || c.data_type || '' col 
from dba_tab_cols c,dba_objects o 
where c.data_type like '%WITH TIME ZONE' 
and c.owner=o.owner and c.table_name=o.object_name 
and o.object_type='TABLE' 
order by col;

22、检查兼容的高级队列(Compatible Advanced Queues)

select owner,queue_table,recipients,compatible 
from dba_queue_tables 
where recipients='MULTIPLE' 
and compatible like '%8.0%';

23、检查基于XMLSchema的XMLType对象

select distinct owner from dba_xml_schemas;


select distinct p.tablespace_name 
from dba_tablespaces p,dba_xml_tables x,dba_users u,all_tables t 
where t.table_name=x.table_name and t.tablespace_name=p.tablespace_name 
and x.owner=u.username;
24、检查 SPATIAL 空间组件对象
select owner,index_name from dba_indexes where ityp_name='SPATIAL_INDEX';


select owner,table_name,column_name from dba_tab_columns 
where data_type='SDO_GEOMETRY' and owner!='MDSYS'
order by 1,2,3;

25、检查索引组织表、外部表、临时表、物化视图

select owner,table_name from dba_tables 
where IOT_TYPE is not null and owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO');


select owner,table_name from dba_external_tables 
where  owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO');


select owner,table_name from dba_tables 
where temporary='Y' and owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO');


select owner,count(*) from dba_mviews group by owner;

26、检查 Opaque Types 类型字段

Select distinct owner,data_type from dba_tab_columns 
where owner in ('PROD_CC','PROD_CB','PROD_CF','PROD_CU','PROD_CO');
27、收集数据字典统计信息

查询最近的统计信息收集

set linesize 200
select max(end_time) LATEST, operation from DBA_OPTSTAT_OPERATIONS
where operation in ('gather_dictionary_stats', 'gather_fixed_objects_stats')
group by operation;

收集统计信息命令如下

EXECUTE DBMS_STATS.GATHER_DICTIONARY_STATS;
EXECUTE DBMS_STATS.GATHER_FIXED_OBJECTS_STATS;


收集几个聚簇索引的统计信息
--Bug 25286819 : CLUSTER INDEX STATS NOT GATHERED WHEN STALE TABLE OR DICTIONARY STATS ARE GATHER 


exec dbms_stats.gather_schema_stats('SYS');
exec dbms_stats.gather_index_stats('SYS','I_OBJ#');
exec dbms_stats.gather_index_stats('SYS','I_FILE#_BLOCK#');
exec dbms_stats.gather_index_stats('SYS','I_TS#');
exec dbms_stats.gather_index_stats('SYS','I_USER#');
exec dbms_stats.gather_index_stats('SYS','I_TOID_VERSION#');
exec dbms_stats.gather_index_stats('SYS','I_MLOG#');
exec dbms_stats.gather_index_stats('SYS','I_RG#');

28、查看数据字典状态

该 dbupgdiag.sql 脚本在升级之前或之后收集有关数据库状态的诊断信息。从 My Oracle Support 556610.1 下载脚本,并以数据库 SYS 用户身份运行脚本。该脚本在名为  db_upg_diag_sid_timestamp.log 的日志文件中以可读格式生成诊断信息 ,其中 sid 是数据库的 Oracle 系统标识符, timestamp 是生成文件的时间。

SQL> @dbupgdiag.sql 
Enter location for Spooled output as Parameter 1: 
Enter value for 1: /tmp


more db_upg_diag_jiekedb_21_Apr_2022_1102.log

29、确认是否有物化视图刷新

select o.name from sys.obj$ o,sys.user$ u,sys.sum$ s 
where o.type#=42 and bitand(s.mflags,8)=8;
30、查看数据类型
select distinct(DATA_TYPE) from all_tab_columns where owner='PROD_CC';


select distinct(DATA_TYPE) from user_tab_columns;


DATA_TYPE
--------------------------------------------------------------------------------
TIMESTAMP(6)
NVARCHAR2
NUMBER
CHAR
CLOB
DATE
BLOB
VARCHAR2

b838a5ca361550103a00ff31b622a746.png本文 PDF 电子版如有需要的朋友,可在本公众号后台回复【SQL大全四】获取。

31、确认数据文件不需要介质恢复,且不处于备份(backup)模式

select * from v$recover_file;
select * from v$backup where status !='NOT ACTIVE';

32、处理分布式事务

select * from dba_2pc_pending;


如果上面有返回行,执行下面内容。


select local_tran_id from dba_2pc_pending;
EXECUTE DBMS_TRANSACTION.PURGE_LOST_DB_ENTRY('');
Commit;

33、清理回收站信息

Select count(*) from dba_recyclebin;


select OWNER,OBJECT_NAME,OBJECT_TYPE,CREATED,LAST_DDL_TIME 
from  dba_objects where CREATED >=to_date('2023-05-20 15:38:58','yyyy-mm-dd HH24:MI:SS') 
and owner!='SYS' and OBJECT_TYPE='TABLE';


purge dba_recyclebin;

34、检查表空间和数据文件的状态

select tablespace_name,status from dba_tablespaces;


select status,online_status,count(*) from dba_data_files 
group by status,online_status;

35、获取创建 DBLINK 的脚本

col DB_LINK for a15
col USERNAME for a15
col HOST for a45
select * from dba_db_links;


 SELECT 'CREATE '||DECODE(U.NAME,'PUBLIC','public ')||'DATABASE LINK '||CHR(10)
    ||DECODE(U.NAME,'PUBLIC',Null, 'SYS','',U.NAME||'.')|| L.NAME||chr(10)
    ||'CONNECT TO ' || L.USERID || ' IDENTIFIED BY "'||L.PASSWORD||'" USING 
    '''||L.HOST||''''
    ||chr(10)||';' TEXT
    FROM SYS.LINK$ L, SYS.USER$ U
WHERE L.OWNER# = U.USER#;

36、获取创建表空间语句

创建所有表空间语句,以用于从生产端导入所有权限及用户,相关脚本如下:

set heading off feedback off trimspool on linesize 500 
spool create_tablespace.sql 
prompt /* ===================== */ 
prompt /* Create user tablespaces */ 
prompt /* ===================== */ 
select 'create TABLESPACE ' || tablespace_name || 
       ' DATAFILE ' ||'''+DATA/JREDB/DATAFILE/'||tablespace_name||'.dbf'''||' size 10M
           autoextend on;'
   from dba_tablespaces 
   where tablespace_name not in ('SYSTEM','SYSAUX','USERS')
      and contents = 'PERMANENT'; 
prompt /* ===================== */ 
prompt /* Create user temporary tablespaces */ 
prompt /* ===================== */ 
select 'create TEMPORARY TABLESPACE ' || tablespace_name || 
       ' TEMPFILE ' ||'''+DATA/JREDB/DATAFILE/'||tablespace_name||'.dbf'''||' size 10M 
           autoextend on;'
from dba_tablespaces 
   where tablespace_name not in ('TEMP')
      and contents = 'TEMPORARY'; 
spool off

37、查询表空间使用率

set pagesize 1000 linesize 180
tti 'Tablespace Usage Status'
col "TOTAL(GB)" for 99,999,999.999
col "USAGE(GB)" for 99,999,999.999
col "FREE(GB)" for 99,999,999.999 
col "EXTENSIBLE(GB)" for 99,999,999.999
col "MAX_SIZE(GB)" for 99,999,999.999
col "FREE PCT %" for 999.99
col "USED PCT OF MAX %" for 999.99
col "NO_AXF_NUM" for 9999
col "AXF_NUM" for 999
select d.tablespace_name "TBS_NAME"
      ,d.contents "TYPE"
      ,nvl(a.bytes /1024/1024/1024,0) "TOTAL(GB)"
      ,nvl(a.bytes - nvl(f.bytes,0),0)/1024/1024/1024 "USAGE(GB)"
      ,nvl(f.bytes,0)/1024/1024/1024 "FREE(GB)"
      ,nvl((a.bytes - nvl(f.bytes,0))/a.bytes * 100,0) "FREE PCT %"
      ,nvl(a.ARTACAK,0)/1024/1024/1024 "EXTENSIBLE(GB)"
      ,nvl(a.MAX_BYTES,0)/1024/1024/1024 "MAX_SIZE(GB)"
      ,nvl((a.bytes - nvl(f.bytes,0))/ (a.bytes + nvl(a.ARTACAK,0)) * 100,0) "USED PCT OF MAX %"
      ,a.NO_AXF_NUM
      ,a.AXF_NUM
from sys.dba_tablespaces d,
(select tablespace_name
       ,sum(bytes) bytes
       ,sum(decode(autoextensible,'YES',maxbytes - bytes,0 )) ARTACAK
       ,count(decode(autoextensible,'NO',0))  NO_AXF_NUM
       ,count(decode(autoextensible,'YES',0)) AXF_NUM
       ,sum(decode(maxbytes, 0, BYTES, maxbytes))   MAX_BYTES
from dba_data_files
group by tablespace_name
) a,
(select tablespace_name
       ,sum(bytes) bytes
from dba_free_space
group by tablespace_name
) f
where d.tablespace_name = a.tablespace_name(+)
  and d.tablespace_name = f.tablespace_name(+)
  and not (d.extent_management like 'LOCAL'and d.contents like 'TEMPORARY')
union all
select d.tablespace_name "TBS_NAME",d.contents "TYPE",nvl(a.bytes /1024/1024/1024,0) "TOTAL(GB)",nvl(t.bytes,0)/1024/1024/1024 "USAGE(GB)",nvl(a.bytes - nvl(t.bytes,0),0)/1024/1024/1024 "FREE(GB)",nvl(t.bytes/a.bytes * 100,0) "FREE PCT %",nvl(a.ARTACAK,0)/1024/1024/1024 "EXTENSIBLE(GB)",nvl(a.MAX_BYTES,0)/1024/1024/1024 "MAX_SIZE(GB)",nvl(t.bytes/(a.bytes + nvl(a.ARTACAK,0)) * 100,0) "USED PCT OF MAX %",a.NO_AXF_NUM,a.AXF_NUM
from sys.dba_tablespaces d,
(select tablespace_name
       ,sum(bytes) bytes
       ,sum(decode(autoextensible,'YES',MAXbytes - bytes,0 )) ARTACAK
       ,count(decode(autoextensible,'NO',0)) NO_AXF_NUM
       ,count(decode(autoextensible,'YES',0)) AXF_NUM
       ,sum(decode(maxbytes, 0, BYTES, maxbytes))   MAX_BYTES
from dba_temp_files
group by tablespace_name
) a,
(select tablespace_name
      , sum(bytes_used) bytes 
from v$temp_extent_pool
group by tablespace_name
) t
where d.tablespace_name = a.tablespace_name(+)
  and d.tablespace_name = t.tablespace_name(+)
  and d.extent_management like 'LOCAL'
  and d.contents like 'TEMPORARY%'
order by 3 desc;

38、比对新旧环境中的 profile 是否一致

select distinct(t.pro) from 
(select s.profile pro,l.profile pro2 
from dba_profiles@dblink s,dba_profiles l 
where s.profile = l.profile(+)
) t 
where t.pro2 is null order by t.pro;


--目标环境需要创建连接源环境的 DBLINK

39、查看 ADMINISTER DATABASE TRIGGER 权限

检查拥有 ADMINISTER DATABASE TRIGGER 权限的用户。如果用户创建了数据库级别的触发器,则必须要拥有 ADMINISTER DATABASE TRIGGER 权限。

select owner,trigger_name from dba_triggers where base_object_type='DATABASE' and owner not in (select grantee from dba_sys_privs where privilege='ADMINISTER DATABASE TRIGGER');


OWNER                          TRIGGER_NAME
------------------------------ ------------------------------
SYSTEM                         LOGON_IP_CONTROL
SYSMAN                         MGMT_STARTUP


Grant ADMINISTER DATABASE TRIGGER TO OWNER;

40、无效对象警告和 DBA 注册表错误

在开始升级之前,Oracle 强烈建议您运行升级前信息工具 ( preupgrd.jar)。

升级前信息工具识别无效的 SYS 和 SYSTEM 对象,以及其他无效对象。用于 utlrp.sql 重新编译无效对象。如果您在升级之前未能执行此操作,则很难确定系统中的哪些对象在

开始升级之前是无效的,以及哪些对象由于升级而变得无效。
$ORACLE_HOME/jdk/bin/java -jar /u01/app/oracle/product/19.0.0/dbhome_1/rdbms/admin/preupgrade.jar FILE TEXT


升级前执行
SQL>@/u01/app/oracle/cfgtoollogs/jiekedb/preupgrade/preupgrade_fixups.sql


升级后执行
SQL>@/u01/app/oracle/cfgtoollogs/jiekedb/preupgrade/postupgrade_fixups.sql

全文完,希望可以帮到正在阅读的你,如果觉得此文对你有帮助,可以分享给你身边的朋友,同事,你关心谁就分享给谁,一起学习共同进步~~~

欢迎关注我的公众号【JiekeXu DBA之路】,第一时间一起学习新知识!


本文已生成 PDF 电子版如有需要的朋友,可在本公众号后台回复【SQL大全四】获取。

———————————————————————————
公众号:JiekeXu DBA之路
CSDN :https://blog.csdn.net/JiekeXu
墨天轮:https://www.modb.pro/u/4347
腾讯云:https://cloud.tencent.com/developer/user/5645107
———————————————————————————

bf232cc14e160a6a5cea0d069aaf1a5e.gif

分享几个数据库备份脚本

Oracle 表碎片检查及整理方案

OGG|Oracle GoldenGate 基础2022 年公众号历史文章合集整理
 
 

Oracle 19c RAC 遇到的几个问题

OGG|Oracle 数据迁移后比对一致性

OGG|Oracle GoldenGate 微服务架构

Oracle 查询表空间使用率超慢问题一则

Oracle 11g升级到19c需要关注的几个问题

国产数据库|TiDB 5.4 单机快速安装初体验

Oracle ADG 备库停启维护流程及增量恢复

Linux 环境搭建 MySQL8.0.28 主从同步环境

8c57958fc11b2a824cb79ce437709716.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/547325.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

由浅入深了解 深度神经网络优化算法

CV - 计算机视觉 | ML - 机器学习 | RL - 强化学习 | NLP 自然语言处理 导言 优化是从一组可用的备选方案中选择最佳方案。优化无疑是深度学习的核心。基于梯度下降的方法已经成为训练深度神经网络的既定方法。 在最简单的情况下,优化问题包括通过系统地从允许集合中…

Jenkins+GitLab+Docker搭建前端自动化构建镜像容器部署

前言 🚀 需提前安装环境及知识点: 1、Docker搭建及基础操作 2、DockerFile文件描述 3、Jenkins搭建及基础点 🚀 目的: 将我们的前端项目打包成一个镜像容器并自动发布部署,可供随时pull访问 一、手动部署镜像及容器 1…

6-《网络面试》

6-《网络面试》 1.http是什么?http的工作机制?http报文?1.1 http工作机制:1.2 URL和http报文 2. HTTP请求方法和状态码3.Get和Post的区别4.HTTP的Header解析1.text/html2.x-www-form-urlencoded3.multipart/form-data4.applicatio…

中断相关概念并利用中断实现按键点亮LED灯

一.中断相关概念 什么是中断? 中断是指计算机运行过程中,出现某些意外情况需主机干预时,机器能自动停止正在运行的 程序并转入处理新情况的程序,处理完毕后又返回原被暂停的程序继续运行。 什么是EXTI? 外部…

【vue上传文件——hash】

vue上传文件 要求:只能上传视频,先计算文件的hash值,hash值一样则不需要上传,不一样在执行上传 分析:因为el-upload没有找到合适的属性,本次用的是原生的input的type属性为file上传 代码: html: 通过点击选取文件按钮调用input上传 js 第一步:点击上传文件先效验是否…

windows下免U盘安装manjaro

建议 建议先看这个https://www.bilibili.com/read/cv23161386/ 背景 新到了一块硬盘,想把这台主机做成双系统的,windowsmanjaro系统。 为什么选择manjaro? win中的虚拟机已经有了日常使用的ubuntu。体验一下manjaro。ubuntu用来开发办公要…

python 之 logging的使用

一、日志模块 import logginglogging.debug("调试日志") logging.info(消息日志) logging.warning("告警日志") logging.error(错误日志) logging.critical(严重错误日志)debug(调试)级别用于输出调试信息,这些信息主…

JDK源码阅读环境搭建

本次针对jdk8u版本的搭建 1.新建项目 新建java项目JavaSourceLearn ,这里我创建的是maven 2.获取JDK源码 打开Project Structure 找到本地JDK安装位置将src.zip解压到项目java包中 整理下项目结构,删除用不到的目录 提示: 添加源码到项目之后首次运行…

【BBQ: A Hand-Built Bias Benchmark for Question Answering 论文精读】

BBQ: A Hand-Built Bias Benchmark for Question Answering 论文精读 InformationAbstract1 Introduction2 Related Work3 The Dataset3.1 Coverage3.2 Template Construction3.3 Vocabulary4 Validation5 Evaluation6 Results7 Discussion8 Conclusion9 Ethical Consideration…

keepalived+nginx搭建高可用kubeadm1.25

实验环境 系统都是centos 7 IP地址主机名称192.168.0.1k8s-master01192.168.0.2k8s-master02192.168.0.3k8s-master03192.168.0.230k8s-vip192.168.0.4k8s-node01192.168.0.5k8s-node02 所有节点修改主机名称 cat <<EOF >> /etc/hosts 192.168.0.1 k8s-master0…

python 容器

容器 Python中&#xff0c;可包含其他对象的对象&#xff0c;称之为“容器”。容器是一种数据结构。 常用的容器主要划分为两种&#xff1a;序列&#xff08;如&#xff1a;列表、元祖等&#xff09;和映射&#xff08;如&#xff1a;字典&#xff09;。序列中&#xff0c;每个…

国考省考行测:百分点和百分数,相对量和绝对量的比较

国考省考行测&#xff1a;百分点和百分数 2022找工作是学历、能力和运气的超强结合体! 公务员特招重点就是专业技能&#xff0c;附带行测和申论&#xff0c;而常规国考省考最重要的还是申论和行测&#xff0c;所以大家认真准备吧&#xff0c;我讲一起屡屡申论和行测的重要知识…

MyBatis扩展

目录 单元测试 spring boot的单元测试 spring boot的单元测试的使用 1.在要测试的类里,右键点击生成 2.点击test 3.配置测试的信息,点击ok 4.在生成的测试类里,加注解,写测试代码 5.运行单元测试 6.查看测试结果 追加测试方法 断言 MyBatis 单表传参查询 MyBatis获取…

YOLO V3 SPP ultralytics 第二节:根据yolo的数据集,生成准备文件和yolo的配置文件

目录 1. 介绍 2. 完整代码 3. 代码讲解 3.1 生成 my_train_data.txt和my_val_data.txt 3.2 生成 my_data.data 文件 3.3 生成 my_yolov3.cfg 3.4 关于my_data_label.names文件 1. 介绍 根据 第一节 的操作&#xff0c;已经生成了下图中圆圈中的部分&#xff0c;而本…

又一个2W+的答题抽奖活动,复盘复盘总结总结

又一个2W的答题抽奖活动&#xff0c;复盘复盘总结总结 前段时间太忙了&#xff0c;现在才有时间对一些活动进行复盘总结&#xff0c;这里先对其中一个答题抽奖活动进行复盘总结一下。遇到的一些问题、分析以及其解决方案。 答题抽奖 参与者每答对一道题既可获得相对应的分数&…

什么是 Schnorr 签名?

在密码学中&#xff0c;Schnorr 签名是由 Schnorr 签名算法生成的数字签名。 与大多数区块链不同&#xff0c;BTC自其早期以来基本保持不变&#xff0c;大多数升级都是有限的&#xff0c;并旨在增强网络的效率而不是功能。BTC协议的更新是非常罕见的&#xff0c;并且通常用于技…

华为云赋能云:聚焦产数融合,深化数字赋能

编辑&#xff1a;阿冒 设计&#xff1a;沐由 假如你是一家制造企业的老板&#xff0c;一定会觉得近期的日子不太好过&#xff1a;国家统计局最新公布的数据显示&#xff0c;4月份制造业采购经理指数&#xff08;PMI&#xff09;为49.2%&#xff0c;较上月下降了2.7个百分点。 市…

I/O控制方式

目录 一、程序查询方式 1.程序查询流程 2.程序查询接口结构 3.案例习题 四、优缺点 二、程序中断方式 1.中断的概念 2.流程图 3.案例习题 三、DMA方式 1.DMA传送过程 2.DMA与主存交换数据的三种方式 3. 与中断程序处理的区别 4.案例习题 一、程序查询方式 1.程序…

C++中pair用法

博主简介&#xff1a;Hello大家好呀&#xff0c;我是陈童学&#xff0c;一个与你一样正在慢慢前行的人。 博主主页&#xff1a;陈童学哦 所属专栏&#xff1a;CSTL 前言&#xff1a;Hello各位小伙伴们好&#xff01;欢迎来到本专栏CSTL的学习&#xff0c;本专栏旨在帮助大家了解…

使用FFMPEG进行音频重采样

准备 1. ffmpeg 4.4 2. sdl2 3.一段原始的音频PCM数据 重采样流程 1.设置输入音频参数和输出音频参数 2.根据设置的参数初始化SwrContent上下文 3.创建一个输入buffer, 根据输入的音频参数&#xff08;采样率&#xff0c;通道数&#xff0c;样本位深度&#xff09;申请空间…