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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:42:41+00:00 2026-05-27T03:42:41+00:00

In the following query SELECT col1,col2 FROM table1 WHERE col3=’value1′ AND col4=’value2′ If I

  • 0

In the following query

SELECT  col1,col2
FROM    table1
WHERE   col3='value1'
  AND   col4='value2'

If I have 2 separate indexes one on col3 and the other on col4, Which one of them will be used in this query ?

I read somewhere that for each table in the query only one index is used. Does that mean that there is no way for the query to use both indexes ?

Secondly, If I created a composite index using both col3 and col4 together but used only col3 in the WHERE clause will that be worse for the performance?
example:

SELECT  col1,col2
FROM    table1
WHERE   col3='value1'

Lastly, Is it better to just use Covering indexes in all cases ? and does it differ between MYISAM and innodb storage engines ?

  • 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-27T03:42:41+00:00Added an answer on May 27, 2026 at 3:42 am

    A covering index is not the same as a composite index.

    If I have 2 separate indexes one on col3 and the other on col4, Which one of them will be used in this query ?

    The index with the highest cardinality.
    MySQL keeps statistics on which index has what properties.
    The index that has the most discriminating power (as evident in MySQL’s statistics) will be used.

    I read somewhere that for each table in the query only one index is used. Does that mean that there is no way for the query to used both indexes ?

    You can use a subselect.
    Or even better use a compound index that includes both col3 and col4.

    Secondly, If I created a composite index using both col3 and col4 together but used only col3 in the WHERE clause will that be worse for the performance? example:

    Compound index
    The correct term is compound index, not composite.
    Only the left-most part of the compound index will be used.
    So if the index is defined as

    index myindex (col3, col4)  <<-- will work with your example.
    index myindex (col4, col3)  <<-- will not work. 
    

    See: http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html

    Note that if you select a left-most field, you can get away with not using that part of the index in your where clause.
    Imagine we have a compound index

    Myindex(col1,col2)
    
    SELECT col1 FROM table1 WHERE col2 = 200  <<-- will use index, but not efficiently
    SELECT * FROM table1 where col2 = 200     <<-- will NOT use index.  
    

    The reason this works is that the first query uses the covering index and does a scan on that.
    The second query needs to access the table and for that reason scanning though the index does not make sense.
    This only works in InnoDB.

    What’s a covering index
    A covering index refers to the case when all fields selected in a query are covered by an index, in that case InnoDB (not MyISAM) will never read the data in the table, but only use the data in the index, significantly speeding up the select.
    Note that in InnoDB the primary key is included in all secondary indexes, so in a way all secondary indexes are compound indexes.
    This means that if you run the following query on InnoDB:

    SELECT indexed_field FROM table1 WHERE pk = something
    

    MySQL will always use a covering index and will not access the actual table. Although it could use a covering index, it will prefer the PRIMARY KEY because it only needs to hit a single row.

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

Sidebar

Related Questions

I have a query like queryStr := 'SELECT col1, col2, col3, col4 FROM Table1
I currently have the following query SELECT table1.col1, table2.col2 FROM table1 INNER JOIN table2
I have the following query: select col1, sum( col2 ), count( col3 ) from
I have the following query (MySQL): SELECT col1, col2 FROM database1.table ->WHERE col3 !=
I am trying to accomplish the following: SELECT col1, col2 FROM table1 UNION SELECT
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have the following tables : Table1 ( Col1 : varchar2, Col2 : number,
The following MySQL query completes in under 0.02 seconds : SELECT * FROM `Table1`

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.