Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:22:23+00:00 2026-06-11T12:22:23+00:00

I am reading tick value of the record with largest id . What is

  • 0

I am reading tick value of the record with largest id. What is the difference between following queries that causes to slow execution?

Slow Query:

SELECT tick
FROM   eventlog
WHERE  id IN (SELECT max(id) FROM eventlog)

Quick Query:

SELECT max(id) INTO @id
FROM   eventlog;

SELECT tick
FROM   eventlog
WHERE  id = @id;

Schema

CREATE TABLE eventlog (
    id INT (11) NOT NULL AUTO_INCREMENT,
    tick INT NOT NULL,
    eventType_id INT NOT NULL,
    compType INT (10) UNSIGNED NOT NULL,
    compID INT (10) UNSIGNED NOT NULL,
    value_double DOUBLE NOT NULL,
    value_int INT (10),
    hierarchy_id VARCHAR (255) NOT NULL,
    PRIMARY KEY (id),
    INDEX htet (
        hierarchy_id,
        tick,
        eventType_id
    )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:22:24+00:00Added an answer on June 11, 2026 at 12:22 pm

    Because the in query doesn’t use index, the mysql will scan all the records to find the row.

    From http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

    B-Tree Index Characteristics

    A B-tree index can be used for column comparisons in expressions that
    use the =, >, >=, <, <=, or BETWEEN operators.

    There’s no IN

    And

    Hash Index Characteristics

    Hash indexes have somewhat different characteristics from those just discussed:

    They are used only for equality comparisons that use the = or <=>
    operators (but are very fast). They are not used for comparison
    operators such as < that find a range of values.

    there’s no IN either.

    As @tombom mentioned, foo IN ('bar', 'bla') is short for foo = 'bar' OR foo = 'bla', however, I believe they’re different. So I make a test on a table with enough data records, and find out the following:

    mysql> show columns from t_key;   
    +-------+---------+------+-----+---------+----------------+
    | Field | Type    | Null | Key | Default | Extra          |
    +-------+---------+------+-----+---------+----------------+
    | a     | int(11) | NO   | PRI | NULL    | auto_increment |
    | b     | int(11) | YES  | MUL | NULL    |                |
    +-------+---------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)
    
    mysql> select count(a) from t_key;
    +----------+
    | count(a) |
    +----------+
    |   989901 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> explain select a from t_key where a in (select max(a) from t_key);  
    +----+--------------------+-------+-------+---------------+---------+---------+------+--------+------------------------------+
    | id | select_type        | table | type  | possible_keys | key     | key_len | ref  | rows   | Extra                        |
    +----+--------------------+-------+-------+---------------+---------+---------+------+--------+------------------------------+
    |  1 | PRIMARY            | t_key | index | NULL          | PRIMARY | 4       | NULL | 989901 | Using where; Using index     |
    |  2 | DEPENDENT SUBQUERY | NULL  | NULL  | NULL          | NULL    | NULL    | NULL |   NULL | Select tables optimized away |
    +----+--------------------+-------+-------+---------------+---------+---------+------+--------+------------------------------+
    2 rows in set (0.00 sec)
    
    mysql> explain select a from t_key where a =(select max(a) from t_key);
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+------------------------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra                        |
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+------------------------------+
    |  1 | PRIMARY     | t_key | const | PRIMARY       | PRIMARY | 4       | const |    1 | Using index                  |
    |  2 | SUBQUERY    | NULL  | NULL  | NULL          | NULL    | NULL    | NULL  | NULL | Select tables optimized away |
    +----+-------------+-------+-------+---------------+---------+---------+-------+------+------------------------------+
    2 rows in set (0.00 sec)
    

    Then I try the IN query with static sequences, it works as @tombom mentioned:

    mysql> explain select a from t_key where a in (100,200);
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                    |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    |  1 | SIMPLE      | t_key | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where; Using index |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    1 row in set (0.00 sec)
    
    mysql> explain select a from t_key where a=100 or a=200;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                    |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    |  1 | SIMPLE      | t_key | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where; Using index |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
    1 row in set (0.00 sec)
    

    I don’t know whether the mysql would convert the IN query into ORs one when possible(for instance, the sequences is known before query), and I didn’t find related documents, but the explain shows that it did scan the table in this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading a book about C# I noticed that sometimes is mentioned value type and
Reading the Doctrine2 documentation for the Query Builder I found the following expressions: //
Reading across difference lineage of CPU created by intel , many questions aroused in
Reading the documentation at http://jersey.java.net/nonav/documentation/latest/client-api.html#d4e704 makes me think that ClientFilter s are processed in
I have a function that has event handler. It is reading gps position and
Reading the code of many javascript libraries, I see that many developers use to
while reading about declare construct from php manual i tried the following example using
Reading an article called Increase LINQ Query Performance in July's MSDN magazine, the author
Reading Apple documentation i found that UIEventTypeMotion is useful only to intercept Shake. It
Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.