当前位置:   article > 正文

达梦数据库常用sql查询_达梦数据库查询语句

达梦数据库查询语句

提前预知

以下sql查询皆为dm8版本

一、常用的数据库信息sql查询

1、数据库相关信息

a、查询实例

  1. SELECT '实例名称' 数据库选项,INSTANCE_NAME 数据库集群相关参数值 FROM v$instance
  2. union all
  3. SELECT '数据库授权码',(SELECT SERIES_NO FROM V$LICENSE)
  4. UNION ALL
  5. SELECT '数据库有效期',cast((SELECT EXPIRED_DATE FROM V$LICENSE)as VARCHAR)
  6. UNION ALL
  7. SELECT '授权客户',(SELECT AUTHORIZED_CUSTOMER FROM V$LICENSE)
  8. UNION ALL
  9. SELECT '数据库版本',substr(svr_version,instr(svr_version,'(')) FROM v$instance
  10. union all
  11. SELECT '数据库版本小号',(SELECT id_code) FROM v$instance
  12. union all
  13. SELECT '数据库实例路径',(SELECT PARA_VALUE from v$dm_ini where para_name like'%SYSTEM_PATH%')
  14. FROM v$instance
  15. union all
  16. SELECT '字符集',
  17. CASE SF_GET_UNICODE_FLAG() WHEN '0' THEN 'GBK18030' WHEN '1' then 'UTF-8' when '2' then 'EUC-KR' end
  18. union all
  19. SELECT '页大小',cast(PAGE()/1024 as varchar)
  20. union all
  21. SELECT '簇大小',cast(SF_GET_EXTENT_SIZE() as varchar)
  22. union all
  23. SELECT '大小写敏感',cast(SF_GET_CASE_SENSITIVE_FLAG() as varchar)
  24. union all
  25. SELECT '数据库模式',MODE$ from v$instance
  26. union all
  27. SELECT '唯一魔数',cast(permanent_magic as varchar)
  28. union all
  29. SELECT 'LSN',cast(cur_lsn as varchar) from v$rlog
  30. union all
  31. SELECT 'BLANK_PAD_MODE',cast ( BLANK_PAD_MODE() as varchar);

例子预览:

b、查询授权信息

  1. SELECT LIC_VERSION "许可证版本号",
  2. SERIES_NO "序列号",
  3. CASE WHEN "SERVER_SERIES"='P' THEN '个人版'
  4. WHEN "SERVER_SERIES"='S' THEN '标准版'
  5. WHEN "SERVER_SERIES"='E' THEN '企业版'
  6. WHEN "SERVER_SERIES"='A' THEN '安全版'
  7. WHEN "SERVER_SERIES"='C' THEN '云版本'
  8. WHEN "SERVER_SERIES"='D' THEN '开发版'
  9. END "服务器颁布类型",
  10. CASE WHEN "SERVER_TYPE" ='1' THEN '正式版'
  11. WHEN "SERVER_TYPE"='2' THEN '测试版'
  12. WHEN "SERVER_TYPE"='3' THEN '试用版'
  13. END "服务器发布类型",
  14. SERVER_VER "服务器版本号",
  15. EXPIRED_DATE "有效日期",
  16. AUTHORIZED_CUSTOMER "用户名称",
  17. AUTHORIZED_USER_NUMBER "授权用户数",
  18. CONCURRENCY_USER_NUMBER "并发连接数",
  19. MAX_CPU_NUM "最大CPU数目",
  20. NOACTIVE_DEADLINE "未激活状态截止日期",
  21. PRODUCT_TYPE "产品类型",
  22. PROJECT_NAME "项目名称",
  23. CPU_TYPE "授权运行的 CPU类型",
  24. OS_TYPE "授权运行的操作系统",
  25. MAX_CORE_NUM "授权最大CPU核个数",
  26. CASE WHEN "CLUSTER_TYPE"='1' THEN '主备'
  27. WHEN "CLUSTER_TYPE"='2' THEN 'MPP'
  28. WHEN "CLUSTER_TYPE"='NULL' THEN '无集群'
  29. END "授权使用的集群类型",
  30. DATE_GEN "KEY的生成日期"
  31. FROM V$LICENSE;

c、数据库基本信息

  1. SELECT "NAME" "数据库名称" ,
  2. "CREATE_TIME" "数据库创建时间" ,
  3. case when "ARCH_MODE"='Y' then '归档' else '不归档' end "归档模式" ,
  4. "LAST_CKPT_TIME" "最后一次检查点时间" ,
  5. case when "STATUS$" ='1' then '启动'
  6. when "STATUS$"='2' then '启动,redo 完成'
  7. when "STATUS$"='3' then 'MOUNT'
  8. when "STATUS$"='4' then '打开'
  9. when "STATUS$"='5' then '挂起'
  10. end "库状态" ,
  11. case when "ROLE$" ='0' then '普通库'
  12. when "ROLE$"='1' then '主库'
  13. when "ROLE$"='2' then '备库'
  14. end "角色" ,
  15. "TOTAL_SIZE"*page/1024/1024||'M' "总大小" ,
  16. -- "RAC_NODES" "DSC集群系统中的实例总数" ,
  17. "OPEN_COUNT" "数据库open次数" ,
  18. "STARTUP_COUNT" "数据库启动次数" ,
  19. "LAST_STARTUP_TIME" "数据库最近一次启动时间" ,
  20. page/1024 ||'K' "页大小" ,
  21. SF_GET_EXTENT_SIZE() "簇大小" ,
  22. case when SF_GET_CASE_SENSITIVE_FLAG()='1' then '敏感'
  23. when SF_GET_CASE_SENSITIVE_FLAG()='0' then '不敏感'
  24. end "标识符大小写敏感",
  25. case when SF_GET_UNICODE_FLAG() ='0' then 'GB18030'
  26. when SF_GET_UNICODE_FLAG() ='1' then 'UTF-8'
  27. when SF_GET_UNICODE_FLAG() ='2'then 'EUC-KR'
  28. end "数据库字符集"
  29. FROM V$DATABASE;

d、数据库实例信息

  1. SELECT "NAME" "实例名称" ,
  2. "INSTANCE_NUMBER" "实例ID",
  3. "HOST_NAME" "主机名称" ,
  4. "SVR_VERSION" "服务器版本" ,
  5. "DB_VERSION" "数据库版本" ,
  6. "START_TIME" "服务器启动时间" ,
  7. "STATUS$" "系统状态" ,
  8. "MODE$" "数据库模式" ,
  9. "OGUID" "控制文件的 OGUID",
  10. --"RAC_SEQNO" "DSC 序号"
  11. --"RAC_ROLE" "DSC系统角色"
  12. SF_GET_PARA_VALUE(2,'PORT_NUM') "端口号"
  13. FROM V$INSTANCE;

e、表空间信息

  1. select C."ID" "表空间 ID",
  2. C."NAME" "表空间名称",
  3. --C."CACHE" "CACHE名" ,
  4. CASE WHEN C."TYPE$"='1' THEN 'DB 类型'
  5. WHEN C."TYPE$"='2' THEN '临时表空间'
  6. END "表空间类型",
  7. CASE WHEN C."STATUS$"='0' THEN 'ONLINE'
  8. WHEN C."STATUS$"='1' THEN 'OFFLINE'
  9. WHEN C."STATUS$"='2' THEN 'RES_OFFLINE'
  10. WHEN C."STATUS$"='3' THEN 'CORRUPT'
  11. END "状态",
  12. C."TOTAL_SIZE"*page/1024/1024||'M' "总大小",
  13. C."FILE_NUM" "包含的文件数",
  14. C."ENCRYPT_NAME" "加密算法名",
  15. C."ENCRYPTED_KEY" "加密密钥",
  16. D.used_per ||'%' "表空间使用率"
  17. from V$TABLESPACE c
  18. join ( SELECT a.id,
  19. 100-(sum(b.free_size)*100/sum(b.total_size)) used_per
  20. FROM V$TABLESPACE a,
  21. V$DATAFILE b
  22. where a.id=b.GROUP_ID
  23. group by a.id ) d
  24. on c.id=d.id
  25. order by c.id;

 f、数据库实例会话信息

  1. SELECT STATE "会话状态" ,
  2. CLNT_IP "客户端IP",
  3. CLNT_TYPE "连接类型",
  4. CURR_SCH "当前模式",
  5. USER_NAME "当前用户",
  6. COUNT(*) "会话数"
  7. FROM V$SESSIONS
  8. GROUP BY STATE ,
  9. CLNT_IP ,
  10. CLNT_TYPE,
  11. CURR_SCH ,
  12. USER_NAME
  13. ORDER BY STATE;

