开发者

How to reduce the need for these MySQL subqueries?

I need to improve the performance of a large MySQL view. 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. 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 开发者_如何学Pythondbo.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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