赞
踩
一般我们通过count()语句就可以查询单张标的数据量,但是遇到很多情况,查询多张表,数据量特别大的时候,就比较慢,耽搁时间,毕竟开发的时间还是很宝贵的;
也有通过下面的SQL去查询数据量,但不知道为什么,有的库可以查到,有的直接返回空,或许是权限问题吧,哪位大神可以解惑。
select t.table_name,t.num_rows from user_tables t
遇事,不得不创建个函数来查询,具体如下:
- create or replace function count_table_rows(table_name in varchar2,owner in varchar2 default null)
- return number authid current_user IS
- num_rows number;
- stmt varchar2(2000);
- begin
- if owner is null then
- stmt := 'select count(*) from "' || table_name || '"';
- else
- stmt := 'select count(*) from "' || owner || '"."' || table_name || '"';
- end if;
- execute immediate stmt
- into num_rows;
- return num_rows;
- -- Oracle 查询用户下所有表的记录数
- end;
接下来,只需要简单的查询就可以查询了:
- select table_name, count_table_rows(table_name) nrows
- from user_tables
- where table_name like 'AAA%'
- order by nrows desc
当然也可以进行sum()汇总等写法,可以自由发挥啦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。