如何获取 Oracle 数据库中发生的插入/更新次数?
如何获取一段时间内 Oracle 数据库中发生的插入/更新总数?
How do I get the total number of inserts/updates that have occurred in an Oracle database over a period of time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您已将 AWR 配置为保留所有 SQL 语句的数据(默认情况下,如果
STATISTICS_LEVEL
为“TYPICAL”且前 100 条语句,则仅保留按 CPU、运行时间等划分的前 30 条数据)如果STATISTICS_LEVEL
是“ALL”)通过类似的方式并假设 SQL 语句在快照捕获它们之前不会从缓存中老化,您可以使用AWR 表用于其中一些。
您可以收集执行 INSERT 语句的次数和执行 UPDATE 语句的次数
。请注意,这些查询包括您的应用程序发出的语句和 Oracle 在后台发出的语句。如果您想过滤掉某些 SQL 语句,您可以添加其他条件。
同样,您可以获得不同 INSERT 和 UPDATE 语句的总数,
但是 Oracle 不会跟踪给定时间间隔内插入或更新的行数。因此您将无法从 AWR 获取该信息。最接近的方法是尝试利用 Oracle 的监控来确定统计信息是否过时。假设每个表都启用了MONITORING(在11g中是默认的,我相信在10g中也是默认的),即
Oracle会定期刷新插入、更新和删除的大概行数对于每个表到
SYS.DBA_TAB_MODIFICATIONS
表。但这只会显示自从在表上收集统计数据以来的活动,而不是特定时间间隔内的活动。但是,您可以尝试编写一个过程来定期捕获这些数据到您自己的表中并报告该数据。如果您指示 Oracle 将监控信息从内存刷新到磁盘(否则会有长达几个小时的滞后),
您可以获得自上次收集统计信息以来每个表中已更改的行数的近似计数
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 theSTATISTICS_LEVEL
is 'ALL') via something likeand 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
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
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.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)
you can get an approximate count of the number of rows that have changed in each table since statistics were last gathered