Search code examples
mysqlindexingphpmyadmindatabase-performancedatabase-optimization

What is the difference between MySQL grouped indexes and single indexes


In PHP Admin when checking my indexes, I found some grouped indexes and some single indexes.
1) Are my indexes for e.g. post_status indexed twice?
2) If so, which one is best to delete? The grouped or single one?

See screenshot: enter image description here


Solution

  • From Multiple-column index

    MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on. If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

    So your type_status_date index will be used if your filter criteria include all columns, one leftmost column, two leftmost column, and so on, such as

    SELECT * FROM your_table WHERE 
       post_type=a_constant_value1 AND
       post_status=a_constant_value2 AND
       post_date=a_constant_value3 AND
       post_author=a_constant_value4 AND
       id=a_constant_value5;  
    
    SELECT * FROM your_table WHERE 
       post_type=a_constant_value1 AND
       post_status=a_constant_value2 AND
       post_date=a_constant_value3 AND
       post_author=a_constant_value4;
    
    SELECT * FROM your_table WHERE 
       post_type=a_constant_value1 AND
       post_status=a_constant_value2 AND
       post_date=a_constant_value3;
    
    SELECT * FROM your_table WHERE 
       post_type=a_constant_value1 AND
       post_status=a_constant_value2;
    
    SELECT * FROM your_table WHERE 
       post_type=a_constant_value1;
    

    So post_type index for post_type column is redundant index so it can be deleted. id column in type_status_date index also redundant because id column is primary key. You can delete 'id' column in type_status_date index.

    If you never use all columns in any queries, remove any unused columns.

    But following query will not be able to use type_status_date index because post_status is not leftmost column in type_status_date index. For this query post_status index will be used.

    SELECT * FROM your_table WHERE 
       post_status=a_constant_value1;