2、备份与归档信息

a、归档信息

  1. SELECT "ARCH_NAME" "归档名称",
  2. "ARCH_TYPE" "归档类型",
  3. "ARCH_DEST" "归档目标",
  4. "ARCH_FILE_SIZE" "单个归档文件大小",
  5. "ARCH_SPACE_LIMIT" "归档大小上限",
  6. "ARCH_TIMER_NAME" "定时器名称",
  7. "ARCH_IS_VALID" "归档状态",
  8. "ARCH_WAIT_APPLY" "性能模式",
  9. "ARCH_INCOMING_PATH" "远程归档保存在本地的目录",
  10. case arch_space_limit when 0 then '无限制归档上限,归档总大小为'||
  11. (SELECT SUM(LEN)/(1024*1024) FROM V$ARCH_FILE)||'M'
  12. else
  13. ( SELECT SUM(LEN)/(1024*1024)*100 FROM V$ARCH_FILE )/arch_space_limit||'%'
  14. END "已使用归档空间百分比"
  15. FROM V$DM_ARCH_INI;

b、备份集信息

-- 会校验每个备份集,占用IO,慎用 

  1. SELECT "DEVICE_TYPE" "存储介质类型" ,
  2. "BACKUP_ID" "备份ID" ,
  3. "BACKUP_NAME" "备份名" ,
  4. "BACKUP_PATH" "备份路径" ,
  5. CASE WHEN "TYPE" ='0' THEN '基备份' WHEN "TYPE"='1' THEN '增量备份' WHEN "TYPE"='2' THEN '表备份' WHEN "TYPE"='3' THEN '归档备份' END "备份类型" ,
  6. CASE WHEN "LEVEL" ='0' THEN '联机备份' WHEN "LEVEL"='1' THEN '脱机备份' END "是否脱机备份",
  7. CASE WHEN "RANGE#"='1' THEN '库备份' WHEN "RANGE#"='2' THEN '表空间备份' WHEN "RANGE#"='3' THEN '表级备份' WHEN "RANGE#"='4' THEN '归档备份' END "备份类型" ,
  8. "BASE_NAME" "基备份名" ,
  9. "BACKUP_TIME" "备份时间" ,
  10. "BEGIN_LSN" "备份的起始 LSN值" ,
  11. "END_LSN" "结束备份的 LSN值" ,
  12. "BKP_NUM" "备份片个数" ,
  13. CASE WHEN ( SELECT SF_BAKSET_CHECK(DEVICE_TYPE, BACKUP_PATH) ) ='1' THEN '有效' WHEN ( SELECT SF_BAKSET_CHECK(DEVICE_TYPE, BACKUP_PATH) ) =0 THEN '无效' END "备份校验",
  14. 'SELECT SF_BAKSET_CHECK(' ||DEVICE_TYPE ||',' ||BACKUP_PATH||');' "校验命令"
  15. FROM V$BACKUPSET
  16. WHERE BACKUP_TIME>(SYSDATE-7);

3、用户相关信息

