简体   繁体   English

有没有更快的方法从表中选择记录并查询其子表

[英]Is there a faster way to select records from a table and query its sub tables

He everyone,他大家,

I would like to get all records from a table that are changed, but also if one of its child tables has changed.我想从已更改的表中获取所有记录,而且还想获取其中一个子表是否已更改。

I have a working query that does this using subqueries but it takes very long (2 minutes) to complete.我有一个使用子查询执行此操作的工作查询,但需要很长时间(2 分钟)才能完成。

Is there any other way to accomplish this that runs faster?有没有其他方法可以更快地完成此操作?

Thanks!谢谢! Wouter沃特

SELECT
    wp_posts.id AS order_id,
    br_order_archive.id AS archive_id,
    wp_posts.post_date_gmt AS date,
    wp_posts.post_modified_gmt AS lastmod
FROM
    wp_posts
LEFT JOIN 
    br_order_archive ON br_order_archive.order_id = wp_posts.id 
WHERE
    wp_posts.post_type = "shop_order"
AND
    wp_posts.post_status = "wc-completed"
AND (
    br_order_archive.order_id IS NULL 
    OR wp_posts.post_modified_gmt != br_order_archive.lastmod
    OR br_order_archive.lastmod_licenses != (
        select max(wp_woocommerce_software_licenses.created)
        from wp_woocommerce_software_licenses
        where wp_posts.id = wp_woocommerce_software_licenses.order_id
        )
    OR br_order_archive.lastmod_activations != (
        select max(wp_woocommerce_software_activations.activation_time)
        from wp_woocommerce_software_activations
        LEFT JOIN wp_woocommerce_software_licenses ON wp_woocommerce_software_activations.key_id = wp_woocommerce_software_licenses.key_id 
        where wp_posts.id = wp_woocommerce_software_licenses.order_id
        )
    )
ORDER BY 
    wp_posts.ID desc

In the absence of the necessary debugging information, let's start with this bit....在没有必要的调试信息的情况下,让我们从这一点开始......

select max(a.activation_time)
  from wp_woocommerce_software_activations a
  LEFT 
  JOIN wp_woocommerce_software_licenses l
    ON a.key_id = l.key_id
  where p.id = l.order_id
    

A LEFT JOIN on a table from which you select no columns is a strange thing, as is the correlation between the LEFT JOINed table and the posts table (p).未从中选择任何列的表上的 LEFT JOIN 是一件奇怪的事情,LEFT JOINed 表和帖子表 (p) 之间的相关性也是如此。 Normally, a LEFT JOINed table would not have an equals operator in a WHERE clause.通常,LEFT JOINed 表在 WHERE 子句中不会有等于运算符。 For my own sanity, I would start by rewriting this as an uncorrelated subquery, regardless of the impact on performance.为了我自己的理智,无论对性能的影响如何,我都会首先将其重写为不相关的子查询。

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

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