![](/img/trans.png)
[英]Query to retrieve main records in one table and its all sub records from another table
[英]Is there a faster way to select records from a table and query its sub tables
他大家,
我想從已更改的表中獲取所有記錄,而且還想獲取其中一個子表是否已更改。
我有一個使用子查詢執行此操作的工作查詢,但需要很長時間(2 分鍾)才能完成。
有沒有其他方法可以更快地完成此操作?
謝謝! 沃特
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
在沒有必要的調試信息的情況下,讓我們從這一點開始......
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
未從中選擇任何列的表上的 LEFT JOIN 是一件奇怪的事情,LEFT JOINed 表和帖子表 (p) 之間的相關性也是如此。 通常,LEFT JOINed 表在 WHERE 子句中不會有等於運算符。 為了我自己的理智,無論對性能的影響如何,我都會首先將其重寫為不相關的子查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.