识别 Oracle SQL 开发人员中的非空表
Identifying nonempty tables in Oracle SQL Developer
我正在尝试在包含数百个表的数据库中查找数据。我正在为所有表编写简单的 select *
查询并一一检查。这需要永远!我想知道是否有一种方法可以编写查询来过滤数据库中至少有 1 行的所有表。这可能吗?
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;
结果:
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
或select count(*) from dual where exists(...)
:
with
function f(tabname in varchar2) return int as
res int;
begin
execute immediate 'select count(*) cnt from dual where exists(select 1 from "'||tabname||'")' into res;
return res;
end;
select table_name, f(table_name) chk
from user_tables
如果您的数据库维护得当,您将获得优化器统计信息。
如果您 table 缺少统计数据,您可以这样收集它们:
BEGIN
dbms_stats.gather_schema_stats(
ownname => 'HR'
, estimate_percent => X -- note the higher you go here, the more work will be done
);
END;
然后,查询统计数据,而不是您的 table。
SELECT num_rows
, table_name
FROM sys.dba_all_tables
WHERE owner = 'HR'
然后您可以根据您的统计数据的准确程度快速查看哪些 table 是空的。
如果您使用动态 SQL 到 运行 一个 SELECT COUNT(*) 在您的架构中的每个 table 和您的架构 and/or tables 是巨大的,你会有一个非常糟糕的体验,并且可能会导致你的数据库出现性能问题。
我正在尝试在包含数百个表的数据库中查找数据。我正在为所有表编写简单的 select *
查询并一一检查。这需要永远!我想知道是否有一种方法可以编写查询来过滤数据库中至少有 1 行的所有表。这可能吗?
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;
结果:
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
或select count(*) from dual where exists(...)
:
with
function f(tabname in varchar2) return int as
res int;
begin
execute immediate 'select count(*) cnt from dual where exists(select 1 from "'||tabname||'")' into res;
return res;
end;
select table_name, f(table_name) chk
from user_tables
如果您的数据库维护得当,您将获得优化器统计信息。
如果您 table 缺少统计数据,您可以这样收集它们:
BEGIN
dbms_stats.gather_schema_stats(
ownname => 'HR'
, estimate_percent => X -- note the higher you go here, the more work will be done
);
END;
然后,查询统计数据,而不是您的 table。
SELECT num_rows
, table_name
FROM sys.dba_all_tables
WHERE owner = 'HR'
然后您可以根据您的统计数据的准确程度快速查看哪些 table 是空的。
如果您使用动态 SQL 到 运行 一个 SELECT COUNT(*) 在您的架构中的每个 table 和您的架构 and/or tables 是巨大的,你会有一个非常糟糕的体验,并且可能会导致你的数据库出现性能问题。