【问题标题】:Need to optimize Teradata query需要优化 Teradata 查询
【发布时间】:2014-03-05 04:15:48
【问题描述】:

我正在尝试优化以下 teradata 查询。任何人都可以请这个。检索记录需要花费大量时间。

   select top 100 I.item_sku_nbr,L.loc_nbr,MIS.MVNDR_PRTY_ID from 
QA_US_MASTER_VIEWS.item I,
qa4_US_MASTER_VIEWS.location L,
qa4_US_MASTER_VIEWS.item_str IST,
qa4_US_MASTER_VIEWS.mvndr_item_str MIS 
    where  MIS.str_LOC_ID = L.loc_id and    
mis.str_loc_id = IST.str_loc_id and     
IST.str_loc_id = L.loc_id and   
MIS.ITEM_STAT_CD = IST.ITEM_STAT_CD and     
IST.ITEM_ID = I.ITEM_ID and     
MIS.ITEM_ID = IST.ITEM_ID       and     
I.ITEM_STAT_CD =  100           and 
IST.curr_rmeth_cd = 2           and
MIS.curr_dsvc_typ_cd = 3        and 
MIS.OK_TO_ORD_FLG = 'Y'  and        
MIS.EFF_END_DT = DATE '9999-12-31' and  
IST.EFF_END_DT = DATE '9999-12-31' and 
MIS.ACTV_FLG ='Y' and       
IST.ACTV_FLG ='Y'  and  I.ACTV_FLG='Y'  

解释QA_US_MASTER.LOCATION的计划:

1) 首先,我们将 QA_US_MASTER.LOCATION 锁定在视图中 qa4_US_MASTER_VIEWS. 访问位置。

2) 接下来,我们从 QA_US_MASTER.LOCATION 执行全 AMP 检索步骤 通过全行扫描查看 qa4_US_MASTER_VIEWS.Location Spool 1 (group_amps) 中没有剩余条件,即 在 AMP 上本地构建。 Spool 1 的大小估计为 高置信度为 10,903 行(1,613,644 字节)。这 此步骤的估计时间为 0.01 秒。

3) 最后,我们向所有涉及的 AMP 发送一个 END TRANSACTION 步骤 正在处理请求。

-> Spool 1 的内容作为以下结果发送回用户 声明 1. 总估计时间为 0.01 秒。

解释 qa4_US_MASTER_VIEWS.item_str 的计划:

1) 首先,我们将 QA_US_MASTER.item_str 锁定在视图中 qa4_US_MASTER_VIEWS.item_str 用于访问。

2) 接下来,我们从 QA_US_MASTER.item_str 执行全 AMP 检索步骤 通过全行扫描查看 qa4_US_MASTER_VIEWS.item_str Spool 1 (group_amps) 中没有剩余条件,即 在 AMP 上本地构建。输入表不会被缓存在 内存,但它有资格进行同步扫描。结果 spool 文件不会缓存在内存中。线轴 1 的尺寸为 低置信度估计为 1,229,047,917 行( 325,697,698,005 字节)。此步骤的估计时间为 4 分 51 秒。

3) 最后,我们向所有涉及的 AMP 发送一个 END TRANSACTION 步骤 正在处理请求。

-> Spool 1 的内容作为以下结果发送回用户 声明 1. 总估计时间为 4 分 51 秒。

解释QA_US_MASTER.ITEM的计划:

1) 首先,我们在视图 qa4_US_MASTER_VIEWS.item 中锁定 QA_US_MASTER.ITEM 访问。

2) 接下来,我们从 QA_US_MASTER.ITEM 中执行全 AMP 检索步骤 通过全行扫描查看 qa4_US_MASTER_VIEWS.item 剩余条件进入阀芯 1 (group_amps),它是内置的 本地在 AMP 上。 Spool 1 的尺寸估计为高 置信度为 1,413,284 行(357,560,852 字节)。这 此步骤的估计时间为 0.40 秒。

3) 最后,我们向所有涉及的 AMP 发送一个 END TRANSACTION 步骤 正在处理请求。

-> Spool 1 的内容作为以下结果发送回用户 声明 1. 总估计时间为 0.40 秒。

解释QA_US_MASTER.MVNDR_ITEM_STR 的计划:

1) 首先,我们将 QA_US_MASTER.MVNDR_ITEM_STR 锁定在视图中 qa4_US_MASTER_VIEWS.mvndr_item_str 用于访问。

2) 接下来,我们从 QA_US_MASTER.MVNDR_ITEM_STR 在视图中 qa4_US_MASTER_VIEWS.mvndr_item_str 通过全行扫描 Spool 1 (group_amps) 中没有剩余条件,这是内置的 本地在 AMP 上。输入表不会缓存在内存中, 但它有资格进行同步扫描。结果卷轴 文件不会缓存在内存中。线轴 1 的尺寸为 以高置信度估计为 1,316,279,746 行( 327,753,656,754 字节)。此步骤的估计时间为 6 分 4 秒。

