常用统计数据查询

统计数据是数据库在运维使用的过程中周期性进行的常用查询,可以帮助数据库用户较为直观准确地掌握当前数据库的状态以及健康程度。

在 MatrixOne 中,统计数据包含了如下几方面的内容:

  • 元数据(Meatadata):描述数据库的数据,包含数据库信息、表信息、列信息。
  • SQL 统计:在特定时间范围内,SQL 的执行成功与否、执行用户、起始与停止时间。
  • 角色与权限信息:通过查询,获取到 MatrixOne 下所有角色的授权、权限、继承信息,以及执行时间与授权人。

查看当前租户下所有数据库基本信息

  1. > select md.datname as database_name,md.created_time as created_time,mu.user_name as creator, mr.role_name as owner_role, count(mt.reldatabase) as total_tables
  2. from mo_catalog.mo_database md,mo_catalog.mo_role mr, mo_catalog.mo_user mu, mo_catalog.mo_tables mt
  3. where md.creator=mu.user_id and md.owner=mr.role_id and mt.reldatabase_id=md.dat_id
  4. group by mt.reldatabase,md.datname,md.created_time,mu.user_name,mr.role_name
  5. order by md.created_time asc;

查看所有的自增列相关信息

  1. > select att_database as database_name,att_relname as table_name,attname as column_name
  2. from mo_catalog.mo_columns
  3. where att_is_auto_increment=1
  4. order by att_database, att_relname asc;

查看所有视图

  1. > select mt.relname as view_name, mt.reldatabase as database_name,mu.user_name as created_user,mr.role_name as owner_role,mt.created_time
  2. from mo_catalog.mo_tables mt, mo_catalog.mo_user mu, mo_catalog.mo_role mr
  3. where mt.relkind='v' and mt.creator=mu.user_id and mt.owner=mr.role_id
  4. order by 1,2 asc;

查看所有外部表

  1. > select mt.relname as view_name, mt.reldatabase as database_name,mu.user_name as created_user,mr.role_name as owner_role,mt.created_time
  2. from mo_catalog.mo_tables mt, mo_catalog.mo_user mu, mo_catalog.mo_role mr
  3. where mt.relkind='e' and mt.creator=mu.user_id and mt.owner=mr.role_id
  4. order by 1,2 asc;

查看所有表的主键

  1. > select att_database as database_name,att_relname as table_name,attname as column_name
  2. from mo_catalog.mo_columns
  3. where att_constraint_type='p' and att_relname not like '%!%'
  4. order by att_database, att_relname asc;

查看所有没有主键的表

  1. > select distinct att_database as database_name,att_relname as table_name
  2. from mo_catalog.mo_columns
  3. minus
  4. select att_database as database_name,att_relname as table_name
  5. from mo_catalog.mo_columns
  6. where att_constraint_type='p'
  7. order by database_name,table_name asc;

查看过去 24 小时内的 sql 统计(非 sys 租户暂不支持)

  1. > select user,host,status,count(status) as count, date_sub(now(), interval 24 hour) as start_time, now() as end_time
  2. from system.statement_info
  3. where status in ('Success','Failed') and user <> 'internal'
  4. and request_at between date_sub(now(), interval 24 hour) and now()
  5. group by status,user,host
  6. order by user,status asc;

查看所有角色授予用户信息

  1. > select mu.user_name as user_name,mr.role_name as role_name,mug.with_grant_option
  2. from mo_catalog.mo_user mu, mo_catalog.mo_role mr, mo_catalog.mo_user_grant mug
  3. where mu.user_id=mug.user_id and mr.role_id=mug.role_id
  4. order by mu.user_name,mr.role_name asc;

查看所有角色权限信息

  1. > select mrp.role_name,mrp.privilege_name,mrp.obj_type,mrp.privilege_level,md.datname as object_name,with_grant_option
  2. from mo_catalog.mo_role_privs mrp, mo_catalog.mo_database md
  3. where mrp.obj_id=md.dat_id and mrp.obj_type='database'
  4. union
  5. select mrp.role_name,mrp.privilege_name,mrp.obj_type,mrp.privilege_level,'*',with_grant_option
  6. from mo_catalog.mo_role_privs mrp
  7. where obj_id=0
  8. union
  9. select mrp.role_name,mrp.privilege_name,mrp.obj_type,mrp.privilege_level,mt.relname as object_name,with_grant_option
  10. from mo_catalog.mo_role_privs mrp, mo_catalog.mo_tables mt
  11. where mrp.obj_id=mt.rel_id and mrp.obj_type='table'
  12. order by 1,2 asc;

查看所有角色继承信息

  1. > select mr1.role_name as inheritor_role,mr2.role_name as inheritee_role,mu.user_name as operator_user,mrg.granted_time,mrg.with_grant_option
  2. from mo_catalog.mo_user mu, mo_catalog.mo_role mr1, mo_catalog.mo_role mr2,mo_catalog.mo_role_grant mrg
  3. where mu.user_id=mrg.operation_user_id and mr1.role_id=mrg.grantee_id and mr2.role_id=mrg.granted_id
  4. order by mr1.role_name,mr2.role_name asc;

参考文档

本篇文章中所查询的 MatrixOne 系统数据库和表是 MatrixOne 存储系统信息的地方。如果你想了解更多关于系统数据库和表的详细信息,请参见 MatrixOne 系统数据库和表