I want to compare my algorithm, here is some simplification from my case, Assume that the table is indexed using the log_timestamp column.
First query:
SELECT
name
FROM
user_table
WHERE
DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) >= DATE('2018-01-01')
AND
DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) < DATE('2019-01-01');
Second query:
SELECT
name
FROM
user_table
WHERE
log_timestamp >= DATETIME_SUB('2018-01-01', INTERVAL 7 HOUR)
AND
log_timestamp < DATETIME_SUB('2019-01-01', INTERVAL 7 HOUR);
Which of the two queries above would be faster and why?
The question you asked should really be two separate questions. The first, along the lines of what you asked above, is which of the two queries is faster right now. The second question, which is really the one to consider, is how can you tune both queries to make them faster, and which one would be the fastest.
As it turns out, only the second query can use an index:
SELECT name
FROM user_table
WHERE log_timestamp >= DATETIME_SUB('2018-01-01', INTERVAL 7 HOUR) AND
log_timestamp < DATETIME_SUB('2019-01-01', INTERVAL 7 HOUR);
This query should benefit from an index on (log_timestamp, name)
. Note that your first query cannot really benefit from any index, so I expect your second query to be much faster, after the right index has been created.
To find out how a query works fast or slow, we must know how many records are in the table.
if the number of records is still between 100 - 1000 (depending on the number of fields in the table), when both queries are executed, both will display results in almost the same time.
if the number of records has exceeded 100,000 , it will start to see the time difference in displaying the results.
Remember, don't forget to use the EXPLAIN function to see how the query goes.
let's analyze the two queries
First Query
SELECT
name
FROM
user_table
WHERE
DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) >= DATE('2018-01-01')
AND
DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) < DATE('2019-01-01');
MySQL will :
Second Query
SELECT
name
FROM
user_table
WHERE
log_timestamp >= DATETIME_SUB('2018-01-01', INTERVAL 7 HOUR)
AND
log_timestamp < DATETIME_SUB('2019-01-01', INTERVAL 7 HOUR);
MySQL will :
Notes :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.