MySQL Index's

MySQL Database.  How many INDEX's should a table have?  What indexs should they be? Primary, Foreign?  What is the best setup?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
>>How many INDEX's should a table have?

As many as it needs.  Indexes are based on usage.  You need to look at how the applications use the data and created indexes to benefit the queries.

OVER INDEXING is almost just as bad as under indexing.

>>What indexs should they be? Primary, Foreign?

A table can only have one primary key.  Foreign keys are based on the tables relationships so as many as you need.

>>What is the best setup?

It is based on the applications
Chris StanyonWebDevCommented:
There's no right or wrong answer in how many they should have. As pointed out above, it will all depend on your table, the realtionship of that table to other tables and how you query your table.

As a general rule of thumb, add one Primary Key Index - this is used to uniquely identify the record. It can be something meaningful, such as a Customer ID, Order ID etc, or something more arbitrary, like an AutoIncrementing ID Field.

Then think about the fields in your table that would allow you to link to another table. These will be the Foreign Keys (generally relating to  a Primary Key from another table). This should be Indexed.

Now think about the fields that you commonly search on. It makes sense to add an index to these columns, as it helps performance.

You can also add a Unique Index to a column (or group of columns) to enforce some data integrity. For example, you may add a Unique Index to an Email column to prevent 2 users registering the same address.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.