I'm using a UNION query to extract data from two tables based on date. Query below
SELECT title, id, date as date_added
FROM test1 WHERE test1.id
UNION
SELECT title, customer as id, date_added
FROM test2 WHERE test2.id
ORDER BY date_added DESC LIMIT 0,8
I have an index on date and date_added on each table...the problem is that the query isn't optimized and when I use EXPLAIN it shows all the rows of both the tables are being selected to give me the output.
Is there any other way of doing this query so that it can be optimized? The only solution i can think of is running the two queries separately with LIMITS and sorting the data in the application, but it seems it would be impossible to perform pagination in my application with that.