skip to Main Content

Consider we have A,B,C,D,E,F,G,H columns in my table and if I make composite indexing on column ABCDE because these column are coming in where clause and then I want composite indexing on ABCEF then I create new composite indexing on ABCEF in same table but in a different query, we want indexing on column ABCGH for that i make another composite indexing in same table,

So my question is can we make too many composite indexes as per our requirements because sometimes our column in where clause gets change and we have to increase its performance so tell me is there any other way to optimise the query or we can make multiple composite indexes as per the requirements.

I tried multiple composite indexing and it did not give me any problem till now but still i want to know from all of you that will it give me any problem in future or is it ok to use only composite indexes and not single index.

Your answers will help me alot waiting for your replies.
Thanks in advance.

4

Answers


  1. You can have as many as you want. However, each additional index has a cost when updating, inserting or deleting. The trick is to

    1. find common segments and make indexes for those.
    2. Or create them as required when queries are too slow.

    As an example, if you are "needing" indexes for ABCDE, ABDEF, and ABGIH then create an index on just AB

    Login or Signup to reply.
  2. Each index requires space on the disk to be stored, and time to be updated every time you update(/insert/delete) an indexed column value.

    So as long as you don’t run out of storage or write operations are too slow, because you have to update too many indexes, you are not limited to create as many specific indexes as you want.

    This depends on your use case and should be measured with production like data.

    A common solution would be to create one index specific for your most important query e.g. in your case ABCDE.
    Other queries can still use the as many columns from left to right until there is a first difference. e.g. a query searching for ABCEF could still use ABC on the previous mentioned index.
    To also utilise column E you could add a where condition to D to your query in a way you know it matches all values e.g. D < 100 if you know there are only values 1-99.

    Login or Signup to reply.
  3. InnoDB supports up to 64 indexes per table (cf. https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.html).

    If you try to create a composite index for every permutation of N columns, you would need N-factorial indexes. So for 8 columns, you would need 40,320 indexes. Clearly this is more than InnoDB supports.

    You probably don’t need that many indexes. In practice, I’ve rarely seen more than 6 indexes in a given table. All queries that are needed are optimized by one of those.

    I understand you said sometimes you change the terms in your query’s WHERE clause, so it might need a composite index with different columns.

    You can rely on indexes that have a subset of all the columns that would be optimal. That won’t be 100% optimized, but it will be better than no index.

    You can’t predict the optimal set of indexes for a given query until you write that query.

    Login or Signup to reply.
    • There is a limit of 64 secondary indexes (at least in InnoDB).

    • Order the indexes so that columns being tested with = come first. (The order of those columns in the INDEX does not matter.)

    • The leftmost columns in an index are the most important.

    • There is little or now use in including more than one column that will be searched by a range.

    • Study your likely queries, and find the most common combinations of 2 or 3 columns; build indexes starting with those.

    • In your two examples (ABCDEFGH and ABCEF), ABC would work for both (assuming at least A and B are tested with =). If you do throw on more columns, that one INDEX can still be used for both cases.

    • Maybe you would what to declare both ABCDEFGH and BCEFA; This handles your ABCDEF case, plus cases that have B, but not A. (Remember ‘leftmost’.)

    • Use the SlowLog to find the slowest queries and make better indexes for them.

    • More on indexing: Index Cookbook

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search