Wrong solution: querying with many joins at once should always be faster than using fragmented queries in multiple SQL statements, provided that your query is correct.
If your query is terribly slow as soon as you add n:n or 1:n joins, in Doctrine, there are several causes for it.
One of the most frequent mistakes in queries with multiple joins, is the use of the LEFT JOIN + WHERE construction where you could use INNER JOIN with ON. Consider this DQL example:
SELECT a.*, c.*
FROM Article a
LEFT JOIN a.Buyer b
LEFT JOIN b.Creditcard c
LEFT JOIN c.digitalinfo d
WHERE b.id = 2 AND d.info LIKE 'TEST%'
This is a very slow query if all tables have 10000 records. It will first join the entire table b with table a, resulting in 10000 ^ 2 rows, whereas in your WHERE clause you throw away pretty much 99,9% of them all.
SELECT a.*, c.*
FROM Article a
INNER JOIN a.Buyer b WITH b.id=2
LEFT JOIN b.Creditcard c
INNER JOIN c.Digitalinfo d WITH d.info LIKE 'TEST%'
Here, the INNER JOIN a.Buyer b does not end up with 100 000 000 rows, but since it uses an extended ON clause (Doctrine calles this WITH), it will only leave a small set. Therefore the other two joins will go lightning fast compared to what they performed like in the other statement.
Also, make sure you ALWAYS have indices
- On columns that you search on. (If you search on full name, as in FirstName+' '+LastName, create an index on that sequence!)
- On columns that you do specific joins on
- On foreign keys and the fields they have the reference set to.
If you want to know what your database is doing behind the scenes, you could for example in MySQL type EXPLAIN followed by your query, and it tells you exactly why it is taking so long.