MySQL: index on non-primary key column
is it possible to use a column other than the primary key 开发者_StackOverflow中文版as the index and are there any disadvantages to doing that?
Absolutely. This helps with SELECT ... WHERE
where the condition isn't your primary key. However, be careful, don't index too much - only index what you need.
Many of my tables have secondary indexes besides the primary key. You can create multiple indexes per table, but in MySQL only one index is used per table in a given query.
Which indexes should you create? This depends on your queries. See the presentation I did this week, MENTOR Your Indexes.
Adding more indexes adds incrementally to overhead on insert/update/delete. When you change data, the RDBMS must keep indexes in sync. But it's nearly always true that the right indexes give your select queries so much benefit that they more than justify the overhead of maintaining them.
is there much of a performance hit with using multiple indexes?
In the book "The Art of SQL" the author tested this and found that there's a greater performance hit as you add multiple indexes. But interestingly, it doesn't go up linearly. The additional overhead of the second, third, etc. indexes falls off, in a roughly logarithmic curve. That is, two indexes is not twice as expensive as one index. Four indexes is not twice as expensive as two indexes.
You can go for alternative how ever there are
Advantages of MySQL Indexes Generally speaking, MySQL indexing into database gives you three advantages:
- Query optimization: Indexes make search queries much faster.
- Uniqueness: Indexes like primary key index and unique index help to avoid duplicate row data.
- Text searching: Full-text indexes in MySQL version 3.23.23, users have the opportunity to optimize searching against even large amounts of text located in any field indexed as such.
Disadvantages of MySQL indexes When an index is created on the column(s), MySQL also creates a separate file that is sorted, and contains only the field(s) you're interested in sorting on.
Firstly, the indexes take up disk space. Usually the space usage isn’t significant, but because of creating index on every column in every possible combination, the index file would grow much more quickly than the data file. In the case when a table is of large table size, the index file could reach the operating system’s maximum file size.
Secondly, the indexes slow down the speed of writing queries, such as INSERT, UPDATE and DELETE. Because MySQL has to internally maintain the “pointers” to the inserted rows in the actual data file, so there is a performance price to pay in case of above said writing queries because every time a record is changed, the indexes must be updated. However, you may be able to write your queries in such a way that do not cause the very noticeable performance degradation.
精彩评论