a、用户信息

  1. select "USERNAME" "用户名",
  2. "ACCOUNT_STATUS" "账号状态",
  3. "LOCK_DATE" "锁定开始的时间",
  4. "EXPIRY_DATE" "密码过期时间",
  5. "DEFAULT_TABLESPACE" "默认表空间",
  6. "TEMPORARY_TABLESPACE" "默认临时表空间",
  7. "CREATED" "创建时间",
  8. "PROFILE" "表空间所在路径",
  9. "EDITIONS_ENABLED" "是否可读" ,
  10. "AUTHENTICATION_TYPE" "用户登陆验证类型" ,
  11. "NOWDATE" "当前日期时刻"
  12. from DBA_USERS;

b、用户会话设置信息

  1. select b.name "用户名" ,
  2. a.SESS_PER_USER "最大会话数" ,
  3. a.CONN_IDLE_TIME "最大空闲时间(分钟)",
  4. a.FAILED_NUM "登录失败次数" ,
  5. a.LIFE_TIME "口令有效期(天)" ,
  6. a.REUSE_TIME "口令等待期(天)" ,
  7. a.REUSE_MAX "口令变更次数" ,
  8. a.LOCK_TIME "口令锁定期(分钟)" ,
  9. a.GRACE_TIME "口令宽限期(天)" ,
  10. CASE WHEN a.LOCKED_STATUS='1' THEN 'LOCKED'
  11. ELSE THEN 'OPEN'
  12. END "用户状态",
  13. a.LASTEST_LOCKED "最后一次的锁定时间" ,
  14. a.PWD_POLICY "口令策略" ,
  15. --a.RN_FLAG "是否只读" ,
  16. a.ALLOW_ADDR "允许的 IP 地址" ,
  17. a.NOT_ALLOW_ADDR "不允许的 IP 地址",
  18. a.ALLOW_DT "允许登录的时间段" ,
  19. a.NOT_ALLOW_DT "不允许登录的时间段" ,
  20. a.LAST_LOGIN_DTID "上次登录时间" ,
  21. a.LAST_LOGIN_IP "上次登录 IP 地址" ,
  22. a.FAILED_ATTEMPS "即将被锁定的连续登录失败的次数"
  23. from SYSUSERS a,
  24. SYS.SYSOBJECTS b
  25. where a.id=b.id;

4、死锁与阻塞信息

a、死锁查询

  1. select "SEQNO" "编号" ,
  2. "TRX_ID" "事务ID" ,
  3. "SESS_ID" "会话ID" ,
  4. "SESS_SEQ" "会话序列号" ,
  5. "SQL_TEXT" "产生死锁的SQL语句",
  6. "HAPPEN_TIME" "死锁发生的时间"
  7. from V$DEADLOCK_HISTORY;

b、事务阻塞信息

  1. WITH TRX_TAB AS
  2. (
  3. SELECT OB1.NAME,
  4. L1.TRX_ID FROM V$LOCK L1,
  5. SYSOBJECTS OB1 WHERE L1.TABLE_ID=OB1.ID
  6. AND OB1.ID <>0
  7. )
  8. ,
  9. TRX_SESS AS
  10. ( SELECT L.TRX_ID WT_TRXID ,
  11. L.ROW_IDX BLK_TRXID,
  12. L.BLOCKED , ( SELECT NAME TABLE_NAME FROM TRX_TAB A WHERE A.TRX_ID=L.TRX_ID ) WT_TABLE ,
  13. S1.SESS_ID WT_SESS ,
  14. S2.SESS_ID BLK_SESS ,
  15. S1.USER_NAME WT_USER_NAME ,
  16. S2.USER_NAME BLK_USER_NAME,
  17. S1.SQL_TEXT ,
  18. S1.CLNT_IP ,
  19. DATEDIFF(S, S1.LAST_SEND_TIME, SYSDATE) SS
  20. FROM V$LOCK L ,
  21. V$SESSIONS S1,
  22. V$SESSIONS S2
  23. WHERE L.TRX_ID =S1.TRX_ID
  24. AND L.ROW_IDX=S2.TRX_ID
  25. )
  26. SELECT SYSDATE "当前时间" ,
  27. WT_TRXID "所属事务ID" ,
  28. BLK_TRXID "TID锁对象事务ID",
  29. CASE WHEN BLOCKED='1' THEN '是' ELSE THEN '否' END "是否阻塞" ,
  30. WT_TABLE "被阻塞表" ,
  31. WT_SESS "被阻塞的会话ID" ,
  32. BLK_SESS "阻塞的会话ID",
  33. WT_USER_NAME "被阻塞用户" ,
  34. BLK_USER_NAME "阻塞用户" ,
  35. SF_GET_SESSION_SQL(WT_SESS) "被阻塞的SQL" ,
  36. CLNT_IP "客户端IP" ,
  37. SS||'秒' "阻塞时间"
  38. FROM TRX_SESS
  39. WHERE BLOCKED=1;

