怎样统计库中各表的行数?

[复制链接]
查看11 | 回复9 | 2005-10-30 17:05:33 | 显示全部楼层 |阅读模式
DBA_TABLES视图中的NUM_ROWS怎么不起作用呢?
select出来是空值~
有什么别的办法可以将表的行数插入到一个统计表中么?
//
create table table_rownum
(table_namevarchar2(30),
rownumber number
);
//
insert into table_rownum
(table_name)
select tname from tab;
请问如何相应的将表的行数插入到统计表中呢?
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
要分析表才行
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
用dbms_stats.gather_schema_stats('username')进行一下分析,再查相应的Statistic视图,就有你要的行数
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
analyze
怎么做??
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
写个过程即可,不要随便分析表.
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
--先建立函数
create or replace function f_lqf_temp
(tablename in varchar2)
return number
is
ls_sql varchar2(1000);
li_num number(10,0);
begin
ls_sql := 'select count(*) from ' || tablename;
execute immediate ls_sql into li_num;
return li_num;
end f_lqf_temp;
--处理
select table_name,f_lqf_temp(table_name) from dba_tables where owner = 'GZ1117' and table_name like '%CHECK%'
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最初由 jlandzpa 发布
[B]写个过程即可,不要随便分析表. [/B]

Why?
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
我整理了一下!
--使用system用户创建统计表
create table table_rows
(table_name varchar2(50) not null,
table_rows number not null);
--把权限赋给username用户
grant all on table_rows to username;
--切换至username用户下 建立函数
create or replace function count_row
(tablename in varchar2)
return number
is
ls_sql varchar2(500);
li_num number(10,0);
begin
ls_sql := 'select count(*) from ' || tablename;
execute immediate ls_sql into li_num;
return li_num;
end count_row;
--切换至username用户下 处理
truncate table system.table_rows;
insert into system.table_rows (table_name,table_rows)
select table_name,count_row(table_name) from dba_tables where owner = 'USERNAME';
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
是的
如果是生产系统的话,千万不要随便使用dbms_stats.gather_schema_stats('username')或者analyze 去分析表,那样的话你的SQL语句的执行计划将发生很大的变化
但是你可以使用analyze table XXXvalidate structure
我觉得要想统计一个user 下的各个表的数据,最好写个函数去处理,或者用sql 语句实现
如下:
SQL>set termount off;
SQL>set feedback off;
SQL>set verify off;
SQL>set echo off;
SQL>set pagesize off;
SQL>spool count_table.sql;
SQL>select 'SELECT COUNT(*) FROM' ||table_name||';'from user_tables;
SQL>spool off;
SQL>@count_table.sql
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
为什么还要在system下建。user_tables已经很全了。
而且还有定期维护。
SQL> select table_name, num_rows from user_tables;
TABLE_NAME
NUM_ROWS
------------------------------ ----------
BONUS
DEPT
EMP
SALGRADE
TEST
SQL> analyze table emp compute statistics;
Table analyzed.
SQL> select table_name, num_rows from user_tables;
TABLE_NAME
NUM_ROWS
------------------------------ ----------
BONUS
DEPT
EMP
14
SALGRADE
TEST
SQL>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行