本文主要是介绍【性能调优】快速获得表中记录件数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
xiaoma0401 发表于 2011-11-5 20:41 其实进行问题调查的话,观察几天或连续一定间隔地观察几次大体就可以判明问题了, 一般次数不会需要太多,所以可以写一组select count(1) from tblname 脚本,然后每次把执行结果保留下来,几次对比即可。(如黏贴到excel中观察) 如果感觉不好对比,也可以专门建一个表,包含表名、记录件数、操作日期 几个字段就可以了, 每次使用insert into 。。。select 语法,类似如下将检索记录插入到表中即可。 insert into cntTbl (表名,记录件数,操作日期) select ‘dept’ as name, count(1) as cnt, to_char(sysdate,'YYYY-MM-DD') as opdate from dept gruop by name,cnt,opdate; commit; 以上,供参考,希望能有帮助 ![]() |
xiaoma0401 发表于 2011-11-5 20:41
楼上理解的没错,关键问题是我只是知道当前的记录数,过去每天的就无法获得了
The old statistics are purged automatically at regular intervals based on the statistics history retention setting and the time of the recent analysis of the system. Retention is configurable using the ALTER_STATS_HISTORY_RETENTION procedure of DBMS_STATS. The default value is 31 days, which means that you would be able to restore the optimizer statistics to any time in last 31 days.
历史统计信息 一般会保留31天。 这些信息包括表的num_rows
Enable or disable autopurging of statistic histories
dbms_stats.alter_stats_history_retention(retention IN NUMBER);
NULL = change to default value
0 = never save old stats, autopurge statistics history
1 = statistics history never purged by autopurge
exec dbms_stats.alter_stats_history_retention(0);
maclean 发表于 2011-11-6 00:36
The old statistics are purged automatically at regular intervals based on the statistics history r ...
跟 maclean 老师又学到新东西


可以利用Oracle自己的统计信息,来得到表中记录的行数。简单做了个实验。
-----------------------------------------------
■在当前用户下查询 user_tables
SQL> conn scott/tiger
已连接。
SQL> select table_name,tablespace_name, num_rows,table_lock from user_tables where table_name in ('DEPT','EMP');
TABLE_NAME TABLESPACE_NAME NUM_ROWS TABLE_LO
------------------------------ ------------------------------ ---------- --------
DEPT USERS 4 ENABLED
EMP USERS 14 ENABLED
已选择2行。
SQL> select count(1) from dept;
COUNT(1)
----------
4
已选择 1 行。
SQL> select count(1) from emp;
COUNT(1)
----------
14
已选择 1 行。
■再换到dba_tables 下查询试试,发现信息一样,而且如果需要查询多个用户下的表的话,使用 dba_tables 更加方便。
SQL> conn / as sysdba
已连接。
SQL> select owner,table_name,tablespace_name, num_rows,table_lock from dba_tables where table_name in ('DEPT','EMP');
OWNER TABLE_NAME TABLESPACE_NAME NUM_ROWS TABLE_LO
------------------------------ ------------------------------ ------------------------------ ---------- --------
SCOTT DEPT USERS 4 ENABLED
SCOTT EMP USERS 14 ENABLED
已选择2行。
SQL>
-----------------------------------------------
TO楼主
Oracle的统计信息是定期启动相关Job来自动收集的,
但是,偶有情况,统计信息收集不是很及时,导致我们看到的统计信息不是最新的,
所以这种方法可参考价值很大,但如果 个别表记录件数与想定件数差异较大,建议再利用select count(1) from 表名 的方式再自己手动验证一下。
或着像下面这样先对相关表重新进行表分析,重新收集统计信息后,再查询user_tables或dba_tables。
analyze table table_name compute statsticstic ;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20335819/viewspace-710313/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20335819/viewspace-710313/
这篇关于【性能调优】快速获得表中记录件数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!