5、作业调度信息

a、作业信息

  1. SELECT A.ID "作业ID号" ,
  2. A.NAME "作业名称" ,
  3. CASE WHEN A."ENABLE"='1' THEN '启用'
  4. WHEN A."ENABLE"='0' THEN '不启用'
  5. END "是否被启用",
  6. A.USERNAME "创建者名称" ,
  7. A.CREATETIME "创建时间" ,
  8. A.MODIFYTIME "最后一次被修改的时间" ,
  9. A.DESCRIBE "作业描述" ,
  10. B.LAST_DATE ||' ' ||B.LAST_SEC "最后一次运行时间",
  11. B.NEXT_DATE ||' ' ||B.NEXT_SEC "下一次运行时间",
  12. B.WHAT "执行任务的PL/SQL块"
  13. FROM SYSJOB.SYSJOBS A,
  14. SYSJOB.USER_JOBS B
  15. WHERE A.ID=B.JOB;

b、调度查询

  1. SELECT "ID" "调度ID号" ,
  2. "NAME" "调度的名称" ,
  3. "JOBID" "作业ID号" ,
  4. CASE WHEN "ENABLE"='1' THEN '启用'
  5. WHEN "ENABLE"='0' THEN '不启用'
  6. END "是否启用",
  7. CASE WHEN "TYPE" ='0' THEN '只执行一次'
  8. WHEN "TYPE"='1' THEN '按天执行'
  9. WHEN "TYPE"='2' THEN '按周执行'
  10. WHEN "TYPE"='3' THEN '某个月某一天执行'
  11. WHEN "TYPE"='4' THEN '一个月的第一周第几天执行'
  12. WHEN "TYPE"='5' THEN '一个月的第二周的第几天执行'
  13. WHEN "TYPE"='6' THEN '一个月的第三周的第几天执行'
  14. WHEN "TYPE"='7' THEN '一个月的第四周的第几天执行'
  15. WHEN "TYPE"='8' THEN '一个月的最后一周的第几天执行'
  16. END "调度类型",
  17. CASE WHEN "TYPE" ='0' THEN '只执行一次'
  18. WHEN "TYPE"='1' THEN '每隔' ||FREQ_INTERVAL ||'天执行'
  19. WHEN "TYPE"='2' THEN '每' ||FREQ_INTERVAL ||'周执行'
  20. WHEN "TYPE"='3' THEN '每隔' ||FREQ_INTERVAL ||'个月执行'
  21. ELSE THEN '每隔' ||FREQ_INTERVAL ||'个月执行'
  22. END "执行的频率",
  23. CASE WHEN "TYPE" ='0' THEN '只执行一次'
  24. WHEN "TYPE"='1' THEN '每天执行'
  25. WHEN "TYPE"='2' THEN "FREQ_SUB_INTERVAL" ||'(10进制)'
  26. else THEN '第' ||"FREQ_SUB_INTERVAL" ||'天执行'
  27. END "具体执行的频率" ,
  28. "FREQ_MINUTE_INTERVAL" "一天内每隔多少分钟执行一次",
  29. "STARTTIME" "调度的起始时间" ,
  30. "ENDTIME" "调度结束时间" ,
  31. --"SCHNAME" "驱动触发器所属的数据库模式名" ,--"TRIGNAME" "触发器名" ,
  32. "VALID" "是否合法"
  33. --"DESCRIBE" "注释信息"
  34. FROM SYSJOB.SYSJOBSCHEDULES;

