In general, what I noticed with queries, and SQL performance is DATA, which you connect to, for example, the ONE to ONE relationship is much faster than the ONE to MANY relationship.
I noticed the ONE to MANY relationship on tables 3000 , joining a table with 30,000 items can take up to 11-15 seconds using LIMIT . But the same request, processed with all ONE to ONE relationships, will take less than 1 second.
So my suggestion is to speed up your request. According to Left Outer Join (desc), βLEFT JOIN and LEFT OUTER JOIN are the same,β so it doesn't matter which one you use.
But ideally, you should use INNER , because in your question you indicated B.Date1 IS NOT NULL
Based on this parent columns in the join selection (desc) , you can use the parent column in SELECT in JOIN.
SELECT a.Id FROM A a INNER JOIN (SELECT b.Id AS 'Id', COUNT(1) as `TotalLinks` FROM B b WHERE ((b.Date1 IS NOT NULL) AND ((a.Date >= b.Date2) AND (a.Date < b.Date1)) GROUP BY b.Id) AS `ab` ON (a.Id = ab.Id) AND (a.Flag = 'Y') WHERE a.Flag = 'Y' AND b.totalLinks > 0 LIMIT 0, 500
Try also LIMIT data you need; this will reduce the filtering needed by SQL.
source share