如何在 mysql 中重用子查询?
我有以下查询,并且使用相同的子查询 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现它......在 mysql 中你可以使用用户变量,例如。
I found it...in mysql you can use user variables eg.
您可以提取子查询的结果并将其放入临时表中。然后更新您的查询以使用临时表的内容。
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.