c、作业步骤查询

  1. SELECT "EXEC_ID" "作业执行的ID号" ,
  2. "NAME" "作业名" ,
  3. "STEPNAME" "步骤名" ,
  4. "START_TIME" "步骤开始的时间",
  5. "END_TIME" "步骤结束的时间" ,
  6. --"ERRTYPE" ,
  7. "ERRCODE" "错误码" ,
  8. "ERRINFO" "错误描述信息",
  9. "RETRY_ATTEMPTS" "当前重试次数"
  10. FROM SYSJOB.SYSSTEPHISTORIES2 A
  11. WHERE ( SELECT COUNT(*)
  12. FROM SYSJOB.SYSSTEPHISTORIES2 B
  13. WHERE B.NAME = A.NAME
  14. AND B.EXEC_ID >= A.EXEC_ID ) <= 10
  15. ORDER BY A.START_TIME DESC,
  16. A.NAME;

6、SQL查询相关信息

a、实例启动以来长sql

  1. SELECT top 20 "SQL_TEXT" "SQL文本",
  2. "EXEC_TIME"/1000 ||'秒' "执行时间" ,
  3. "FINISH_TIME" "执行结束时间",
  4. "N_RUNS" "执行次数"
  5. FROM V$SYSTEM_LONG_EXEC_SQLS
  6. ORDER BY EXEC_TIME DESC;

b、占用内存大的sql

  1. select top 20 "SESS_ID" "SESSION的ID",
  2. --"SQL_ID" "语句的SQLID" ,
  3. "SQL_TEXT" "SQL文本" ,
  4. "MEM_USED_BY_K"/1024||'M' "使用的内存",
  5. "FINISH_TIME" "执行结束时间" ,
  6. "N_RUNS" "执行次数"
  7. --"SEQNO" "编号" ,--"TRX_ID" "事务号" ,
  8. --"SESS_SEQ" "会话序列号"
  9. from V$SYSTEM_LARGE_MEM_SQLS
  10. order by mem_used_by_k desc;

7、其他

a、共享池相关

  1. select "ADDR" "内存结构地址" ,
  2. "NAME" "内存池名称" ,
  3. "IS_SHARED" "是否共享" ,
  4. "CHK_MAGIC" "是否打开内存校验" ,
  5. "CHK_LEAK" "是否打开泄漏检查" ,
  6. "IS_OVERFLOW" "是否已经触发BAK_POOL的分配",
  7. --"IS_DSA_ITEM" ,
  8. "ORG_SIZE"/1024/1024 ||'M' "初始大小",
  9. "TOTAL_SIZE"/1024/1024 ||'M' "当前总大小",
  10. "RESERVED_SIZE"/1024/1024 ||'M' "已经分配大小",
  11. "DATA_SIZE"/1024/1024 ||'M' "分配的数据占用大小",
  12. "EXTEND_SIZE"/1024/1024 ||'M' "每次扩展的块大小",
  13. "TARGET_SIZE"/1024/1024 ||'M' "扩展的目标大小" ,
  14. "EXTEND_LEN" "扩展链长度" ,
  15. "N_ALLOC" "累计分配次数" ,
  16. "N_EXTEND_NORMAL" "TARGET范围内累计扩展次数" ,
  17. "N_EXTEND_EXCLUSIVE" "超出TARGET累计扩展次数",
  18. "N_FREE" "累计释放次数" ,
  19. "MAX_EXTEND_SIZE"/1024/1024||'M' "最大的扩展块",
  20. "MIN_EXTEND_SIZE"/1024/1024 ||'M' "最小的扩展块" ,
  21. "FILE_NAME" "源文件名" ,
  22. "FILE_LINE" "所在的代码行",
  23. "CREATOR" "创建者线程号"
  24. from V$MEM_POOL;

ps:后续会收集更多实用且常用的查询sql

想要了解更多:达梦数据库 - 新一代大型通用关系型数据库 | 达梦在线服务平台

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1006409
推荐阅读
相关标签
  

闽ICP备14008679号