通过SQL代码或脚本迅速定位SQL,通过表和存储过程关系、主外键关系生成初步血缘关系进一步输出到知识图谱中,尽快找到应用场景聚合点,另外是发现表的热度、数量分析等

代码示例

  1. # 查看正在执行的SQL,通过报表迅速定位SQL语句

  2. select a.username, a.sid,a.machine,b.sql_text, b.sql_fulltext,a.sql_exec_start,a.logon_time

  3. from v$session a, v$sqlarea b

  4. where a.sql_address = b.address;

  5. #表大小分析

  6. select table_name,num_rows,a.blocks

  7. from all_tables a;

  8. # 表热度分析

  9. select * from v$segment_statistics

  10. where statistic_name='logical reads'

  11. and object_name='TT'

  12. # 表主外键依赖->知识图谱

  13. select a.owner, --主键拥有者

  14. a.table_name, --主键表

  15. b.column_name, --主键列

  16. c.owner ref_owner, --外键拥有者

  17. c.table_name ref_table_name, --外键表

  18. d.column_name ref_column_name--外键列

  19. from user_constraints a

  20. left join user_cons_columns b on a.constraint_name=b.constraint_name

  21. left join user_constraints c on a.constraint_name=c.r_constraint_name

  22. left join user_cons_columns d on c.constraint_name=d.constraint_name

  23. where a.constraint_type='P'

  24. and c.table_name is not null

  25. order by a.table_name

  26. # 表依赖的存储过程->知识图谱

  27. select a.owner,a.name,a.type

  28. from dba_dependencies a

  29. where a.referenced_name='RECORD10W'

  30. and a.referenced_type in ('TABLE','VIEW')

  31. and a.type='PROCEDURE'

  32. # 数据字典

  33. select a.table_name,a.column_name,

  34. case when a.data_type in ('CHAR','VARCHAR2','NVARCHAR2') then a.data_type||'('||a.data_length||')'

  35. when a.data_type in ('BLOB','CLOB','DATE','TIMESTAMP(6)','FLOAT','LONG') then a.data_type

  36. when a.data_type in ('NUMBER') and a.data_length=22 and a.data_precision is null and a.data_scale=0 THEN 'INTEGER'

  37. when a.data_type in ('NUMBER') and a.data_length=22 and a.data_precision is null and a.data_scale is null THEN 'NUMBER'

  38. when a.data_type in ('NUMBER') and a.data_length=22 and a.data_precision is not null and a.data_scale is not null THEN 'NUMBER'||'('||a.data_precision||','||a.data_scale||')'

  39. end datatype,

  40. a.nullable,

  41. case when d.position is not null then 'P' end primarykey,

  42. b.comments

  43. from user_tab_columns a

  44. left outer join user_col_comments b on a.TABLE_NAME=b.table_name and a.column_name=b.column_name

  45. left outer join user_constraints c on a.TABLE_NAME=c.table_name and c.constraint_type='P'

  46. left join user_cons_columns d on c.constraint_name=d.constraint_name and a.TABLE_NAME=d.table_name and a.column_name=d.column_name

  47. where a.table_name='TESTTYPE'

  48. order by a.table_name,a.column_id

  49. #执行最多的SQL

  50. select s.sql_text,

  51. s.executions "执行次数",

  52. u.username "用户名"

  53. from v$sqlarea s

  54. left join all_users u

  55. on u.user_id = s.parsing_user_id

  56. where u.username in ('METADATA')

  57. and s.parsing_schema_name not iN ('SYS','SYSTEM','SYSMAN')