【发布时间】:2011-10-06 18:04:45
【问题描述】:
我有一个查询需要很长时间才能在 Oracle 中完成。在我杀死它之前它运行了几个小时。有什么办法可以加快速度吗?
这是我的查询:
select distinct(random_selection.randnum),
random_selection.dropper_id,
random_selection.ozip3
from random_selection
where random_selection.dropper_id is not null
and random_selection.quarter = 121
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in
(select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq
from forecast_entry, production_weeks
where forecast_entry.week = production_weeks.production_week
and production_weeks.project_cd = 'EXFC'
and production_weeks.yyq >= 121)
union
select distinct(random_selection.randnum),
dropper_city_brk_2.dropper_id,
random_selection.ozip3
from random_selection, dropper_city_brk_2, dropper
where random_selection.ozip3 = dropper_city_brk_2.zip3
and dropper.dropper_id = dropper_city_brk_2.dropper_id
and dropper.active = 1
and dropper_city_brk_2.dropper_id <> 10002
and random_selection.quarter = 121
and random_selection.dropper_id is null
and (random_selection.dropper_id, random_selection.randnum, random_selection.quarter) in
(select forecast_entry.dropper, forecast_entry.rand_num, production_weeks.yyq
from forecast_entry, production_weeks
where forecast_entry.week = production_weeks.production_week
and production_weeks.project_cd = 'EXFC'
and production_weeks.yyq >= 121)
查询解释:
主要目标是从random_selection 表中获取所有randnum、dropper_id 和ozip3,它们不在forecast_entry 表中,并且在yyq 121 中并且具有EXFC 的项目代码。通过关联周和生产周从生产周表中检索 yyq。一些 dropper_id 为空,因此我们需要通过关联 ozip3 和 zip3 从 dropper_city_brk_2 表中提取该数据。我们不希望 dropper_id 处于非活动状态,因此它们的活动必须等于 1,这是通过关联 dropper 表来实现的。
希望对你有帮助
【问题讨论】:
-
您应该在问题中包含您的表架构以及您设置的任何索引。
-
这是我一生中见过的最大的 SQL 查询 :) 阅读它需要稍作休息
-
我见过更大的......
-
除了显示您的架构之外,还显示 EXPLAIN PLAN 的输出
-
迈克,我们并不都是通灵者,您需要发布表格结构、索引、表格大小并解释我们帮助您的计划。还要检查您的数据库统计信息是否是最新的。
标签: sql performance oracle