DM-常用运维SQL语句
Posted Biubiu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DM-常用运维SQL语句相关的知识,希望对你有一定的参考价值。
日常维护脚本SQL语句
查看配置
-- 查看数据常用参数配置值,数据库常用参数便于了解当前实例数据库文件存放位置,备份集存放位置,数据库缓冲内存大小、使用端口、最大会话连接数等。
SELECT PARA_NAME,PARA_VALUE FROM V$DM_INI WHERE PARA_NAME IN(\'MEMORY_POOL\',\'BUFFER\',\'PORT_NUM\',\'MAX_SESSIONS\',\'MAX_SESSION_STATEMENT\',\'INSTANCE_NAME\',\'BAK_PATH\',\'SYSTEM_PATH\',\'ARCH_INI\');
select sf_get_page_size() page_size, sf_get_extent_size() extent_size, sf_get_unicode_flag() unicode_flag, sf_get_case_sensitive_flag() case_sensitive_flag, sf_get_system_path() system_path;
查看所占字节长度
-- 建表
create table t2(c1 varchar2(10));
-- 插入数据
insert into t2 values(\'a\');
insert into t2 values(\'啊\');
insert into t2 values(\'㺉\');
commit;
-- 查看所占字节长度
select c1,lengthb(c1),(ascii(c1)) from t2;
不同字符集,不同字符所占的字节长度。
查看字符集
字符集在创建实例时指定,设定后不可更改,如果需要变更字符集,只能重建实例。
select sf_get_unicode_flag();
--返回字符集对应的编号: 0 表示 GB18030,1 表示 UTF-8,2 表示 EUC-KR (指韩文字符集)。
检查数据库实例中字符串比较大小写敏感信息
SELECT SF_GET_CASE_SENSITIVE_FLAG();
--1为大小写敏感,0为大小写不敏感
数据库软件版本类型
-- 软件版本在 SERVER_SERIES 列体现,有如下值:P:个人版、S:标准版、E:企业版、A:安全版、C:云版本。
-- 服务器发布类型(SERVER_TYPE)。 1:正式版、2:测试版、3:试用版
select SERVER_SERIES, SERVER_TYPE, EXPIRED_DATE from v$license;
SQL> select SERVER_SERIES, SERVER_TYPE, EXPIRED_DATE from v$license;
LINEID SERVER_SERIES SERVER_TYPE EXPIRED_DATE
---------- ------------- ----------- ------------
1 D 3 2021-06-02
SQL> select * from V$VERSION;
LINEID BANNER
---------- --------------------------------------------------------------
1 DM Database Server x64 V7.6.1.60-Build(2020.06.02-122414)ENT
2 DB Version: 0x7000a
查看数据库状态
-- 查看数据库名和启动状态
select NAME,CREATE_TIME,ARCH_MODE,STATUS$,ROLE$ from v$database;
select NAME,CREATE_TIME,ARCH_MODE,case STATUS$ when \'1\' then \'starting\' when \'2\' then \'redo comp\' when \'3\' then \'MOUNT\' when \'4\' then \'OPEN\' when \'5\' then \'hold\' when \'6\' then \'close\' end STATUS,ROLE$ from v$database;
STATUS$
实例状态:1:启动;2:启动,redo 完成;3:MOUNT; 4:打开;5:挂起;6:关闭
数据库运行时长
SELECT (SYSDATE-START_TIME)*24 db_lifetime FROM V$INSTANCE;
表空间使用率
-- 查看表空间使用情况
select a.tablespace_name,
a.total_mb,
round(b.free_mb, 2) free_mb,
TO_CHAR(ROUND((a.total_mb - b.free_mb) / a.total_mb * 100, 2),
\'990.99\') || \'%\' "usage %"
from (select tablespace_name, sum(bytes) / 1024 / 1024 total_mb
from dba_data_files
group by tablespace_name) a,
(select tablespace_name, sum(bytes) / 1024 / 1024 free_mb
from dba_free_space
group by tablespace_name) b,
dba_tablespaces d
where a.tablespace_name = b.tablespace_name(+)
and a.tablespace_name = d.tablespace_name(+)
order by 4 desc;
数据文件路径
SELECT tablespace_name
,file_name
,AUTOEXTENSIBLE
,bytes / 1024 / 1024 || \'M\' total_space
,user_bytes / 1024 / 1024 || \'M\' user_space
,user_bytes * 100 / bytes "% FREE"
FROM dba_data_files
;
redo日志文件
select * from v$rlogfile;
-- 增加redo日志组
SQL> alter database add logfile \'/dm7/data/DAMENG/DAMENG04.log\' size 256;
-- 调整redo日志大小(只能减小,不能增大)
alter database resize logfile \'/dm7/data/DAMENG/DAMENG04.log\' to 128;
当前数据库中的模式名
-- 当前数据库中的模式名
select distinct object_name TABLE_SCHEMA from all_objects where object_type = \'SCH\';
查出各模式对应的用户
-- 查出各模式对应的用户
SELECT sch_obj.id, sch_obj.name, sch_obj.crtdate, user_obj.name
FROM (SELECT NAME, id, pid, crtdate
FROM sys.sysobjects
WHERE type$ = \'SCH\') sch_obj
,(SELECT NAME, id
FROM sys.sysobjects
WHERE type$ = \'UR\'
AND subtype$ = \'USER\') user_obj
WHERE sch_obj.pid = user_obj.id
ORDER BY sch_obj.name;
开启归档
-- 开启归档
select name,arch_mode from v$database;
select PARA_NAME,PARA_VALUE,MIN_VALUE,MAX_VALUE,MPP_CHK,SESS_VALUE,FILE_VALUE,PARA_TYPE from v$dm_ini where para_name=\'ARCH_INI\';
alter database mount;
alter database archivelog;
--设置本地归档
alter database add archivelog \'DEST =/ups/app/dmdb/dmarch,TYPE=local,FILE_SIZE=1024,SPACE_LIMIT=10240\';
alter database open;
-- 检查确认
select name,arch_mode from v$database;select PARA_NAME,PARA_VALUE,MIN_VALUE,MAX_VALUE,MPP_CHK,SESS_VALUE,FILE_VALUE,PARA_TYPE from v$dm_ini where para_name=\'ARCH_INI\';
select arch_name,arch_type,arch_dest,arch_file_size,ARCH_SPACE_LIMIT from v$dm_arch_ini;
select * from V$ARCH_STATUS;
--测试切换归档 看有没有生成
alter system archive log current;
资源限制
--资源限制信息
SELECT B.NAME, -- 用户名称
-- A.ID, -- 用户 ID
-- A.PASSWORD, -- 用户口令
A.AUTHENT_TYPE, -- 用户认证方式:NDCT_DB_AUTHENT/NDCT_OS_AUTHENT/NDCT_NET_AUTHENT/NDCT_UNKOWN_AUTHENT
A.SESS_PER_USER, -- 在一个实例中,一个用户可以同时拥有的会话数量
A.CONN_IDLE_TIME, -- 用户会话的最大空闲时间
A.FAILED_NUM, -- 用户登录失败次数限制
A.LIFE_TIME, -- 一个口令在终止使用前可以使用的天数
A.REUSE_TIME, -- 一个口令在可以重新使用之前必须经过的天数
A.REUSE_MAX, -- 一个口令在可以重新使用前必须改变的次数
A.LOCK_TIME, -- 用户口令锁定时间
A.GRACE_TIME, -- 用户口令过期后的宽限时间
A.LOCKED_STATUS, -- 用户登录是否锁定:LOGIN_STATE_UNLOCKED/LOGIN_STATE_LOCKED
A.LASTEST_LOCKED, -- 用户最后一次的锁定时间
A.PWD_POLICY, -- 用户口令策略:NDCT_PWD_POLICY_NULL/NDCT_PWD_POLICY_1/NDCT_PWD_POLICY_2/NDCT_PWD_POLICY_3/NDCT_PWD_POLICY_4/NDCT_PWD_POLICY_5
A.RN_FLAG, -- 是否只读
A.ALLOW_ADDR, -- 允许的 IP 地址
A.NOT_ALLOW_ADDR, -- 不允许的 IP 地址
A.ALLOW_DT, -- 允许登录的时间段
A.NOT_ALLOW_DT, -- 不允许登录的时间段
A.LAST_LOGIN_DTID, -- 上次登录时间
A.LAST_LOGIN_IP, -- 上次登录 IP 地址
A.FAILED_ATTEMPS, -- 将引起一个账户被锁定的连续注册失败的次数
A.ENCRYPT_KEY -- 用户登录的存储加密密钥
FROM SYSUSERS A,SYS.SYSOBJECTS B
WHERE A.ID=B.ID
;
死锁
-- 死锁记录
SELECT * FROM V$DEADLOCK_HISTORY;
select count(*) metric_value from v\\$lock where blocked<>0;
性能
执行超过2s的活动SQL
select * from (
SELECT sess_id,sql_text,datediff(ss,last_recv_time,sysdate) Y_EXETIME,
SF_GET_SESSION_SQL(SESS_ID) fullsql,clnt_ip
FROM V$SESSIONS WHERE STATE=\'ACTIVE\')
where Y_EXETIME>=2;
锁查询
select o.name,l.* from v$lock l,sysobjects o where l.table_id=o.id and blocked=1;
阻塞查询
with locks as(
select o.name,l.*,s.sess_id,s.sql_text,s.clnt_ip,s.last_send_time from v$lock l,sysobjects o,v$sessions s
where l.table_id=o.id and l.trx_id=s.trx_id ),
lock_tr as ( select trx_id wt_trxid,row_idx blk_trxid from locks where blocked=1),
res as( select sysdate stattime,t1.name,t1.sess_id wt_sessid,s.wt_trxid,
t2.sess_id blk_sessid,s.blk_trxid,t2.clnt_ip,SF_GET_SESSION_SQL(t1.sess_id) fulsql,
datediff(ss,t1.last_send_time,sysdate) ss,t1.sql_text wt_sql from lock_tr s,locks t1,locks t2
where t1.ltype=\'OBJECT\' and t1.table_id<>0 and t2.ltype=\'OBJECT\' and t2.table_id<>0
and s.wt_trxid=t1.trx_id and s.blk_trxid=t2.trx_id)
select distinct wt_sql,clnt_ip,ss,wt_trxid,blk_trxid from res;
查看表上的锁是哪个会话导致
select a.*,b.NAME,c.SESS_ID from v$lock a
left join sysobjects b on b.ID=a.TABLE_ID
left join v$sessions c on a.TRX_ID=c.TRX_ID;
-- sp_close_session(sess_id); -- kill对应的session
审计日志
-- 显示审计记录,用来查询当前系统默认路径下的审计文件信息。此动态性能视图只有在审计开关打开时才有内容,且只有审计用户可以查询。DM 的 INI 参数 ENABLE_AUDIT 控制
SELECT PARA_VALUE FROM V$PARAMETER WHERE PARA_NAME=\'ENABLE_AUDIT\';
disql SYSAUDITOR/SYSAUDITOR
select * from V$AUDITRECORDS;
select * from SYSAUDITOR.SYSAUDIT;
作业调度信息
--作业调度信息
SELECT * FROM SYSJOB.SYSJOBSCHEDULES;
--作业运行历史信息
SELECT * FROM SYSJOB.SYSSTEPHISTORIES2 A WHERE (SELECT COUNT(*) FROM SYSJOB.SYSSTEPHISTORIES2 B WHERE B.NAME = A.NAME AND B.EXEC_ID >= A.EXEC_ID) <= 1 ORDER BY A.START_TIME DESC,A.NAME;
-- 用户定义的作业信息
select * from SYSJOB.SYSJOBS;
-- 作业包括的所有步骤信息
select * from SYSJOB.SYSJOBSTEPS;
select j.NAME, js.NAME, j.ENABLE, j.VALID, j.DESCRIBE, js.SEQNO, js.COMMAND, js.OUTPUT_FILE_PATH
from SYSJOB.SYSJOBS j left join SYSJOB.SYSJOBSTEPS js on j.ID = js.JOBID
order by j.id;
-- 作业步骤的执行情况的日志
select * from SYSJOB.SYSJOBHISTORIES;
-- 作业的执行情况的日志
select * from SYSJOB.SYSJOBHISTORIES2;
-- 作业步骤的执行情况的日志
select * from SYSJOB.SYSSTEPHISTORIES2;
-- 作业管理系统中所有已定义操作员的信息
select * from SYSJOB.SYSOPERATORS;
-- 作业管理系统中所有已定义的警报信息
select * from SYSJOB.SYSALERTS;
-- 存储警报需要通知的操作员的信息,即警报和操作员的关联信息
select * from SYSJOB.SYSALERTNOTIFICATIONS;
-- 存储警报发生的历史记录的日志
select * from SYSJOB.SYSALERTHISTORIES;
-- SYSMAILINFO 存储作业管理系统管理员的信息
select * from SYSJOB.SYSMAILINFO;
注意:需要先开启作业调度代理,否则抛出不存在SYSJOB schema对象数据。
-- 开启代理作业
SP_INIT_JOB_SYS(1);
创建作业调度的步骤
初始化,创建系统表
SP_INIT_JOB_SYS(1);
创建job名称
call SP_CREATE_JOB(\'job_1\',1,0,\'\',0,0,\'\',0,\'\');
开启作业配置选项
call SP_JOB_CONFIG_START(\'job_1\');
创建job作业步骤
-- 新增
call SP_ADD_JOB_STEP(\'job_1\', \'统计数据更新情况\', 0, \'begin PKG_SYN_DATA.P_HGZX_SYN_DATA; end;\', 1, 2, 0, 0, NULL, 0);
/*
-- 修改
call SP_ALTER_JOB_STEP(\'job_1\', \'统计数据更新情况\', 0, \'begin PKG_SYN_DATA.P_HGZX_SYN_DATA; end;\', 1, 2, 0, 0, NULL, 0);
/
创建调度步骤
-- 新增(每天下午14:00执行1次)
call SP_ADD_JOB_SCHEDULE(\'job_1\', \'调度测试\', 1, 1, 1, 0, 0, \'14:30:00\', NULL, \'2019-06-21 13:51:58\', NULL, \'\');
/
-- 修改
call SP_ALTER_JOB_SCHEDULE(\'job_1\', \'调度测试\', 1, 1, 1, 0, 0, \'14:30:00\', NULL, \'2019-06-21 13:51:58\', NULL, \'\');
作业调度配置提交
call SP_JOB_CONFIG_COMMIT(\'job_1\');
查询确认创建的JOB调度任务
select * from dba_jobs;
使用作业备份
---开启代理作业
SP_INIT_JOB_SYS(1);
--定时每周六运行,进行全量备份
call SP_CREATE_JOB (\'JOB_DB_FULL_BAK\',1,0,\'\',0,0,\'\',0,\'定时全库备份\');
call SP_JOB_CONFIG_START(\'JOB_DB_FULL_BAK\');
call SP_ADD_JOB_STEP(\'JOB_DB_FULL_BAK\',\'STEP_FULL_BAK\',6,\'01010/home/dmdba/dmbak\',1,2,0,0,\'/home/dmdba/dmbak/job_db_full_bak.log\',0);
call SP_ADD_JOB_SCHEDULE(\'JOB_DB_FULL_BAK\',\'SCHEDULE_FULL_BAK\',1,2,1,64,0,\'22:00:00\',NULL,\'2020-12-16 12:00:00\',NULL,\'每周六晚 22:00:00 全备\');
call SP_JOB_CONFIG_COMMIT(\'JOB_DB_FULL_BAK\');
--定时每天运行,进行增量备份(晚上)
call SP_CREATE_JOB(\'JOB_DB_INCREMENT_BAK\',1,0,\'\',0,0,\'\',0,\'定时增量备份\');
call SP_JOB_CONFIG_START(\'JOB_DB_INCREMENT_BAK\');
call SP_ADD_JOB_STEP(\'JOB_DB_INCREMENT_BAK\',\'STEP_INCREMENT_BAK\',6,\'11020000/home/dmdba/dmbak\',1,2,0,0,\'/home/dmdba/dmbak/job_incr.log\',1);
call SP_ADD_JOB_SCHEDULE(\'JOB_DB_INCREMENT_BAK\',\'SCHEDULE_INCREMENT_BAK\',1,1,1,0,0,\'02:00:00\',NULL,\'2020-12-16 12:00:00\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOB_DB_INCREMENT_BAK\');
--定时每周日运行,删除前20天的备份,包括全量和增量
call SP_CREATE_JOB(\'JOB_DB_DEL_BAK\',1,0,\'\',0,0,\'\',0,\'定时删除备份\');
call SP_JOB_CONFIG_START(\'JOB_DB_DEL_BAK\');
call SP_ADD_JOB_STEP(\'JOB_DB_DEL_BAK\',\'STEP_DEL_BAK\',0,\'SP_DB_BAKSET_REMOVE_BATCH(NULL,SYSDATE-20);\',0,0,0,0,\'/home/dmdba/dmbak/job_del_bak.log\',1);
call SP_ADD_JOB_SCHEDULE(\'JOB_DB_DEL_BAK\',\'SCHEDULE_DEL_BAK\',1,2,1,1,0,\'00:00:00\',NULL,\'2020-12-16 12:00:00\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOB_DB_DEL_BAK\');
--定时每周日运行,删除前3天的归档日志
call SP_CREATE_JOB(\'JOB_DEL_ARCH\',1,0,\'\',0,0,\'\',0,\'定时删除归档\');
call SP_JOB_CONFIG_START(\'JOB_DEL_ARCH\');
call SP_ADD_JOB_STEP(\'JOB_DEL_ARCH\',\'STEP_DEL_ARCH\',0,\'SP_ARCHIVELOG_DELETE_BEFORE_TIME(SYSDATE-3);\',0,0,0,0,\'/home/dmdba/dmbak/job_del_arch.log\',1);
call SP_ADD_JOB_SCHEDULE(\'JOB_DEL_ARCH\',\'SCHEDULE_DEL_ARCH\',1,2,1,1,0,\'00:00:00\',NULL,\'2020-12-16 12:00:00\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOB_DEL_ARCH\');
统计信息
--创建视图,统计所有的模式和表
CREATE OR REPLACE VIEW V_ALL_TAB As
SELECT
A.NAME SCH_NAME,
B.NAME TAB_NAME
FROM
SYS.SYSOBJECTS A JOIN SYS.SYSOBJECTS B
ON
A.ID = B.SCHID
AND A.TYPE$ = \'SCH\'
AND A.NAME NOT IN (\'SYS\',\'CTISYS\',\'SYSAUDITOR\',\'SYSSSO\',\'SYSJOB\',\'SYSDBA\')
AND B.SUBTYPE$ = \'UTAB\'
AND B.PID = -1
AND B.NAME NOT LIKE \'%$AUX\' ORDER BY 1,2;
--创建记录统计信息的表
CREATE TABLE T_STAT_TAB
(
SCHE_NAME VARCHAR(20),
TAB_NAME VARCHAR(50),
TAB_CNT INT,
STAT_TIME DATETIME(0) DEFAULT SYSDATE
);
--存储过程,统计所有表的数量,或者指定用户下的表的数量
CREATE OR REPLACE PROCEDURE P_STAT_TABLE (SCHEMA_NAME VARCHAR(50)) AS
V_CNT INT;
BEGIN
IF UCASE(SCHEMA_NAME)== \'ALL\' THEN
SCHEMA_NAME := \'\';
END IF;
FOR REC IN (SELECT SCH_NAME,TAB_NAME FROM V_ALL_TAB WHERE SCH_NAME LIKE \'%\'||SCHEMA_NAME||\'%\') LOOP
BEGIN
EXECUTE IMMEDIATE \'SELECT COUNT(*) FROM "\'||REC.SCH_NAME||\'"."\'||REC.TAB_NAME||\'"\' INTO V_CNT;
EXCEPTION WHEN OTHERS THEN
V_CNT := -1;
END;
EXECUTE IMMEDIATE \'INSERT INTO T_STAT_TAB(SCH_NAME,TAB_NAME,TAB_CNT)
VALUES(\'\'\'||REC.SCH_NAME||\'\'\',\'\'\'||REC.TAB_NAME||\'\'\',\'||V_CNT||\')\';
END LOOP;
COMMIT;
END;
P_START_TABLE(\'ALL\');
--定时每周日运行,统计本周所有表数据
call SP_CREATE_JOB(\'JOB_START_TABLE_COUNT_TIMELY\',1,0,\'\',0,0,\'\',0,\'定时统计表数据量\');
call SP_JOB_CONFIG_START(\'JOB_START_TABLE_COUNT_TIMELY\');
call SP_ADD_JOB_STEP(\'JOB_START_TABLE_COUNT_TIMELY\',\'STEP_START_TABLE_COUNT\',0,\'P_START_TABLE("ALL");\',0,0,0,0,\'\',1);
call SP_ADD_JOB_SCHEDULE(\'JOB_START_TABLE_COUNT_TIMELY\',\'SCHEDULE_START_TABLE_COUNT\',1,2,1,1,0,\'00:00:00\',NULL,\'2020-05-20 22:22:22\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOB_START_TABLE_COUNT_TIMELY\');
--创建统计每周总量和增量
CREATE OR REPLACE VIEW V_SEL_TAB_SUM
AS
SELECT *,TOTAL_ROWS - PRV_TOTAL_ROWS INCREMENT_ROWS
FROM(
SELECT STAT_TIME,TOTAL_ROWS,LEAD(TOTAL_ROWS) OVER(ORDER BY STAT_TIME DESC) PRV_TOTAL_ROWS
FROM (
SELECT LEFT(STAT_TIME,10) STAT_TIME,SUM(TAB_CNT) TOTAL_ROWS
FROM T_STAT_TAB
GROUP BY LEFT(STAT_TIME,10)
)
)
WHERE PRV_TOTAL_ROWS IS NOT NULL;
--查看每周总量和增量
SELECT * FROM V_SEL_TAB_SUM;
--统计每个表每周总量和增量
CREATE OR REPLACE VIEW V_SEL_TAB_INCR
AS
SELECT STAT_TIME,TAB_ROWS,PRV_TAB_ROWS,TAB_ROWS - PRV_TAB_ROWS INCREMENT_ROWS,SCHE_NAME,TAB_NAME
FROM(
SELECT STAT_TIME,TAB_ROWS,LEAD(TAB_ROWS) OVER(PARTITION BY SCHE_NAME,TAB_NAME ORDER BY STAT_TIME DESC) PRV_TAB_ROWS,SCHE_NAME,TAB_NAME
FROM (
SELECT LEFT(STAT_TIME,10) STAT_TIME,TAB_CNT TAB_ROWS,SCHE_NAME,TAB_NAME
FROM T_STAT_TAB
)
)
WHERE PRV_TAB_ROWS IS NOT NULL;
--查看每个表每周总量和增量
SELECT * FROM V_SEL_TAB_INCR;
-- 查看指定表每周总量和增量
SELECT * FROM V_SEL_TAB_INCR WHERE TAB_NAME=\'STAT_TAB\';
-- 查看每个表最新周总量和增量
SELECT * FROM V_SEL_TAB_INCR
WHERE STAT_TIME=(
SELECT MAX(STAT_TIME)
FROM V_SEL_TAB_INCR)
ORDER BY TAB_ROWS DESC;
-- 查看增量较大的表数据信息
SELECT * FROM V_SEL_TAB_INCR ORDER BY INCREMENT_ROWS DESC NULLS LAST;
-- 统计数据库存储空间和表空间存储空间
CREATE TABLE T_STAT_DB_SIZE
(
TBS_NAME VARCHAR(20),
TOTAL_SIZE_MB INT,
STAT_TIME DATETIME(0) DEFAULT SYSDATE
);
CREATE OR REPLACE PROCEDURE P_STAT_SIZE AS
BEGIN
INSERT INTO T_STAT_DB_SIZE(TAB_NAME,TOTAL_SIZE_MB)
SELECT \'DB\' TBS_NAME,TOTAL_SIZE*PAGE/1024/1024 FROM V$DATABASE;
INSERT INTO T_STAT_DB_SIZE(TBS_NAME,TOTAL_SIZE_MB)
SELECT ISNULL(NAME,\'ALL_TBS\') NAME,SUM(TOTAL_SIZE*PAGE/1024/1024) TOTAL_SIZE_MB
FROM V$TABLESPACE
GROUP BY ROLLUP(NAME);
END;
CALL SP_STAT_SIZE;
--定时每周日运行,统计本周表空间存储
call SP_CREATE_JOB(\'JOP_STAT_DB_SIZE_TIMELY\',1,0,\'\',0,0,\'\',0,\'定时统计存储容量\');
call SP_JOB_CONFIG_START(\'JOP_STAT_DB_SIZE_TIMELY\');
call SP_ADD_JOB_STEP(\'JOP_STAT_DB_SIZE_TIMELY\',\'STEP_STAT_DB_SIZE\',0,\'P_STAT_SIZE\',0,0,0,0,\'\',1);
call SP_ADD_JOB_SCHEDULE(\'JOP_STAT_DB_SIZE_TIMELY\',\'SCHEDULE_STAT_DB_SIZE\',1,2,1,1,0,\'00:00:00\',NULL,\'2020-05-20 22:22:22\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOP_STAT_DB_SIZE_TIMELY\');
-- 统计每个表空间增量
CREATE OR REPLACE VIEW V_SEL_DB_INCR
AS
--统计每个表空间增量
SELECT STAT_TIME,TBS_NAME,TOTAL_SIZE_MB,PRV_TOTAL_SIZE_MB,
TOTAL_SIZE_MB - PRV_TOTAL_SIZE_MB INCREMENT_ROWS
FROM (
SELECT STAT_TIME,TBS_NAME,TOTAL_SIZE_MB,
LEAD(TOTAL_SIZE_MB) OVER(PARTITION BY TBS_NAME ORDER BY STAT_TIME DESC) PRV_TOTAL_SIZE_MB
FROM(
SELECT LEFT(STAT_TIME,19) STAT_TIME,TBS_NAME,TOTAL_SIZE_MB
FROM T_STAT_DB_SIZE)
)
WHERE PRV_TOTAL_SIZE_MB IS NOT NULL;
--统计每个表空间增量
SELECT * FROM V_SEL_TAB_INCR;
--统计会话并发情况
CREATE TABLE T_STAT_SESS_COUNT
(
SESS_TYPE VARCHAR(20),
SESS_COUNT INT,
SESS_TIME DATETIME(0)
);
--统计会话并发情况
CREATE OR REPLACE PROCEDURE P_STAT_SESS(STAT_CNT INT) AS
BEGIN
INSERT INTO T_STAT_SESS_COUNT
SELECT \'SESS\' SESS_TYPE,COUNT(*) SESS_COUNT,LEFT(CREATE_TIME,19) SESS_TIME
FROM V$SESSION_HISTORY
WHERE CREATE_TIME > (
SELECT IFNULL(MAX(SESS_TIME),\'2020-01-01 01:01:01\')
FROM T_STAT_SESS_COUNT
WHERE SESS_TYPE = \'SESS\')
GROUP BY LEFT(CREATE_TIME,19)
HAVING COUNT(*) > STAT_CNT;
INSERT INTO T_STAT_SESS_COUNT
SELECT \'SQL\' SESS_TYPE,COUNT(*) SESS_COUNT,LEFT(START_TIME,19) SESS_TIME
FROM V$SQL_HISTORY
WHERE START_TIME > (
SELECT IFNULL(MAX(SESS_TIME),\'2020-01-01 01:01:01\')
FROM T_STAT_SESS_COUNT
WHERE SESS_TYPE = \'SQL\')
GROUP BY LEFT(START_TIME,19)
HAVING COUNT(*) > STAT_CNT;
END;
CALL P_STAT_SESS(2);
--定时每小时运行,统计时间段内并发情况
call SP_CREATE_JOB(\'JOP_STAT_SESS_COUNT_TIMELY\',1,0,\'\',0,0,\'\',0,\'定时统计会话并发量\');
call SP_JOB_CONFIG_START(\'JOP_STAT_SESS_COUNT_TIMELY\');
call SP_ADD_JOB_STEP(\'JOP_STAT_SESS_COUNT_TIMELY\',\'STEP_STAT_SESS_SCOUNT\',0,\'CALL P_STAT_SESS(5);\',0,0,0,0,\'\',1);
call SP_ADD_JOB_SCHEDULE(\'JOP_STAT_SESS_COUNT_TIMELY\',\'SCHEDULE_STAT_SESS_COUNT\',1,1,1,0,60,\'00:00:00\',\'23:59:59\',\'2020-05-20 22:22:22\',NULL,\'\');
call SP_JOB_CONFIG_COMMIT(\'JOP_STAT_SESS_COUNT_TIMELY\');
--查询会话并发统计,按并发量排序。
SELECT * FROM T_STAT_SESS_COUNT WHERE SESS_TYPE = \'SESS\' ORDER BY SESS_COUNT DESC;
--查询会话并发统计,按并发时间排序。
SELECT * FROM T_STAT_SESS_COUNT WHERE SESS_TYPE = \'SESS\' ORDER BY SESS_TIME DESC;
--按照分钟统计会话并发
SELECT LEFT(SESS_TIME,16),SUM(SESS_COUNT)
FROM T_STAT_SESS_COUNT
WHERE SESS_TYPE=\'SESS\'
GROUP BY LEFT(SESS_TIME,16)
ORDER BY 2 DESC ,1 DESC;
--查询SQL并发统计,按并发量排序。
SELECT * FROM T_STAT_SESS_COUNT WHERE SESS_TYPE = \'SQL\' ORDER BY SESS_COUNT DESC;
--查询SQL并发统计,按并发时间排序。
SELECT * FROM T_STAT_SESS_COUNT WHERE SESS_TYPE = \'SQL\' ORDER BY SESS_TIME DESC;
--按照分钟统计SQL并发
SELECT LEFT(SESS_TIME,16),SUM(SESS_COUNT)
FROM T_STAT_SESS_COUNT
WHERE SESS_TYPE=\'SQL\'
GROUP BY LEFT(SESS_TIME,16)
ORDER BY 2 DESC ,1 DESC;
通过V$ERR_INFO 视图查看对应的错误代号
select top 5 * from V$ERR_INFO;
LINEID CODE errinfo
---------- ----------- --------------------
1 100 Empty result
2 101 String truncated
3 102 NULL in set function
4 103 Invalid table name
5 104 Delete 0 row
查看自带函数
-- 统计信息相关函数
select name, CLASS$ from v$ifun where name like \'%SP%STAT%INIT\';
执行计划缓存
-- 创建系统包
CALL SP_CREATE_SYSTEM_PACKAGES(1);
-- 收集SYSDBA 模式下所有索引的统计信息
DBMS_STATS.GATHER_SCHEMA_STATS(\'SYSDBA\',100,FALSE,\'FOR ALL COLUMNS SIZE AUTO\');
-- 清理执行计划缓存
CALL SP_CLEAR_PLAN_CACHE();
统计存储过程数量
-- 统计存储过程数量
SELECT sch.name, proc.name, proc.crtdate
FROM sysobjects sch, sysobjects proc
WHERE proc.schid = sch.id
AND sch.name IN (\'MID\', \'APP\')
AND proc.subtype$ = \'PROC\';
AND proc.crtdate > SYSDATE - 1;
清空模式下所有表数据
-- 清空模式下所有表数据
DECLARE
BEGIN
FOR rs IN (SELECT *
FROM all_tables
WHERE "OWNER" IN (\'MID\', \'BAS\'))
LOOP
DECLARE
BEGIN
EXECUTE IMMEDIATE \'TRUNCATE TABLE \' || rs.owner || \'.\' || rs.table_name;
EXCEPTION
WHEN OTHERS THEN
print(\'TRUNCATE TABLE\' || rs.owner || \'.\' || rs.table_name);
END;
END LOOP;
END;
查看数据库使用内存
-- 查看数据库使用内存
SELECT NAME
, -- 内存池描述名称
SUM(org_size)
, -- 内存池原始大小,sum 函数:总数
sf_get_ep_seqno(ROWID)
, --rowid 数据对象编号
SUM(total_size) / 1024.0 / 1024 / 1024 total_gb
, -- 内存池目前大小
SUM(reserved_size)
, -- 内存池内已经分配掉的空间
COUNT(*)
,AVG(total_size) / 1024.0 / 1024 avg_size_mb -- 内存池目前大小,avg 函数:平均值
FROM v$mem_pool
GROUP BY NAME, sf_get_ep_seqno(ROWID)
ORDER BY 4 DESC;
以上是关于DM-常用运维SQL语句的主要内容,如果未能解决你的问题,请参考以下文章