简体   繁体   English

如何加快查询

[英]How to Speed up the query

I am trying to create a table with below sql and it is running for days. 我试图用下面的sql创建一个表,它运行了几天。

Below is the sql. 下面是sql。

create table I83094_Emnmt1 AS
    Select 
     'I83094'                                              AS Audit_Id
     ,rap01.plcy                                           AS plcy
     ,raa02.enddt_t                                        As enddt
     ,RPAD(NVU_GET_TERM_ID(RAP01.PLCY, Raa02.ENDDT_T),1)   AS Term_Id
     ,nvu_get_nxt_proc_enddt(RAP01.PLCY, Raa02.ENDDT_T)    As next_enddt
     ,exception_date                                       AS exception_date
     ,RAP07.MVRDT_t
     ,RAP07.MVRDT_s
     ,rap07a.prefdt_t
     ,RAP07.MRTLST
     ,RAP07.BRTHDT_T                                       AS BirthDate
     ,floor((tt.ja2_effdt_t - RAP07.BRTHDT_T)/365 )        AS Operator_Age
     ,rap07a.perseq
     ,tt.ja2_effdt_t                                       AS TERM_DATE
     ,rap01.j01_PT_LINE_cat_Cd                             AS j01_PT_LINE_cat_Cd
     ,rap01.j01_pt_cdb_part_id                             AS j01_pt_cdb_part_id
     ,Rap01.J01_Pt_State_Cd                                As J01_Pt_State_Cd
     ---
from RAP01
----
Join RAA02
  ON raa02.j46_pt_line_cat_cd  = rap01.j01_pt_line_cat_cd
AND raa02.j46_pt_cdb_part_id   = rap01.j01_pt_cdb_part_id
AND raa02.j46_pt_state_cd      = rap01.j01_pt_state_cd
AND raa02.plcy                 = rap01.plcy
AND raa02.sprodt_t  between '14-OCT-2013' AND '14-OCT-2018'
AND raa02.ahevnt               = '0993'
---
JOIN ewt_prama_term tt
  ON tt.ja2_pt_line_cat_cd     = rap01.j01_pt_line_cat_cd
AND tt.ja2_pt_cdb_part_id      = rap01.j01_pt_cdb_part_id
AND tt.ja2_pt_state_cd         = rap01.j01_pt_state_cd
AND tt.ja2_plcy                = rap01.plcy
and raa02.enddt_t              between tt.ja2_effdt_t and tt.ja2_expdt_t - 1
-----
JOIN rap07a
  ON rap07a.j36_pt_line_cat_cd = rap01.j01_pt_line_cat_cd
AND rap07a.j36_pt_cdb_part_id  = rap01.j01_pt_cdb_part_id 
AND rap07a.j36_pt_state_cd     = rap01.j01_pt_state_cd
AND rap07a.plcy                = rap01.plcy
--and RAP07a.perseq              = rap18.perseq
AND raa02.enddt_t  BETWEEN rap07a.prefdt_t AND (rap07a.dropdt_t  - 1)
----
JOIN RAP07
  ON  RAP07.J37_PT_LINE_CAT_CD =  rap01.j01_pt_line_cat_cd   
AND  RAP07.J37_PT_CDB_PART_ID  =  rap01.j01_pt_cdb_part_id  
AND  RAP07.J37_PT_STATE_CD     =  rap01.j01_pt_state_cd     
AND  rap07.plcy                =  RAP01.PLCY
AND  RAP07.perseq              =  rap07a.perseq
AND RAP07.MRTLST in ('MA','IC','DC','WC','EC','DP')
--AND RAP07.DRVDES_01 IN ('R','P')  
--AND RAP07.MVRDT_T < (select exception_date from I83094_exception)
AND floor((tt.ja2_effdt_t - RAP07.BRTHDT_T)/365) < 50
AND  raa02.enddt_t BETWEEN RAP07.enddt_t AND (RAP07.dropdt_t  - 1)
---
join I83094_exception exp
  ON exp.line_cd               = rap01.line3
AND exp.state_cd               = rap01.state
AND exp.company_cd             = rap01.co3
AND exp.marital_status_cd      = RAP07.MRTLST
---
WHERE Rap01.J01_Pt_Line_Cat_Cd = 'A'
AND Rap01.Line3               IN ('010','019')
AND RAP07.MVRDT_T < exp.exception_date;

field 'J01_Pt_State_Cd' represents the different states, this table contains billions of data, and will take a lot of time to execute. 字段“ J01_Pt_State_Cd”表示不同的状态,该表包含数十亿数据,并且将花费大量时间来执行。 I tried to create table but have to wait for 2 or 3 days. 我尝试创建表,但必须等待2或3天。 How can I improve the query so that I can perform the query in less time. 如何改善查询,以便可以在更短的时间内执行查询。

The group by works with grouping functions, like sum , max , min and count . group by具有分组功能,例如summaxmincount When you use some of these functions, you normally want to sum grouping by some attribute, for example sum(sales) group by vendor . 使用这些功能中的某些功能时,通常需要按某个属性对分组求和,例如, sum(sales) group by vendor

When you use this keyword in a query without grouping functions, it works like a distinct , but you have to include all your fields in the group by. 当您在不带分组功能的查询中使用此关键字时,它的工作原理distinct ,但您必须在分组依据中包括所有字段。

The error that you are getting is just that, you are not including all your fields in your group by . 您得到的错误仅仅是,您没有将所有字段包括在group by

I think you are misunderstanding what a group by expression does. 我认为您误会了一个按表情分组的行为。

From your comments I assume you are trying to run this on a large amount of data and it is taking a long time. 根据您的评论,我认为您正在尝试对大量数据运行此操作,这需要很长时间。 A group by will not help this, group by expressions are used to aggregate data over certain fields, for example if I wanted to find out how long each user has spent logged on to a service I could use a group by on the username. group by不能帮上忙,group by表达式用于汇总某些字段上的数据,例如,如果我想找出每个用户登录服务花费了多长时间,我可以在用户名上使用group by。

In order to speed up your query I'd recommend the standard stuff, make sure your tables are indexed properly and make sure all of those joins are necessary. 为了加快查询速度,我建议您使用标准的东西,请确保您的表已正确编制索引,并确保所有这些连接都是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM