views:

55

answers:

3

Sorry all,

I do have this mini table with 3 columns but, I will list the values either by one or other column.

1) Is it ok to have two of those columns with indexes?

2) Should we have only one index per table?

3) At the limit, if we have a table with 100 columns for example, and we have 50 of them with indexes, is this ok?

Thanks, MEM

+3  A: 

It's fine to have many indexes, even multiple indexes on one table, as long as you are using them.

Run EXPLAIN on your queries and check which indexes are being used. If there are indexes that are not being used by any queries then they aren't giving you any benefit and are just slowing down modifications to the table. These unused indexes should be removed.

You may also want to consider multiple-column indexes if you have not already done so.

Mark Byers
Thanks a lot for the clarification. :)
MEM
I see... I'm trying to better understand the rationale behind when should we have two indexes or use one multiple index. for the name index that grabs both firstname and lastname, it makes sense... but I must see more examples about it. Thanks for this two new things on such a simple question. ;) EXPLAIN and multiple-column indexes. :D
MEM
This is good advice, many people just index all the fields and don't even consider using multiple indexes. Mark would you happen to know how much it slows down the table when updating if you have lots of indexes?
Gary Green
+1  A: 

There is no problem with having more than one index per table, and no, one index per table isn't really a guideline.

Having too many indexes is inefficient because

  • It requires additional storage
  • MySQL needs to determine which index to use for a particular query

Edit : As per Pablo and Mark, you need to understand how your data is being accessed in order for you to build effective indices. You can then refine and reduce the indexes.

nonnb
Thanks. So performance and space. Seems to be the issue. All answers seems to point into that direction. :) Thanks a lot.
MEM
+1  A: 

To quickly answer your questions, I think:

  1. Yes, it's OK to have two or even more columns with indexes
  2. Not necessarily. You can have more than one index per table.
  3. It might be OK, it might be not OK. It depends. The thing with indexes is that they take space (in DISK) and they make operations that modify your data (INSERT/UPDATE/DELETE) slower since for each and everyone of them, there will be a TABLE and INDEX update involved.

Your index creation should be driven by your queries. See what queries are you going to have on your system and create indexes accordingly.

Pablo Santa Cruz
Clear. So and, like Mark Byers suggested, the EXPLAIN (that I didn't know btw), could be used for such determination. Thanks for your time answering. :)
MEM
Yes! Explain is your **FRIEND**! In MySQL and almost every other relational database server out there...
Pablo Santa Cruz