首页 > 解决方案 > Oracle select * from all_sequences 太慢了

问题描述

我的项目使用 Hibernate,我想增加启动时间(目前是 1.5 分钟),所以我检查了启动时发生的情况。在SessionFactory初始化期间,发出的查询之一是:

select * from all_sequences;

需要将近一分钟!来自 Oracle SQL Developer 的相同查询需要相似的时间。返回的记录总数为 102。

还有其他正常运行的查询(一位数毫秒的响应时间)

为什么这么慢?

标签: javaoraclehibernate

解决方案


收集数据字典和固定对象的优化器统计信息:

begin
    dbms_stats.gather_dictionary_stats;
    dbms_stats.gather_fixed_objects_stats;
end;
/

Oracle 需要良好的对象统计信息才能构建良好的执行计划。有很多机制可以为我们的自定义对象收集统计信息,但有时我们也需要为系统对象收集统计信息。(虽然我很惊讶这是开箱即用的必要条件。通常这些问题只发生在极端变化之后,比如创建一百万个新序列。)

如果收集优化器统计信息没有帮助,请尝试使用以下步骤生成执行计划,并将结果发布到问题中。

--Run the query:
select /*+ gather_plan_statistics */ * from all_sequences;

--Find the SQL_ID:
select * from gv$sql where sql_text like '%gather_plan_statistics%';

--Generate the execution plan, with estimated and actual results.
select *
from table(dbms_xplan.display_cursor(sql_id => '9wgbmhhrf0bwr', format=>'ALLSTATS LAST'));

推荐阅读