My first attempt at a question proved to be confusing and I received some mixed answers (probably due to my confusing question). Here's a different and better question...
Assume my table looks like this in MySQL:
CREATE TABLE `people` (
`person_id` INT(11),
`alias_num` TINYINT(3),
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`person_id`,`alias_num`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
With data like this:
person_id alias_num first_name last_name
--------- --------- ---------- ---------
1 1 John Smith
2 1 Joe Smith
3 1 Bill Smith # <-- Notice this guy has 3 aliases
3 2 Billy Smith # <--
3 3 William Smith # <--
4 1 Susan Thompson
...
Assume jo
and smi
were entered into an HTML search form (with both fields required) and my query will always be like this:
SELECT person_id FROM people WHERE first_name LIKE 'jo%' AND last_name LIKE 'smi%';
Question: What's the best index(es) to add to my table to make the above query the fastest?
Note:
I did some quick testing on a table of almost a million rows and it looks like that 2 seperate indexes of first_name(15)
and last_name(15)
seem faster than a composite index of last_name(15),first_name(15)
using SQL_NO_CACHE? But maybe I'm testing this wrong. I'm also considering that maybe a combination of both composite index and an index on a single name would be good (if that doesn't confuse the optimizer)?
Bonus Question:
Considering that I'm searching for partial words, not full words, would something like ElasticSearch do this query any better?