Filtering before joining or after joining SQL

I have two requests to get the same data, they more or less have the same runtime.

SELECT name AS prog_name,d.chap_name,d.vid_name,d.idvideo FROM program 
     JOIN (SELECT name AS chap_name,chapter.idprogram, c.vid_name,c.idvideo FROM chapter 
     JOIN (SELECT a.name AS vid_name, a.idvideo, b.idchapter FROM video a 
     JOIN (SELECT idvideo,idchapter,x.idchaptervideo FROM chaptervideo 
     JOIN (SELECT idchaptervideo FROM prescriptionvideo  WHERE idprescription=50)x 
     ON x.idchaptervideo=chaptervideo.idchaptervideo) b 
     ON b.idvideo=a.idvideo)c 
     ON c.idchapter=chapter.idchapter) d 
     ON d.idprogram=program.idprogram

SELECT program.name AS prog_name,chapter.name as chap_name,video.name as vid_name,video.idvideo FROM prescriptionvideo
JOIN chaptervideo ON prescriptionvideo.idchaptervideo=chaptervideo.idchaptervideo
JOIN video on chaptervideo.idvideo=video.idvideo
JOIN chapter on chaptervideo.idchapter=chapter.idchapter
JOIN program on chapter.idprogram=program.idprogram
where idprescription=50

Can anyone guide me which one is better. I used filtering before merging in the first and after joining the last. The MySQL explanation shows more row scans in the first than the last.

+3
source share
2 answers

Option 1 is better, since it is easy to read and write and has the value of checking the bottom line, otherwise the queries have the same set of results.

0
source

Explanation MySQL explains more row scans in the first compared to the last.

, ! . , where. SQL- , .

SELECT program.name AS prog_name,chapter.name as chap_name,video.name as vid_name,video.idvideo FROM prescriptionvideo 
JOIN chaptervideo ON prescriptionvideo.idchaptervideo=chaptervideo.idchaptervideo 
JOIN video on chaptervideo.idvideo=video.idvideo 
JOIN chapter on chaptervideo.idchapter=chapter.idchapter and chapter.idprescription=50
JOIN program on chapter.idprogram=program.idprogram 
+1

All Articles