【问题标题】:SQL query hangs on joinSQL 查询在连接时挂起
【发布时间】:2010-11-07 12:18:47
【问题描述】:

我编写了一个 SQL 查询,它为每个 Year-Week-Mine-Product 生成一些统计数据报告。

除了一件事之外,它的工作原理完全符合预期 - trn.wid-date 不是要使用的正确日期。

我应该使用 td.datetime-act-comp-dump。当我用 td.datetime-act-comp-dump 替换 trn.wid-date 时,它不会给我任何错误,但似乎只是无限期地挂起。我昨天放了一段时间,它回来了 ORA-01652 无法在表空间 TEMP 中将临时段扩展 128,尽管从那以后我没有看到这个错误。

考虑到我能够在下面的查询中成功返回 MAX(td.datetime-act-comp-dump),我不明白是什么原因造成的

select to_char(trn.wid_date, 'IYYY') as dump_year,
       to_char(trn.wid_date-7/24, 'IW') as dump_week,
       SUBSTR(trn.train_control_id,1,2) as Mine,
       vcon.product_type_code as Product,
       COUNT(DISTINCT trn.train_control_id) as Trains,
       COUNT(1) as Wagons,
       MIN(trn.wid_date) as Min_WID_Hrs,
       MAX(trn.wid_date) as Max_WID_Hrs,
       MIN(td.datetime_act_comp_dump) as Min_Fin_Dump,
       MAX(td.datetime_act_comp_dump) as Max_Fin_Dump,        
       ROUND(SUM(con.weight_total-con.empty_weight_total),0) as Tot_Tonnes,       
       ROUND(AVG(con.weight_total-con.empty_weight_total),2) as Avg_Tonnes,
       ROUND(MIN(con.weight_total-con.empty_weight_total),2) as Minimum,
       ROUND(PERCENTILE_DISC(0.99) WITHIN GROUP (ORDER BY (con.weight_total-con.empty_weight_total) DESC),2) as "1st",      

from  widsys.consist con
      INNER JOIN widsys.train trn
      USING (train_record_id)
      INNER JOIN tpps.train_details td
      ON trn.train_tpps_id||trn.mine_code = td.train_id||td.mine_code
      INNER JOIN widsys.v_consist_ore_detail vcon
      USING (consist_id)

where trn.direction = 'N'
      and to_char(trn.wid_date, 'IYYY') = 2009
      and to_char(trn.wid_date-7/24, 'IW') = 25

group by to_char(trn.wid_date, 'IYYY'),
         to_char(trn.wid_date-7/24, 'IW'),
         SUBSTR(trn.train_control_id,1,2),
         vcon.product_type_code

order by to_char(trn.wid_date-7/24, 'IW') DESC

为了排除故障,从上面的查询中,我尝试删除与 vcon 相关的所有内容,并将 trn.wid_date 替换为 td.datetime -act-comp-dump。效果是它只报告 Year-Week-Mine 而不是 Year-Week-Mine-Product。 (请参阅下面的查询)

这个新查询实际上会执行,而不仅仅是挂起,但会返回一些奇怪的结果,而且还不够,因为它不会对 Product 进行分解。

select to_char(td.datetime_act_comp_dump, 'IYYY') as dump_year,
       to_char(td.datetime_act_comp_dump-7/24, 'IW') as dump_week,
       SUBSTR(trn.train_control_id,1,2) as Mine,
       --vcon.product_type_code as Product,
       COUNT(DISTINCT trn.train_control_id) as Trains,
       COUNT(1) as Wagons,
       MIN(trn.wid_date) as Min_WID_Hrs,
       MAX(trn.wid_date) as Max_WID_Hrs,
       MIN(td.datetime_act_comp_dump) as Min_Fin_Dump,
       MAX(td.datetime_act_comp_dump) as Max_Fin_Dump,        
       ROUND(SUM(con.weight_total-con.empty_weight_total),0) as Tot_Tonnes,       
       ROUND(AVG(con.weight_total-con.empty_weight_total),2) as Avg_Tonnes,
       ROUND(MIN(con.weight_total-con.empty_weight_total),2) as Minimum,
       ROUND(PERCENTILE_DISC(0.99) WITHIN GROUP (ORDER BY (con.weight_total-con.empty_weight_total) DESC),2) as "1st"       

from  widsys.consist con
      INNER JOIN widsys.train trn
      USING (train_record_id)
      INNER JOIN tpps.train_details td
      ON trn.train_tpps_id||trn.mine_code = td.train_id||td.mine_code
      --INNER JOIN widsys.v_consist_ore_detail vcon
      --USING (consist_id)

where trn.direction = 'N'
      and to_char(td.datetime_act_comp_dump, 'IYYY') = 2009
      and to_char(td.datetime_act_comp_dump-7/24, 'IW') = 25

group by to_char(td.datetime_act_comp_dump, 'IYYY'),
         to_char(td.datetime_act_comp_dump-7/24, 'IW'),
         SUBSTR(trn.train_control_id,1,2)
         --vcon.product_type_code

order by to_char(td.datetime_act_comp_dump-7/24, 'IW') DESC

对可能出现的问题有什么建议吗?

干杯,

汤米

【问题讨论】:

    标签: sql join timeout ora-01652


    【解决方案1】:

    如果没有更多信息,我唯一能想到的是train_detailsdatetime_act_comp_dump 列没有被索引,而wid_date 是。这听起来像是一个非常正常的性能问题,其中某些内容未编入索引,或者 traintrain_details 表的大小差异很大,并且您的连接正在爆炸。

    我不确定您使用的是哪个数据库,但您可能想弄清楚如何运行查询执行计划分析器并查看两个执行计划之间的区别。我怀疑答案将是结构性的,或者可能是连接语句中的连接导致了一些特定于数据库的问题。

    【讨论】:

      【解决方案2】:

      通过为 widsys 表和 tpps 表创建一个子查询,我设法让它更快地运行 muuuuuuuch。然后在两列上进行隐式内连接,而不是连接。

      SELECT blah FROM (widsys subquery) w, (tpps subquery) t WHERE w.mine_code = t.mine_code and w.train_id = t.train_tpps_id
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多