简体   繁体   English

最优优势 SQL 子查询

[英]Optimal Advantage SQL sub-query

I'm trying to optimize an SQL query for an Advantage 11 database.我正在尝试优化 Advantage 11 数据库的 SQL 查询。 In short, I'm trying to build a query to calculate the total number of steps in a manufacturing sequence and the number of those that have been completed.简而言之,我正在尝试构建一个查询来计算制造序列中的总步骤数以及已完成的步骤数。 The system supports multiple "releases" per job and multiple sub-assemblies per release.系统支持每个作业的多个“版本”和每个版本的多个子组件。 There are several tables that contain various values needed for the query.有几个表包含查询所需的各种值。 I have a query that works but it is way to slow to be of programmatic use.我有一个有效的查询,但它是程序化使用的缓慢方法。 The database tables involved are:涉及的数据库表有:

inproces (releases that are in process)
H-JOB#         HPROC-SEQ
ABC-0100-001   101
ABC-0100-002   101
DEF-0100-001   205
ABC-0100-001P1 302

release  (main release information, including release status)
R-JOB#     R-TRACKING-NBR    R-RELEASE-STATUS
ABC-0100   ABC-0100-001      Y
ABC-0100   ABC-0100-002      Y
DEF-0100   DEF-0100-001      Y
GHI-0100   GHI-0100-002      N

pnlrel   (sub-assembly release information, including release status)
P-JOB-NBR  P-TRACKING-NBR    P-RELEASED-FLAG
ABC-0100   ABC-0100-001P01   Y
DEF-0100   DEF-0100-001P01   Y
GHI-0100   GHI-0100-002P01   N

travdet  (process steps per job, per sequence)
Job-#          Process-Sequence   Gate ID    
ABC-0100-001   101                IS  
ABC-0100-001   101                DR
ABC-0100-001   101                PL
ABC-0100-001   101                SM
ABC-0100-001   101                GN
ABC-0100-001   103                IS  
ABC-0100-001   103                DR
ABC-0100-001   103                PL
ABC-0100-001   103                SM
ABC-0100-002   101                IS  
ABC-0100-002   101                DR
ABC-0100-002   101                PL
ABC-0100-002   101                SK
DEF-0100-001   205                AB
DEF-0100-001   205                CD
DEF-0100-001   205                EF
DEF-0100-001   205                GH
DEF-0100-001   205                IJ
DEF-0100-001   212                AB
DEF-0100-001   212                CD
DEF-0100-001   212                EF
DEF-0100-001   212                GH
DEF-0100-001   212                IJ
ABC-0100-001P1 302                QR
ABC-0100-001P1 302                ST
ABC-0100-001P1 302                UV
ABC-0100-001P1 302                WX
ABC-0100-001P1 302                YZ
ABC-0100-001P1 309                QR
ABC-0100-001P1 309                ST
ABC-0100-001P1 309                UV
ABC-0100-001P1 309                WX
ABC-0100-001P1 309                YZ

detail   (process steps completed per release)
D-JOB#         D-DEST
ABC-0100-001   IS  
ABC-0100-001   DR
ABC-0100-001   PL
DEF-0100-001   AB
DEF-0100-001   CD
DEF-0100-001   EF
DEF-0100-001   GH
ABC-0100-001P1 QR
ABC-0100-001P1 ST
ABC-0100-001P1 UV
ABC-0100-001P1 WX
ABC-0100-001P1 SK

history  (current process step)
S-JOB#      S-GATE  
ABC-0100-001   IJ
ABC-0100-002   SK
DEF-0100-001   GH
ABC-0100-001P1 SK

So for every record in "inproces":因此,对于“inproces”中的每条记录:

  • Determine if the release is actually released (and not cancelled) by verifying release.R-RELEASE-STATUS = Y (for main releases) or pnlrel.P-RELEASED-FLAG = Y (for sub-assemblies).通过验证 release.R-RELEASE-STATUS = Y(对于主要版本)或 pnlrel.P-RELEASED-FLAG = Y(对于子组件)来确定版本是否实际发布(而不是取消)。 I unioned two queries together to cover both of these criteria as a release will be in either the release OR pnlrel table, but not both.我将两个查询合并在一起以涵盖这两个条件,因为发布将在发布或 pnlrel 表中,但不会同时在两者中。

  • Verify process step "SK", which would indicate it has been moved to stock.验证过程步骤“SK”,这表明它已被转移到库存中。

  • Ultimately if those qualification are met I need to count the number of process steps in the matching process sequence from travdet and the number of completed process steps from detail.最终,如果满足这些条件,我需要计算来自 travdet 的匹配流程序列中的流程步骤数以及来自 detail 的已完成流程步骤的数量。

The query I'm using:我正在使用的查询:

select inproces."H-JOB#" as job, count(distinct travdet."GATE ID") as ttl , count(distinct detail."D-DEST") as comp
from inproces 
left join release on inproces."H-JOB#" = release."R-TRACKING-NBR"
left join travdet on release."R-JOB#" = travdet."JOB-#" and inproces."H-PROC-SEQ" = travdet."Process-Sequence" 
left join detail on detail."D-JOB#" = inproces."H-JOB#" and detail."D-DEST" <> ''
left join history on inproces."H-JOB#" = history."S-JOB#" and history."S-GATE" <> 'SK'
where release."R-RELEASE-STATUS" = 'Y' 
group by job
union
select inproces."H-JOB#" as job, count(distinct travdet."GATE ID")as ttl, count(distinct detail."D-DEST") as comp
from inproces 
left join pnlrel on inproces."H-JOB#" = pnlrel."P-TRACKING-NBR"
left join travdet on pnlrel."P-JOB-NBR" = travdet."JOB-#" and inproces."H-PROC-SEQ" = travdet."Process-Sequence"
left join detail on detail."D-JOB#" = inproces."H-JOB#" and detail."D-DEST" <> ''
left join history on inproces."H-JOB#" = history."S-JOB#" and history."S-GATE" <> 'SK'
where pnlrel."P-RELEASED-FLAG" = 'Y' 
group by job

The output (for data from sample tables):输出(针对样本表中的数据):

job             ttl    comp
ABC-0100-001    5      3 
DEF-0100-001    10     4   

Note that ABC-0100-002 and ABC-0100-001P1 are excluded because their GATE ID = "SK".请注意,ABC-0100-002 和 ABC-0100-001P1 被排除在外,因为它们的 GATE ID = "SK"。

I would really appreciate any advice on how to improve the performance of this query!我非常感谢有关如何提高此查询性能的任何建议!

One of the common problems with slow performing query is lack of index for optimization.执行缓慢的查询的常见问题之一是缺少优化索引。 The lazy solution is to make sure that every column used in the joins is covered by an index.懒惰的解决方案是确保连接中使用的每一列都被索引覆盖。 Alternatively, use the execution plan in the Advantage Data Architect to see potential cause of the problem and suggested solutions.或者,使用 Advantage Data Architect 中的执行计划来查看问题的潜在原因和建议的解决方案。

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

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