3) 最后,我们向所有涉及的 AMP 发送一个 END TRANSACTION 步骤 正在处理请求。

-> Spool 1 的内容作为以下结果发送回用户 声明 1. 总估计时间为 6 分 4 秒。

解释整个查询的计划:

  1) First, we lock QA_US_MASTER.ITEM in view QA_US_MASTER_VIEWS.item
  for access, we lock QA_US_MASTER.LOCATION in view
  qa4_US_MASTER_VIEWS.location for access, we lock
  QA_US_MASTER.MVNDR_ITEM_STR in view
  qa4_US_MASTER_VIEWS.mvndr_item_str for access, and we lock
  QA_US_MASTER.item_str in view qa4_US_MASTER_VIEWS.item_str for
  access. 

  2) Next, we execute the following steps in parallel. 
   1) We do an all-AMPs RETRIEVE step from QA_US_MASTER.LOCATION in
      view qa4_US_MASTER_VIEWS.location by way of an all-rows scan
      with no residual conditions into Spool 3 (all_amps)
      (compressed columns allowed), which is duplicated on all AMPs. 
      The size of Spool 3 is estimated with high confidence to be
      1,013,979 rows (20,279,580 bytes).  The estimated time for
      this step is 0.03 seconds. 

    2) We do an all-AMPs RETRIEVE step from QA_US_MASTER.ITEM in
      view QA_US_MASTER_VIEWS.item by way of an all-rows scan with
      a condition of ("(QA_US_MASTER.ITEM in view
      QA_US_MASTER_VIEWS.item.ITEM_STAT_CD = 100) AND
      (QA_US_MASTER.ITEM in view QA_US_MASTER_VIEWS.item.ACTV_FLG =
      'Y')") into Spool 4 (all_amps) (compressed columns allowed)
      fanned out into 14 hash join partitions, which is duplicated
      on all AMPs.  The size of Spool 4 is estimated with low
      confidence to be 30,819,363 rows (678,025,986 bytes).  The
      estimated time for this step is 0.81 seconds. 

     3) We do an all-AMPs JOIN step from Spool 3 (Last Use) by way of an
    all-rows scan, which is joined to QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str by way of an all-rows scan with a
    condition of 
    ("(QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.CURR_RMETH_CD = 2) AND
     ((QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.EFF_END_DT = DATE '9999-12-31') AND
    (QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.ACTV_FLG = 'Y'))").  Spool 3 and
    QA_US_MASTER.item_str are joined using a dynamic hash join, with a
    join condition of ("QA_US_MASTER.item_str.STR_LOC_ID = LOC_ID"). 
    The input table QA_US_MASTER.item_str will not be cached in memory. 
    The result goes into Spool 5 (all_amps) (compressed columns
    allowed), which is built locally on the AMPs into 14 hash join
    partitions.  The size of Spool 5 is estimated with no confidence
    to be 69,133,946 rows (2,419,688,110 bytes).  The estimated time
    for this step is 1 minute and 8 seconds. 

    4) We do an all-AMPs JOIN step from Spool 4 (Last Use) by way of an
   all-rows scan, which is joined to Spool 5 (Last Use) by way of an
   all-rows scan.  Spool 4 and Spool 5 are joined using a hash join
   of 14 partitions, with a join condition of ("(ITEM_ID = ITEM_ID)
   AND (ACTV_FLG = ACTV_FLG)").  The result goes into Spool 6
   (all_amps) (compressed columns allowed), which is redistributed by
   the hash code of (QA_US_MASTER.item_str.STR_LOC_ID,
   QA_US_MASTER.item_str.ITEM_STAT_CD, QA_US_MASTER.item_str.ITEM_ID,
   QA_US_MASTER.ITEM.ITEM_ID, QA_US_MASTER.LOCATION.LOC_ID) to all
    AMPs into 33 hash join partitions.  The size of Spool 6 is
   estimated with no confidence to be 36,434,893 rows (1,603,135,292
   bytes).  The estimated time for this step is 9.11 seconds. 

    5) We do an all-AMPs RETRIEVE step from QA_US_MASTER.MVNDR_ITEM_STR
   in view qa4_US_MASTER_VIEWS.mvndr_item_str by way of an all-rows
   scan with a condition of ("(QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.CURR_DSVC_TYP_CD = 3) AND
    ((QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.EFF_END_DT = DATE '9999-12-31')
   AND ((QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.ACTV_FLG = 'Y') AND
   (QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.OK_TO_ORD_FLG = 'Y')))") into
   Spool 7 (all_amps) (compressed columns allowed) fanned out into 33
   hash join partitions, which is redistributed by the hash code of (
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.STR_LOC_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_STAT_CD,
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.STR_LOC_ID) to all AMPs.  The input
    table will not be cached in memory, but it is eligible for
   synchronized scanning.  The size of Spool 7 is estimated with no
   confidence to be 173,967,551 rows (5,914,896,734 bytes).  The
   estimated time for this step is 2 minutes and 23 seconds. 

   6) We do an all-AMPs JOIN step from Spool 6 (Last Use) by way of an
   all-rows scan, which is joined to Spool 7 (Last Use) by way of an
   all-rows scan.  Spool 6 and Spool 7 are joined using a hash join
   of 33 partitions, with a join condition of ("(STR_LOC_ID =
   STR_LOC_ID) AND ((ITEM_STAT_CD = ITEM_STAT_CD) AND ((ITEM_ID =
   ITEM_ID) AND ((ACTV_FLG = OK_TO_ORD_FLG) AND ((ACTV_FLG = ACTV_FLG)
   AND ((EFF_END_DT = EFF_END_DT) AND ((ACTV_FLG = ACTV_FLG) AND
   ((OK_TO_ORD_FLG = ACTV_FLG) AND ((ITEM_ID = ITEM_ID) AND
   (STR_LOC_ID = LOC_ID )))))))))").  The result goes into Spool 2
   (all_amps) (compressed columns allowed), which is built locally on
   the AMPs.  The size of Spool 2 is estimated with no confidence to
   be 12,939,628 rows (336,430,328 bytes).  The estimated time for
   this step is 4.00 seconds. 

   7) We do an all-AMPs STAT FUNCTION step from Spool 2 by way of an
  all-rows scan into Spool 10, which is redistributed by hash code
  to all AMPs.  The result rows are put into Spool 1 (group_amps),
  which is built locally on the AMPs.  This step is used to retrieve
  the TOP 100 rows.  Load distribution optimization is used.
  If this step retrieves less than 100 rows, then execute step 8. 
  The size is estimated with no confidence to be 100 rows (3,200
  bytes). 

  8) We do an all-AMPs STAT FUNCTION step from Spool 2 (Last Use) by
  way of an all-rows scan into Spool 10 (Last Use), which is
  redistributed by hash code to all AMPs.  The result rows are put
   into Spool 1 (group_amps), which is built locally on the AMPs. 
  This step is used to retrieve the TOP 100 rows.  The size is
  estimated with no confidence to be 100 rows (3,200 bytes).

  9) Finally, we send out an END TRANSACTION step to all AMPs involved
  in processing the request.

 -> The contents of Spool 1 are sent back to the user as the result of
 statement 1. 

