【问题标题】:How to reduce the number of multiple left join on the same two tables如何减少相同两个表上的多个左连接数
【发布时间】:2022-09-30 19:09:11
【问题描述】:

查询需要很长时间才能执行

我正在尝试减少使用的左连接的数量,因为我要加入相同的两个表,但执行需要很长时间。我需要这个问题的提示或解决方案。由于我正在比较两个表中的日期,我认为我不能按子句进行分组。提前谢谢你 -------------------------------------------------- ----------------------

 select t1.comment
        from comments_aux t1
        where event_comment between  
to_date('20220205000000','yyyymmddhh24miss')  AND (to_date('20220202000000','yyyymmddhh24miss')  + 1)

【问题讨论】:

  • 嗨,您能否提供一些信息,例如表结构、示例数据和预期输出。
  • 顺便说一句,to_date('20220205000000','yyyymmddhh24miss') 可以更简单地写成date '2022-02-05'

标签: sql oracle performance query-optimization


【解决方案1】:

您可以尝试使用CASE 语句。

select t1.c_instalation
         , t1.N_cpe
         , t1.EQuipment
         , t1.grupos
         , t1.D_EVENT_START
         , t1.D_EVENT_END
         , Z_EVENT_START
         , Z_EVENT_END
         , DURATION
         ,(CASE WHEN tz.z_ts_read < t1.Z_EVENT_START - (1/1440*30) AND tz.z_ts_read >= t1.Z_EVENT_START - (1/1440*45) THEN tz.m_read END) reading_45_before
         ,(CASE WHEN tz.z_ts_read < t1.Z_EVENT_START - (1/1440*15) AND tz.z_ts_read >= t1.Z_EVENT_START - (1/1440*30) THEN tz.m_read END) reading_30_before
         ,(CASE WHEN tz.z_ts_read < t1.Z_EVENT_START AND tz.z_ts_read >= t1.Z_EVENT_START - (1/1440*15) THEN tz.m_read END) reading_15_before
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*15) THEN tz.m_read END) reading_15_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*15) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*30) THEN tz.m_read END) reading_30_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*30) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*45) THEN tz.m_read END) reading_45_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*45) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*60) THEN tz.m_read END) reading_60_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*60) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*75) THEN tz.m_read END) reading_75_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*75) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*90) THEN tz.m_read END) reading_90_after
         ,(CASE WHEN tz.z_ts_read > t1.Z_EVENT_START + (1/1440*90) AND tz.z_ts_read <= t1.Z_EVENT_START + (1/1440*105) THEN tz.m_read END) reading_105_after
       from pares_aux t1
       left join  DC tz
       on t1.n_cpe = tz.c_instalation
       and t1.n_equipment=tz.n_equipment
       where Z_EVENT_START between  
to_date('20201002000000','yyyymmddhh24miss')  AND (to_date('20201002000000','yyyymmddhh24miss')  + 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2022-08-09
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多