Mysql – index all columns of a table in MySQL safely

indexmyisamMySQLperformance

I have one table with 4 columns. One is primary key and other three columns are references to columns of other tables. I am using join on these four tables for a search filter. These joins are taking time.

So I was thinking to have indexes on columns because I read that I should create indexes on columns of join conditions. My question is will there be problem if all columns of the table are indexed? Is there any other way to decrease time complexity of these search filters.

More Hint:

  • Table1(main search)-1000 million entries primary_key fk1 fk2 fk3
  • Table2-800 million entries pk1 ..(8-9 columns)
  • Table3-700 million entries pk2 ..(10-12 columns)
  • Table2-850 million entries pk3 ..(7-8 columns)

I am using MyISAM engine.

Best Answer

When it comes to querying, indexing of a table should never be your first concern.

The queries you plan to use should dictate the indexes you need.

Based on the queries, some columns can be individually indexed. Other queries require compound indexes. The ORDER BY and GROUP BY clauses should provide immediate hints for indexes to make. Not using such hints may result in temp table sorting rather than using the indexes for data in the desired order needed.

Low cardinality of column values should eliminate the need for an index.

Even with these things taken into consideration, you may find that query may need some adjustment (a.k.a. refactoring) for performance gains.

When you reach the point of having the right indexes, not you have to worry about the size of those indexes. For a MyISAM table, this would mean that the .MYI file may grow significantly.

The size of the index file as well as the number of indexes should now be weighed against the performance of your queries, especially if the indexes provide the proper ordering of data and fastest retrieval.

Explain plans for queries may change over time depending on the number of rows, cardinality of columns, number of DELETEs and UPDATEs. Once a query's explain plan changes from what it looked like months ago, you should explore the need to add or remove indexes.