我正在维护一个通过JDBC创建Oracle DB的应用程序。从今天开始这个查询:
SELECT NULL AS pktable_cat ,
p.owner AS pktable_schem,
p.table_name AS pktable_name ,
pc.column_name AS pkcolumn_name,
NULL AS fktable_cat ,
f.owner AS fktable_schem,
f.table_name AS fktable_name ,
fc.column_name AS fkcolumn_name,
fc.position AS key_seq ,
NULL AS update_rule ,
DECODE (f.delete_rule, 'CASCADE', 0, 'SET NULL', 2, 1) AS delete_rule ,
f.constraint_name AS fk_name ,
p.constraint_name AS pk_name ,
DECODE(f.deferrable, 'DEFERRABLE',5 ,'NOT DEFERRABLE',7 , 'DEFERRED', 6 ) deferrability
FROM all_cons_columns pc,
all_constraints p ,
all_cons_columns fc,
all_constraints f
WHERE 1 = 1
AND p.table_name = :1
AND p.owner = :3
AND f.constraint_type = 'R'
AND p.owner = f.r_owner
AND p.constraint_name = f.r_constraint_name
AND p.constraint_type = 'P'
AND pc.owner = p.owner
AND pc.constraint_name = p.constraint_name
AND pc.table_name = p.table_name
AND fc.owner = f.owner
AND fc.constraint_name = f.constraint_name
AND fc.table_name = f.table_name
AND fc.position = pc.position
ORDER BY fktable_schem,
fktable_name ,
key_seq
由于某些oracle内部构件,开始变得非常慢,因为它似乎对我所有的分支都是一样的。
有人知道一个可能的原因以及如何面对这个问题吗?
此致 南兹奥
答案 0 :(得分:8)
数据字典或固定对象统计信息可能很旧,请尝试重新收集它们:
exec dbms_stats.gather_dictionary_stats;
exec dbms_stats.gather_fixed_objects_stats;
alter system flush shared_pool;
即使这样也不一定会收集所有系统对象的统计信息。必须手动收集某些对象,如X$KFTBUE
。虽然这是一个罕见的数据字典问题,但这里可能并不相关。
如果这不起作用,则下一步可能的步骤是查看SQL Tuning Advisor等工具来创建配置文件,或使用SQL Plan Management强制优化器使用以前工作过的特定计划。调整数据字典查询可能非常困难,因为您没有太多控制权。
答案 1 :(得分:2)
这是另一个更优雅的解决方案..我发现强制带有sql补丁的规则库优化器也可以工作。需要2个补丁,因为有时jdbc驱动程序使用:1和:3作为绑定变量,有时它使用: 2& :4 .. SQL必须完全匹配才能使补丁工作。
在您的数据库中以sysdba ..
运行它 begin
dbms_sqldiag_internal.i_create_patch (
sql_text =>'SELECT NULL AS pktable_cat,
p.owner as pktable_schem, p.table_name as pktable_name,
pc.column_name as pkcolumn_name, NULL as fktable_cat, f.owner as
fktable_schem, f.table_name as fktable_name,
fc.column_name as fkcolumn_name, fc.position as key_seq, NULL as
update_rule, decode
(f.delete_rule, ''CASCADE'', 0, ''SET NULL'', 2, 1) as delete_rule,
f.constraint_name as fk_name, p.constraint_name as pk_name,
decode(f.deferrable, ''DEFERRABLE'',5 ,''NOT DEFERRABLE'',7 , ''DEFERRED'', 6)
deferrability
FROM all_cons_columns pc, all_constraints p, all_cons_columns fc,
all_constraints f
WHERE 1 = 1 AND p.table_name = :1 AND p.owner = :3 AND
f.constraint_type = ''R'' AND p.owner = f.r_owner AND
p.constraint_name = f.r_constraint_name AND p.constraint_type = ''P''
AND pc.owner = p.owner AND pc.constraint_name = p.constraint_name AND
pc.table_name = p.table_name AND fc.owner = f.owner AND
fc.constraint_name = f.constraint_name AND
fc.table_name = f.table_name AND fc.position = pc.position
ORDER BY fktable_schem, fktable_name, key_seq' ,
hint_text => 'RULE',
name => 'jdbcpatch');
end;
/
begin
dbms_sqldiag_internal.i_create_patch (
sql_text =>'SELECT NULL AS pktable_cat,
p.owner as pktable_schem, p.table_name as pktable_name,
pc.column_name as pkcolumn_name, NULL as fktable_cat, f.owner as
fktable_schem, f.table_name as fktable_name,
fc.column_name as fkcolumn_name, fc.position as key_seq, NULL as
update_rule, decode
(f.delete_rule, ''CASCADE'', 0, ''SET NULL'', 2, 1) as delete_rule,
f.constraint_name as fk_name, p.constraint_name as pk_name,
decode(f.deferrable, ''DEFERRABLE'',5 ,''NOT DEFERRABLE'',7 , ''DEFERRED'', 6)
deferrability
FROM all_cons_columns pc, all_constraints p, all_cons_columns fc,
all_constraints f
WHERE 1 = 1 AND p.table_name = :2 AND p.owner = :4 AND
f.constraint_type = ''R'' AND p.owner = f.r_owner AND
p.constraint_name = f.r_constraint_name AND p.constraint_type = ''P''
AND pc.owner = p.owner AND pc.constraint_name = p.constraint_name AND
pc.table_name = p.table_name AND fc.owner = f.owner AND
fc.constraint_name = f.constraint_name AND
fc.table_name = f.table_name AND fc.position = pc.position
ORDER BY fktable_schem, fktable_name, key_seq' ,
hint_text => 'RULE',
name => 'jdbcpatch2');
end;
/
答案 2 :(得分:2)
问题中的查询是通过调用java.sql.DatabaseMetaData.getExportedKeys()
生成的,Easiest way to obtain database metadata in Java?委托oracle.jdbc.OracleDatabaseMetaData.getExportedKeys()
枚举引用给定表的外键。
如@ Jon的回答所述,Oracle有时会使用次优计划来解决此问题,这可能会或可能不会通过收集统计信息来避免。
如果可以更改代码,可以使用其他替代方法:
第二个选项是CORE-1844项目选择的,该项目过去曾在旧版本中调用DatabaseMetaData
。
新版本使用来自_.initial(array)
的正确联接的优化查询:
SELECT NULL AS pktable_cat, p.owner as pktable_schem,
p.table_name as pktable_name, pc.column_name as pkcolumn_name,
NULL as fktable_cat, f.owner as fktable_schem, f.table_name as fktable_name,
fc.column_name as fkcolumn_name, fc.position as key_seq, NULL as update_rule,
decode (f.delete_rule, 'CASCADE', 0, 'SET NULL', 2, 1) as delete_rule,
f.constraint_name as fk_name, p.constraint_name as pk_name,
decode(f.deferrable, 'DEFERRABLE', 5, 'NOT DEFERRABLE', 7, 'DEFERRED', 6) deferrability
FROM all_constraints p
INNER JOIN all_cons_columns pc ON pc.owner = p.owner
AND pc.constraint_name = p.constraint_name
AND pc.table_name = p.table_name
INNER JOIN all_constraints f ON p.owner = f.r_owner
AND p.constraint_name = f.r_constraint_name
INNER JOIN all_cons_columns fc ON fc.owner = f.owner
AND fc.constraint_name = f.constraint_name
AND fc.table_name = f.table_name
AND fc.position = pc.position
WHERE p.owner = :jdbcSchemaName
AND p.constraint_type in ('P', 'U')
AND f.constraint_type = 'R'
ORDER BY fktable_schem, fktable_name, key_seq
答案 3 :(得分:1)
我发现这种方式作弊..在你对程序进行反向工程之前,以jdbc连接的用户运行它。
CREATE TABLE all_constraints AS
SELECT owner,
constraint_name,
constraint_type,
table_name,
r_owner,
r_constraint_name,
delete_rule,
status,
deferrable,
deferred,
validated,
generated,
bad,
rely,
last_change,
index_owner,
index_name,
invalid,
view_related
FROM all_constraints;
CREATE TABLE all_cons_columns AS
SELECT *
FROM all_cons_columns;
CREATE INDEX ac1
ON all_constraints (owner, constraint_name, table_name);
CREATE INDEX acc1
ON all_cons_columns (owner, constraint_name, table_name);
然后有问题的查询确实尖叫..缺点是你必须不时刷新它...也许使它成为物化视图?
答案 4 :(得分:0)