简体   繁体   English

如何减少对这些MySQL子查询的需求?

[英]How to reduce the need for these MySQL subqueries?

I need to improve the performance of a large MySQL view. 我需要提高大型MySQL视图的性能。 Most of it is pretty straightforward and unlikely to be causing much performance trouble, but there is a long section filled with dozens of subqueries which are asking the same question with one value incrementing. 其中大多数非常简单明了,不太可能引起性能问题,但是有很长一段充满了数十个子查询,这些子查询以一个值递增的方式询问相同的问题。 I figure there must be a better way to do this, but I am no SQL guru. 我认为一定有更好的方法可以做到这一点,但我不是SQL专家。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

(I've generalized the code a bit to make the problem more clear) (我对代码进行了概括,以使问题更清楚)

SELECT
p.something,
p.otherthing,
c.athing,
d.nothing,
(select startdate from dbo.aqcprojects where projectid = p.id and aqcphase = 1) as 'Phase 1 start',
(select finishdate from dbo.aqcprojects where projectid = p.id and aqcphase = 1) as 'Phase 1 end',
(select startdate from dbo.aqcprojects where projectid = p.id and aqcphase = 2) as 'Phase 2 start',
(select finishdate from dbo.aqcprojects where projectid = p.id and aqcphase = 2) as 'Phase 2 end',
 **the above four lines repeated ad nauseum.
FROM
bunch of joins

you can add it to bunch of joins 您可以将其添加到一堆联接中

SELECT
p.something,
p.otherthing,
c.athing,
d.nothing,
project_phase_1.startdate as 'Phase 1 start',
project_phase_1.finishdate as 'Phase 1 end',
project_phase_2.startdate as 'Phase 2 start',
project_phase_2.finishdate as 'Phase 2 end',
 **the above four lines repeated ad nauseum.
FROM
projects AS p
LEFT JOIN dbo.aqcprojects AS project_phase_1 ON project_phase_1.projectid = p.id
LEFT JOIN dbo.aqcprojects AS project_phase_2 ON project_phase_2.projectid = p.id
bunch of joins
WHERE project_phase_1.aqcphase = 1 AND project_phase_2.aqcphase = 2

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

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