Should I add INDEX
to my VARCHAR
column? I need to select rows with a string, not with ID, will it speed my queries?
Example:
SELECT * FROM `cities` WHERE `slug` = 'new-york';
indexMySQLunique-constraint
Should I add INDEX
to my VARCHAR
column? I need to select rows with a string, not with ID, will it speed my queries?
Example:
SELECT * FROM `cities` WHERE `slug` = 'new-york';
Best Answer
You plan to have queries of the type:
So, yes, you should definitely have an index on
(slug)
.Should it be
UNIQUE
index or aUNIQUE
constraint?That depends on whether you allow to have two rows in the table with same "slug". I think this is unlikely to be a requirement, as "slugs" are usually short terms to uniquely identify a thing (user, city, country, web page, etc).
So, yes, the index should be
UNIQUE
. MySQL makes no difference betweenUNIQUE INDEX
andUNIQUE CONSTRAINT
. I prefer to use theUNIQUE
constraint syntax (which adds aUNIQUE
index):