首页 > 解决方案 > 月度数据未正确反映

问题描述

需要最近四个月的数据:

select count(distinct session_id) 
from  master_gui partition for  (to_date('11-25-2020','MM-DD-YYYY')) 
where session_id in (select distinct session_id  
                    from reporting_data partition for (to_date('11-25-2020','MM-DD-YYYY')) 
where  flow_name in ('BEGIN_STATUS'));

上述查询中的任何建议如何包含过去 4 个月的日期。

CHECKED FROM BELOW 分区键值:SELECT OWNER, NAME, OBJECT_TYPE, COLUMN_NAME, COLUMN_POSITION FROM ALL_PART_KEY_COLUMNS

REPORTING_USER  REPORTING_DATA  TABLE   CREATE_TIME 1
REPORTING_USER  MASTER_GUI  TABLE   SESSION_START_TIME  1

使用以下查询获取最近 4 个月的记录(8 月、2 月、10 月和 11 月)

select count(distinct session_id) 
from master_gui  where  SESSION_START_TIME >= add_months(trunc(sysdate), -4)
and  session_id in (select distinct session_id  from reporting_data where CREATE_TIME>= add_months(trunc(sysdate), -4)

and   flow_name in ('BEGIN_STATUS'));

感谢专家,

更改后用于以下查询,是否正确:

因为我们必须从 master_gui 表中获取计数,所以使用它和父键值 SESSION_START_TIME 以及报告数据选项卡;e 父键值 CREATE_TIME。

 select count(distinct session_id) 
from master_gui  where   SESSION_START_TIME < trunc(sysdate,'mm') 
and   SESSION_START_TIME >= add_months( trunc(sysdate, 'mm'),-4)
and  session_id in (select distinct session_id  from REPORTING_DATA where  create_time < trunc(sysdate,'mm') 
and   create_time >= add_months( trunc(sysdate, 'mm'),-4)
and   flow_name in ('BEGIN_STATUS'));

谢谢专家,

下面是正确的,通过使用下面的查询,从子查询中删除不同的子句,可以获得更好的性能。

 select count(distinct session_id) 
from master_gui  where   SESSION_START_TIME < trunc(sysdate,'mm') 
and   SESSION_START_TIME >= add_months( trunc(sysdate, 'mm'),-4)
and  session_id in (select session_id  from REPORTING_DATA where  create_time < trunc(sysdate,'mm') 
and   create_time >= add_months( trunc(sysdate, 'mm'),-4)
and   flow_name in ('BEGIN_STATUS'));

感谢专家,

我只需要在分区中使用以获得更快的性能:

select count(distinct session_id) 
from master_gui  partition for  (to_date('11-01-2020','MM-DD-YYYY')) 
where session_id in (select distinct session_id  from reporting_data  partition for  (to_date('11-30-2020','MM-DD-YYYY')) 
where  flow_name in ('BEGIN_STATUS'));

以上查询在 2020 年 11 月 1 日至 2020 年 11 月 30 日期间是正确的。

标签: sqloracleoracle-sqldeveloperquery-optimizationpartitioning

解决方案


这部分查询意味着您仅从包含 2020 年 11 月 25 日值的分区中选择记录。

from reporting_data partition for  (to_date('11-25-2020','MM-DD-YYYY')) 

因此,如果您的表按每日间隔进行分区,您将仅获得 25 日的记录。如果分区键是每月,您将仅获得 11 月的记录。如果分区键是(比如说)年度,则使用此语法您只能获取过去四个月的记录。

解决方案是简单地省略分区子句并使用 WHERE 子句。

select count(distinct session_id) 
from  master_gui 
where session_id in (select distinct session_id  
                     from reporting_data partition 
                     where <<partition_key_column>> >= sysdate - interval '4' month)
where  flow_name in ('BEGIN_STATUS')
and    <<partition_key_column>> >= sysdate - interval '4' month;

此查询仍将使用分区修剪。


这是对的吗?

看起来像我建议的那样。但是,您已将“过去四个月”细化为过去四个完整的月份,即不包括当前月份。我的搜索条件包括当月。所以也许你真正需要的是

select session_id  
from reporting_data 
where create_time < trunc(sysdate,'mm') 
and   create_time >= add_months( trunc(sysdate, 'mm'),-4)

这将提供从 2020 年 8 月 1 日到 2020 年 11 月 30 日的跨度。

顺便说一句,您不需要子查询中的 DISTINCT。IN 子句将处理重复项,因此 DISTINCT 只会增加不必要的工作,如果您正在处理大量数据,这可能很重要。


推荐阅读