简体   繁体   English

如何替换WHERE子句中的WHERE IN选项以减少请求的执行时间?

[英]How can I replace WHERE IN choices in WHERE clause to reduce execution time of the request?

I am using DBF database in a C++ project written on RAD Studio 10.2 Tokyo. 我在RAD Studio 10.2 Tokyo上编写的C ++项目中使用DBF数据库。 To make request on this database, I am using UniDac Devart component (that is a little bit limited compared to MySQL itself). 为了在此数据库上发出请求,我使用了UniDac Devart组件(与MySQL本身相比有一点限制)。 The problem I have is when using WHERE IN condition in the request, the request makes too much time to be execute (more than one hour sometimes). 我遇到的问题是在请求中使用WHERE IN条件时,该请求花了太多时间执行(有时超过一小时)。

This is the request I have : 这是我的要求:

SELECT accountrp, SUM(amounteur) AS montant FROM %s 
WHERE doctype='1' AND period<>'00' AND
matchno IN(SELECT matchno FROM %s GROUP BY matchno HAVING SUM(amounteur)<>0)
GROUP BY accountrp

accountrp, doctype, period are character and amounteur is numeric. accountrp,doctype,period是字符,countereur是数字。

The problem is around the line matchno IN. 问题出在matchno IN的周围。 I would like to optimize the request without using IN. 我想在不使用IN的情况下优化请求。 I saw on Internet that WHERE IN condition can be replaced by INNER JOIN junctions but I Don't know if it is the solution and how to do this. 我在Internet上看到可以用INNER JOIN联结代替WHERE IN条件,但我不知道这是否是解决方案以及如何执行此操作。

Can you help me, please ? 你能帮我吗 ?

You can try with below 您可以尝试以下

SELECT s.accountrp, SUM(s.amounteur) AS montant FROM %s s
  join 
   (SELECT matchno FROM %s GROUP BY matchno HAVING SUM(amounteur)<>0) t
  on s.matchno =t.matchno
WHERE s.doctype='1' AND s.period<>'00'
GROUP BY s.accountrp

A first suggestion you could change the IN clause in a INNER JOIN 第一个建议是您可以在INNER JOIN中更改IN子句
this should be more efficent for performance 这应该对性能更有效

SELECT accountrp, 
    SUM(amounteur) AS montant 
FROM %s  as s 
INNER JOIN  ( 
  SELECT matchno 
  FROM %s 
  GROUP BY matchno 
  HAVING SUM(amounteur)<>0
) t on t.matchno = s.matchno
WHERE doctype='1' AND period<>'00' AND
GROUP BY accountrp

this because a In clause is equivalent to a set of OR condition and the query is performed each time for each OR .. a inner join instead is just a join between tow table and is performed just one time 这是因为In子句等效于一组OR条件,并且每次为每个OR都执行查询。内部联接只是两个表之间的联接,而仅执行一次

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

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