如何获取 Oracle 数据库中发生的插入/更新次数?

发布于 2024-11-19 21:34:04 字数 39 浏览 5 评论 0原文

如何获取一段时间内 Oracle 数据库中发生的插入/更新总数?

How do I get the total number of inserts/updates that have occurred in an Oracle database over a period of time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-11-26 21:34:04

假设您已将 AWR 配置为保留所有 SQL 语句的数据(默认情况下,如果 STATISTICS_LEVEL 为“TYPICAL”且前 100 条语句,则仅保留按 CPU、运行时间等划分的前 30 条数据)如果 STATISTICS_LEVEL 是“ALL”)通过类似的方式

BEGIN
  dbms_workload_repository.modify_snapshot_settings (
    topnsql => 'MAXIMUM'
  );
END;

并假设 SQL 语句在快照捕获它们之前不会从缓存中老化,您可以使用AWR 表用于其中一些。

您可以收集执行 INSERT 语句的次数和执行 UPDATE 语句的次数

SELECT sum( stat.executions_delta ) insert_executions
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 2;

SELECT sum( stat.executions_delta ) update_executions
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 6;

。请注意,这些查询包括您的应用程序发出的语句和 Oracle 在后台发出的语句。如果您想过滤掉某些 SQL 语句,您可以添加其他条件。

同样,您可以获得不同 INSERT 和 UPDATE 语句的总数,

SELECT count( distinct stat.sql_id ) distinct_insert_stmts
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 2;


SELECT count( distinct stat.sql_id ) distinct_update_stmts
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 6;

但是 Oracle 不会跟踪给定时间间隔内插入或更新的行数。因此您将无法从 AWR 获取该信息。最接近的方法是尝试利用 Oracle 的监控来确定统计信息是否过时。假设每个表都启用了MONITORING(在11g中是默认的,我相信在10g中也是默认的),即

ALTER TABLE table_name
  MONITORING;

Oracle会定期刷新插入、更新和删除的大概行数对于每个表到 SYS.DBA_TAB_MODIFICATIONS 表。但这只会显示自从在表上收集统计数据以来的活动,而不是特定时间间隔内的活动。但是,您可以尝试编写一个过程来定期捕获这些数据到您自己的表中并报告该数据。

如果您指示 Oracle 将监控信息从内存刷新到磁盘(否则会有长达几个小时的滞后),

BEGIN
  dbms_stats.flush_database_monitoring_info;
END;

您可以获得自上次收集统计信息以来每个表中已更改的行数的近似计数

SELECT table_owner,
       table_name,
       inserts,
       updates,
       deletes
  FROM sys.dba_tab_modifications

Assuming that you've configured AWR to retain data for all SQL statements (the default is to only retain the top 30 by CPU, elapsed time, etc. if the STATISTICS_LEVEL is 'TYPICAL' and the top 100 if the STATISTICS_LEVEL is 'ALL') via something like

BEGIN
  dbms_workload_repository.modify_snapshot_settings (
    topnsql => 'MAXIMUM'
  );
END;

and assuming that SQL statements don't age out of the cache before a snapshot captures them, you can use the AWR tables for some of this.

You can gather the number of times that an INSERT statement was executed and the number of times that an UPDATE statement was executed

SELECT sum( stat.executions_delta ) insert_executions
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 2;

SELECT sum( stat.executions_delta ) update_executions
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 6;

Note that these queries include both statements that your application issues and statements that Oracle issues in the background. You could add additional criteria if you want to filter out certain SQL statements.

Similarly, you could get the total number of distinct INSERT and UPDATE statements

SELECT count( distinct stat.sql_id ) distinct_insert_stmts
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 2;


SELECT count( distinct stat.sql_id ) distinct_update_stmts
  FROM dba_hist_sqlstat stat 
       JOIN dba_hist_sqltext txt ON (stat.sql_id = txt.sql_id )
       JOIN dba_hist_snapshot snap ON (stat.snap_id = snap.snap_id)
 WHERE snap.begin_interval_time BETWEEN <<start time>> AND <<end time>>
   AND txt.command_type = 6;

Oracle does not, however, track the number of rows that were inserted or updated in a given interval. So you won't be able to get that information from AWR. The closest you could get would be to try to leverage the monitoring Oracle does to determine if statistics are stale. Assuming MONITORING is enabled for each table (it is by default in 11g and I believe it is by default in 10g), i.e.

ALTER TABLE table_name
  MONITORING;

Oracle will periodically flush the approximate number of rows that are inserted, updated, and deleted for each table to the SYS.DBA_TAB_MODIFICATIONS table. But this will only show the activity since statistics were gathered on a table, not the activity in a particular interval. You could, however, try to write a process that periodically captured this data to your own table and report off that.

If you instruct Oracle to flush the monitoring information from memory to disk (otherwise there is a lag of up to several hours)

BEGIN
  dbms_stats.flush_database_monitoring_info;
END;

you can get an approximate count of the number of rows that have changed in each table since statistics were last gathered

SELECT table_owner,
       table_name,
       inserts,
       updates,
       deletes
  FROM sys.dba_tab_modifications
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文