简体   繁体   中英

How to calculate query time to decide which one is faster or more efficient?

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 :

  • do this DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) through all records in table without using index,
  • after that MySQL will compare with >= DATE('2018-01-01')
  • do this DATE(DATETIME_ADD(log_timestamp , INTERVAL 7 HOUR)) through all records in table without using index,
  • after that MySQL will compare with < DATE('2019-01-01');
  • and display the results Notes : imagine you have 100,000 records in the table, it will takes time to display the results

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 :

  • compare log_timestamp >= DATETIME_SUB('2018-01-01', INTERVAL 7 HOUR) through indexes, not full scan table
  • and compare log_timestamp < DATETIME_SUB('2019-01-01', INTERVAL 7 HOUR); through indexes, not full scan table
  • and display the results

Notes :

  • Remember!!! ... Index in tables, it's just like index in a book. when you wanna read a book that have more than 1000 page, you will see the index first to find the page you looking for. You will not read all the pages, to find the topic you wanna read.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM