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 947297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:03:34+00:00 2026-05-15T23:03:34+00:00

In mysql, does the order of the WHERE clauses affect the time that it

  • 0

In mysql, does the order of the WHERE clauses affect the time that it takes the server to process the query?

I know that the queries are compiled and optimized, but I don’t know if that optimization changes the order of the WHERE clauses so in a hypothetic case it will take less time to process a query that first selects the results with an integer than if it starts with a string index for example.

WHERE a = 2 AND b = 'Wiliam'

- 1: Search in 200 items and find 20 results with value a = 2
- 2: Search in 20 items and find 1 result with b = Wiliam

That

WHERE b = 'Wiliam' AND a = 2

- 1: Search in 200 items and find 20 results with value b = Wiliam
- 2: Search in 20 items and find 1 result with a = 2

Will it be faster if we start searching a integer indexed column? Does mysql know which clause has a worse index to do the second search?

Thanks.

  • 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-05-15T23:03:35+00:00Added an answer on May 15, 2026 at 11:03 pm

    The optimizer can and often will change the order of the evaluation of predicates to better take advantage of the indexes. MySQL keeps statistics on each index and it will use those to choose which index is likely to be fastest. There are no easy rules about which index will be chosen: it is free to choose different indexes depending on the data in the database and the values you are looking for.

    In the case where there are no indexes it can make a difference, but it is rarely worth worrying about:

    SELECT * FROM table1
    WHERE a = 2 AND b = 'Wiliam';
    
    2.9 seconds
    
    SELECT * FROM table1
    WHERE b = 'Wiliam' AND a = 2;
    
    2.6 seconds
    

    Test data:

    CREATE TABLE filler (
            id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
    ) ENGINE=Memory;
    
    DELIMITER $$
    
    CREATE PROCEDURE prc_filler(cnt INT)
    BEGIN
            DECLARE _cnt INT;
            SET _cnt = 1;
            WHILE _cnt <= cnt DO
                    INSERT
                    INTO    filler
                    SELECT  _cnt;
                    SET _cnt = _cnt + 1;
            END WHILE;
    END
    $$
    
    DELIMITER ;
    
    CALL prc_filler(1000000);
    
    INSERT INTO table1 (a, b)
    SELECT id, 'Wiliam' FROM filler;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the order of MySQL query clauses effect efficiency? e.g. SELECT * FROM TABLE
I have a query that does an ORDER BY on a VARCHAR column that
I'm currently working on a PHP/MySQL script that does the following, in this order:
Does anyone know of a company that allows us to deploy PHP/MySQL applications on
I want to perform :group and :order on my module, since Mysql does group
I would like to use ORDER BY FIELD() (MySQL) in Django. Does Django support
As some of you may know, use of the LIMIT keyword in MySQL does
Assume that the following query is issued to a MySQL database: SELECT * FROM
I am trying to put together a mysql query, that links 3 tables together.
I have a MySQL SELECT query that calculates a distance using Pythagoras in the

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.