2

I'm for the first time making a bigger database using MySQL. Since it's going to be a bigger one I need better performance for the queries. So I looked into indexes. Since this is what I was learned I always make a column named id which is auto-incremented and is used as a primary index.

After some searching and reading I think I've understood some things about indexing but I want to be sure.

Indexes is useful when you use a select query with, for example a WHERE clause because instead of searching the whole table, it just searching in a smaller part. You should index things that are used in WHERE and JOIN clauses.

Is it good enough to just add a index in phpMyAdmin on the columns that you want to index or do I have to do something when doing a select query?

3 Answers 3

2

You don't need to make any changes to your SELECT statements. MySQL will automatically use the indexes for lookups when it can.

So yes, it's good enough to add the indexes via phpMyAdmin.

To see this in action, once you've created your indexes, try running a SELECT query prefixed with EXPLAIN. MySQL will tell you which indexes are being used.

0
1

Just add the index. The DB engine will use it if possible. To see if the DB engine uses it you can add an explain before your query and it will output what it does. Example:

explain select * from your_table
0
1

It is true that you can use the index in your where clause, but often you don't know what indexes to search for and instead search for more meaningful data such as names, email-addresses, dates etc.

The power of using indexes comes when you join tables, both for performance of the search and for the size of the database. Imagine if you have a DB with employees and you want to keep track of what office they belong to. With only one table each employee will have a char string containing the office name, ie "Albuquerque Headquarters". Char strings occupy a lot of space and are slow to search for.

Instead, designed with two tables and indexes the employee-table only need to store an int (the index of the other table) for each employee. And the second table keeps track of all the offices. The overall database gets much smaller when the number of data records increase. This approach is also much much easier to maintain and is better in each and every way.

Either way - you can never go wrong with an index and you might as well add one to each table you'll ever create (I am sure that someone might disagree with this statement, but normally I'd say you should just go for it).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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