如何在 mysql 中重用子查询?

发布于 2024-10-30 12:05:43 字数 3422 浏览 2 评论 0原文

我有以下查询,并且使用相同的子查询 4 ​​次,这影响了我的性能。有没有办法通过仅运行一次并在我需要的其他地方引用它来重用子查询?

下面是代码, 谢谢

SELECT adb_product_type.strproduct_type AS strproduct_type, adb_product_cat.strproduct_cat AS strproduct_cat, adb_product.strproduct AS strproduct, 

CASE 
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN 'No' ELSE 'Yes'
END AS 'YesNo', 

CASE 
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE 

(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
END AS 'CampaignCount', 

CASE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE 
 adb_media.name 
END AS mediaName

FROM adb_product_type 
LEFT JOIN adb_product_cat ON adb_product_cat.lngproduct_type = adb_product_type.lngproduct_type 
LEFT JOIN adb_product ON adb_product.lngproduct_cat = adb_product_cat.lngproduct_cat 
LEFT JOIN adb_camp_media ON adb_camp_media.lngproduct = adb_product.lngproduct 
LEFT JOIN adb_media ON adb_media.media_id = adb_camp_media.media_id 
LEFT JOIN adb_camp_media_prod ON adb_camp_media_prod.media_num = adb_camp_media.media_num 
LEFT JOIN adb_campaign ON adb_campaign.campaign_id = adb_camp_media.campaign_id 
WHERE 1=1 AND (adb_campaign.start_date >= '2010-01-01' AND adb_campaign.start_date <= '2010-01-31') 
OR (adb_campaign.end_date >= '2010-01-01' AND adb_campaign.end_date <= '2010-01-31')
OR ( adb_camp_media.campaign_id IS NULL) OR (adb_camp_media_prod.lngproduct IS NULL)

GROUP BY strproduct, mediaName ORDER BY strproduct_type, strproduct_cat, strproduct

I have the following query and I am using the same subquery 4 times which is impacting my performance. Is there any way to reuse the subquery by only running it once and referencing to it in the other places i need it?

below is the code,
thanks

SELECT adb_product_type.strproduct_type AS strproduct_type, adb_product_cat.strproduct_cat AS strproduct_cat, adb_product.strproduct AS strproduct, 

CASE 
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN 'No' ELSE 'Yes'
END AS 'YesNo', 

CASE 
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE 

(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
END AS 'CampaignCount', 

CASE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id) 
FROM adb_camp_media AS adb_camp_media_1 
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31') 
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE 
 adb_media.name 
END AS mediaName

FROM adb_product_type 
LEFT JOIN adb_product_cat ON adb_product_cat.lngproduct_type = adb_product_type.lngproduct_type 
LEFT JOIN adb_product ON adb_product.lngproduct_cat = adb_product_cat.lngproduct_cat 
LEFT JOIN adb_camp_media ON adb_camp_media.lngproduct = adb_product.lngproduct 
LEFT JOIN adb_media ON adb_media.media_id = adb_camp_media.media_id 
LEFT JOIN adb_camp_media_prod ON adb_camp_media_prod.media_num = adb_camp_media.media_num 
LEFT JOIN adb_campaign ON adb_campaign.campaign_id = adb_camp_media.campaign_id 
WHERE 1=1 AND (adb_campaign.start_date >= '2010-01-01' AND adb_campaign.start_date <= '2010-01-31') 
OR (adb_campaign.end_date >= '2010-01-01' AND adb_campaign.end_date <= '2010-01-31')
OR ( adb_camp_media.campaign_id IS NULL) OR (adb_camp_media_prod.lngproduct IS NULL)

GROUP BY strproduct, mediaName ORDER BY strproduct_type, strproduct_cat, strproduct

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-11-06 12:05:43

我发现它......在 mysql 中你可以使用用户变量,例如。

@campaignCount:=(SELECT 
                  COUNT(DISTINCT adb_campaign_1.campaign_id) 
                  FROM adb_camp_media AS adb_camp_media_1 
                   INNER JOIN adb_campaign AS adb_campaign_1 
                    ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
                   WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
                    AND ((adb_campaign_1.start_date >= '2010-01-01' 
                    AND adb_campaign_1.start_date <= '2010-01-31') 
                    OR (adb_campaign_1.end_date >= '2010-01-01' 
                    AND adb_campaign_1.end_date <= '2010-01-31')) )

I found it...in mysql you can use user variables eg.

@campaignCount:=(SELECT 
                  COUNT(DISTINCT adb_campaign_1.campaign_id) 
                  FROM adb_camp_media AS adb_camp_media_1 
                   INNER JOIN adb_campaign AS adb_campaign_1 
                    ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id 
                   WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct 
                    AND ((adb_campaign_1.start_date >= '2010-01-01' 
                    AND adb_campaign_1.start_date <= '2010-01-31') 
                    OR (adb_campaign_1.end_date >= '2010-01-01' 
                    AND adb_campaign_1.end_date <= '2010-01-31')) )
述情 2024-11-06 12:05:43

您可以提取子查询的结果并将其放入临时表中。然后更新您的查询以使用临时表的内容。

You could pull the results of the subquery and place it in a temporary table. Then update your query to use the contents of the temporary table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文