Say I have an ordinary table in my db like so
----------------------------
| id | username | password |
----------------------------
| 24 | blah | blah |
----------------------------
A primary key is assigned to the id column. Now when I run a Mysql query like this:
SELECT id FROM table WHERE username = 'blah' LIMIT 1
Does that primary key index even help? If I am telling it to match usernames, then shouldn’t the username column be indexed instead?
Thanks for your time
In general, when you create an index, you do it based on the expectation of what will be queried. So if you know in advance that you will always, or often, query by username, then you should create an index on username. If you are using the id column as an identifier or foreign key from other tables, then create an index on the id column.
To answer your question directly — yes, index should be on username if you query by username, and if you never query by id.