【问题讨论】:

  • 在每个基表上粘贴解释计划和统计数据
  • 整个查询的解释计划是什么?
  • 为整个查询添加了解释计划

标签: query-optimization teradata


【解决方案1】:

您的查询中没有 ORDER BY,所以您只需要 100 个随机行?

在 Teradata 中,TOP 在创建完整结果集之后完成。您应该将 TOP 移动到派生表中,例如:

select I.item_sku_nbr,L.loc_nbr,MIS.MVNDR_PRTY_ID from 
QA_US_MASTER_VIEWS.item I,
qa4_US_MASTER_VIEWS.location L,
(SELECT TOP 100 * FROM qa4_US_MASTER_VIEWS.item_str) IST,
qa4_US_MASTER_VIEWS.mvndr_item_str MIS 
    where  MIS.str_LOC_ID = L.loc_id and    
mis.str_loc_id = IST.str_loc_id and     
IST.str_loc_id = L.loc_id and   
MIS.ITEM_STAT_CD = IST.ITEM_STAT_CD and     
IST.ITEM_ID = I.ITEM_ID and     
MIS.ITEM_ID = IST.ITEM_ID       and     
I.ITEM_STAT_CD =  100           and 
IST.curr_rmeth_cd = 2           and
MIS.curr_dsvc_typ_cd = 3        and 
MIS.OK_TO_ORD_FLG = 'Y'  and        
MIS.EFF_END_DT = DATE '9999-12-31' and  
IST.EFF_END_DT = DATE '9999-12-31' and 
MIS.ACTV_FLG ='Y' and       
IST.ACTV_FLG ='Y'  and  I.ACTV_FLG='Y' 

【讨论】:

  • 随机行很好。但它在结尾显示语法错误- (SELECT TOP 100 * FROM qa4_US_MASTER_VIEWS.item_str IST), Expected identifier betwenn ) 和 ,
  • 糟糕,我把括号放错地方了,别名丢失了。编辑了我的查询
  • @dnoeth 使用sample 100 不会比top 100 快吗?
  • 可能不会更快,但更随机
猜你喜欢
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多