Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL, how to build index to speed up this query?

In MySQL, how to build index to speed up this query?

SELECT c1, c2 FROM t WHERE c3='foobar';
like image 734
qazwsx Avatar asked Oct 24 '12 14:10

qazwsx


People also ask

How does index make query faster?

Indexing makes columns faster to query by creating pointers to where data is stored within a database. Imagine you want to find a piece of information that is within a large database. To get this information out of the database the computer will look through every row until it finds it.

How do you create an index in SQL to improve performance?

In this case, you can create a large number of SQL Server indexes, adding all required columns as index key or non-key columns to enhance the performance of the SELECT queries and get the requested data faster. Another thing to consider when indexing a database table is the size of the table.

Does index decrease the speed of query?

Of course, Index can reduce the performance of any query and particularly of SELECT queries without even being that indexed utilized directly in the query. There is a huge misconception that Indexes only reduces the performance of INSERT, UPDATE and DELETE statement.


2 Answers

To really give a answer it would be useful to see if you have existing indexes already, but...

All this is assuming table 't' exists and you need to add an index and you only currently have a single index on your primary key or no indexes at all.

A covering index for the query will give best performance for your needs, but with any index you sacrifice some insertion speed. How much that sacrifice matters depends on your application's profile. If you read mostly from the table it won't matter much. If you only have a few indexes, even a moderate write load won't matter. Limited storage space for your tables may also come into play... You need to make the final evaluation of the tradeoff and if it is noticable. The good thing is it's fairly a constant hit. Typically, adding an index doesn't slow your inserts exponentially, just linearly.

Regardless, here are your options for best select performance:

  1. If c3 is your primary key for table t, you can't do anything better in the query to make it faster with an index.
  2. Assuming c1 is your primary key t:

    ALTER TABLE t ADD INDEX covering_index (c3,c2);  
    
  3. If c1 is not your pk (and neither is c2), use this:

    ALTER TABLE t ADD INDEX covering_index (c3,c2,c1);  
    
  4. If c2 is your PK use this:

    ALTER TABLE t ADD INDEX covering_index (c3,c1);  
    
  5. If space on disk or insert speed is an issue, you may choose to do a point index. You'll sacrifice some performance, but if you're insert heavy it might the right option:

    ALTER TABLE t ADD INDEX a_point_index (c3);  
    
like image 130
Ray Avatar answered Oct 12 '22 23:10

Ray


Build indexes on columns that you search for, so in this case, you'll have to add an index on field c3:

CREATE INDEX c3_index ON t(c3)
like image 32
Bart Friederichs Avatar answered Oct 13 '22 01:10

Bart Friederichs