325*_*325 1 oracle oracle-sqldeveloper
我正在尝试在包含数百个表的数据库中查找数据。我只是通过并select *为所有表编写简单的查询,一一检查它们。它需要永远!我想知道是否有办法编写查询来过滤数据库中至少有 1 行的所有表。这可能吗?
小智 5
with
function f(tabname in varchar2) return int as
res int;
begin
execute immediate 'select count(*) cnt from "'||tabname||'" where rownum=1' into res;
return res;
end;
select table_name, f(table_name) chk
from user_tables;
Run Code Online (Sandbox Code Playgroud)
结果:
TABLE_NAME CHK
------------------------------ ----------
STRVALS 1
MY_EMP 1
TBL_3 1
TBL_1 1
TLOCK 1
DATES 1
B 1
A 1
C 1
T0 0
Run Code Online (Sandbox Code Playgroud)
或者select count(*) from dual where exists(...):
with
function f(tabname in varchar2) return int as
res int;
begin
execute immediate 'select count(*) cnt from "'||tabname||'" where rownum=1' into res;
return res;
end;
select table_name, f(table_name) chk
from user_tables;
Run Code Online (Sandbox Code Playgroud)
如果您的数据库得到正确维护,您将获得优化器统计信息。
如果您的表缺少统计信息,您可以这样收集它们:
BEGIN
dbms_stats.gather_schema_stats(
ownname => 'HR'
, estimate_percent => X -- note the higher you go here, the more work will be done
);
END;
Run Code Online (Sandbox Code Playgroud)
然后,查询统计数据,而不是您的表格。
SELECT num_rows
, table_name
FROM sys.dba_all_tables
WHERE owner = 'HR'
Run Code Online (Sandbox Code Playgroud)
然后,您可以根据统计数据的准确程度快速查看哪些表为空或不为空。
如果您使用动态 SQL 对架构中的每个表运行 SELECT COUNT(*),并且您的架构和/或表很大,那么您将获得非常糟糕的体验,并且可能会导致数据库出现性能问题。
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |