Im trying to understand indexes better for when I use Mysql. One issue is Im still having a hard time to determine what type of index I should use such as individual indexes, multi column indexes, covering indexes etc.
One question I have is, is there a general rule to decide what type of indexes to use? When I design my database layout I dont know exactly what all queries will be used until the application is done being built. For one table I could query on one or multiple fields as well as query it for reporting. So if I query a table like so:
SELECT * FROM table1 WHERE field1 = this AND field2 = that GROUP BY field3 ORDER BY field4
Would I create a multiple column index on field1,field3,field3 and field4?
Also what if I have a different query on the same table like:
SELECT * FROM table1 WHERE field1 = this and field3 = that
If I had the multiple column index from the first query will that same index work for the second query since field1 is on the farthest left of the index?
And another question I had was is there a specific order mysql looks for indexes? So for multiple column or a covering index do I add indexes in order of the where clause? Then anything in group clause then anything in order clause? Or does mysql automatically do this?
Sorry for all the questions, just looking for help on this.
First you have to decide which Engine you want to use for a given table
(Full text index keeps an index based on words in a column)
You have to know that MySQL uses only one index per table maximum in a join. So, don’t expect MySQL to combine two indexes of a given table.
Chose the order of the column based on the queries, provided that MySQL can use the top of the index if necessary
For instance
MySQL can use (col1), (col1,col2) and (col1,col2,col3) as index. So to answer your question, your index should be created on
since your two queries needs (field1,field3) and (field1,field2,field3,field4).