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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:41:16+00:00 2026-06-12T11:41:16+00:00

Here is the query: SELECT * FROM table WHERE accountid = 1 ORDER BY

  • 0

Here is the query:

SELECT * FROM table WHERE accountid = 1 ORDER BY logindate DESC LIMIT 1

Now if I added an index with multiple columns on the fields:

INDEX(accountid,logindate)

Would MySQL take advantage of this multiple column index? Or would it not use it because one field is in the where clause and the other is in an order statement? Or does it not matter as long as I use the fields in the order of the multiple column index?

  • 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-12T11:41:17+00:00Added an answer on June 12, 2026 at 11:41 am

    Good question.

    Indexes work left to right, so your WHERE criteria would use the index. The sort would also utilize the index in this case (execution plan below).

    From the manual:

    The index can also be used even if the ORDER BY does not match the index exactly, as long as all of the unused portions of the index and all the extra ORDER BY columns are constants in the WHERE clause. The following queries use the index to resolve the ORDER BY part:

    SELECT * FROM t1
    WHERE key_part1=constant
    ORDER BY key_part2;
    

    If you had a single column index (accountid), a filesort would be used instead. Therefore, your query does benefit from that index.


    Two Column Index

    create table t1 (
      accountid tinyint,
      logindate date);
    
    create index idx on t1 (accountid, logindate);
    
    insert into t1 values (1, '2012-09-05'), (2, '2012-09-09'), (3, '2012-09-04'), 
        (1, '2012-09-01'), (1, '2012-09-26'), (2, '2012-05-16'), 
        (1, '2012-09-01'), (3, '2012-10-19'), (1, '2012-03-01')
    

    Execution Plan

    ID  SELECT_TYPE  TABLE  TYPE  POSSIBLE_KEYS  KEY  KEY_LEN  REF   ROWS  FILTERED  EXTRA
    1   SIMPLE       t1     ref   idx            idx  2        const 5     100       Using where; Using index

    Single Column Index

    create table t1 (
      accountid tinyint,
      logindate date);
    
    create index idx on t1 (accountid);
    
    insert into t1 values (1, '2012-09-05'), (2, '2012-09-09'), (3, '2012-09-04'), 
        (1, '2012-09-01'), (1, '2012-09-26'), (2, '2012-05-16'), (1, '2012-09-01'), 
        (3, '2012-10-19'), (1, '2012-03-01')
    

    Execution Plan

    ID  SELECT_TYPE  TABLE  TYPE   POSSIBLE_KEYS  KEY  KEY_LEN  REF   ROWS  FILTERED  EXTRA
    1   SIMPLE       t1     range  idx            idx  2              5     100       Using where; Using filesort
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my query Select Gender from Table The result pane shows 1 1
Here is my sample query: SELECT userid,count(*) FROM hits GROUP BY userid My table
Here is my query select t1.*, t2.name as customer, t2.name as vendor from order
Here is my query select * from table1 inner join table2 on table1.typeId=table2.typeId; This
Here's my query SELECT * FROM wp_postmeta WHERE meta_value LIKE '.$_POST['username'].' AND meta_value LIKE
here is the query SELECT * FROM customers WHERE NOT EXISTS ( SELECT 1
Here is my Query : select EmployeeName, EmployeeSalary from Employee2 for xml path('EmployeeDetails') returns
Here is the pseudo-code for my inline query in my code: select columnOne from
Here is the query that I am working on: SELECT `unitid`, `name` FROM apartmentunits
Here is my LINQ query: (from o in entities.MyTable orderby o.MyColumn select o.MyColumn).Distinct(); Here

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.