4

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.

2

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.

2

Indexing all columns (index on each column) may or may not improve overall performance. That depends index selectivity. Consider creating covering index for your typical query. For instance, if you usually join all 3 tables, the query will mostly benefit on composite index (f1,f2,f3) . Also, if combination of these 3 fields is unique, you can define primary key on f1,f2,f3, so you don't need surrogate PK (assuming there are no other fields on this table)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.