I have two tables. In the table UnRevisedTable goes the raw data and in the table RevisedTable there is only entries of revised values which a human inserts when he finds a value in UnRevisedTable than he doesnt likes.
In UnRevisedTable there is one entry every minute and in RevisedTable there is one entry for every unwanted entry in UnRevisedTable.
The purpose of those two queries is the same: Show me the union of revised and unrevised data replacing the unrevised data when there is revised data.
I started writing the query2 but it was too slow. Then i wrote the query1 and the query1 is much, much faster.
My question is why query1 is faster than query2? Thx.
query1:
SELECT o.start_date_time,
CASE WHEN r.start_date_time IS NULL THEN o.value ELSE r.value END AS value,
FROM UnRevisedTable o LEFT JOIN RevisedTable r ON o.start_date_time = r.start_date_time
WHERE o.start_date_time >= '".$start."' AND o.start_date_time < '".$finish."' ORDER BY start_date_time ASC;
query2:
select * from(
select RevisedTable.* from RevisedTable where start_date_time between '".$start."' and '".$finish."'
union
select UnRevisedTable.* from UnRevisedTable where start_date_time between '".$start."' and '".$finish."'
) as t1 group by start_date_time;
As Abhik Chakraborty sugested here are the explain query1 and explain query2:
query1:
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+
| 1 | SIMPLE | o | range | PRIMARY | PRIMARY | 8 | NULL | 9 | Using where |
| 1 | SIMPLE | r | eq_ref | PRIMARY | PRIMARY | 8 | Mydb.o.start_date_time | 1 | |
+----+-------------+-------+--------+---------------+---------+---------+------------------------+------+-------------+
query2:
+----+--------------+------------+-------+---------------+---------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+------+------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 14 | Using temporary; Using filesort |
| 2 | DERIVED | RevisedTable | range | PRIMARY | PRIMARY | 8 | NULL | 2 | Using where |
| 3 | UNION | UnRevisedTable| range | PRIMARY | PRIMARY | 8 | NULL | 10 | Using where |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+-------+---------------+---------+---------+------+------+---------------------------------+
explain first query
and thenexplain 2nd query
and share the result in the question. - Abhik Chakraborty