Lets say i have this sample table:
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(512) NULL ,
`lastname` VARCHAR(512) NULL ,
`country` VARCHAR(256) NULL ,
`tags` VARCHAR(512) NULL ,
PRIMARY KEY (`id`) );
Basically the “tags” column is just key words that are set for the user
Now what i want to do is be able to search for a user based on the name and tags
To have fast search, i would had to create an index on “name” and “tags”
So to create index should i do:
ALTER TABLE `vidhucr1_serverlist`.`new_table`
ADD INDEX `INDEX` (`name` ASC, `tags` ASC) ;
or
ALTER TABLE `vidhucr1_serverlist`.`new_table`
ADD INDEX `INDEX1` (`name` ASC)
, ADD INDEX `INDEX2` (`tags` ASC) ;
Having a single index (
name, tags) should be better as long as your query filtersnamesandtags. If your query should omittags, the index would be useless (last statement under example 1). Also, I hope that you do not have multiple “tags” in thetagsfield.Example 1 – Composite Index
Indexes:
INDEX (name, tags)Index would be used for
SELECT ... WHERE name = '...' AND tags = '...'orSELECT ... WHERE tags = '...' AND name = '...'Index would be used for
SELECT ... WHERE name = '...'No index used for
SELECT ... WHERE tags = '...'Example 2 – Two Single Indexes
Indexes:
INDEX1 (name),INDEX2 (tags)Both indexes used for
SELECT ... WHERE name = '...' AND tags = '...'INDEX1used forSELECT ... WHERE tags = '...'INDEX2used forSELECT ... WHERE name = '...'