I have an issue that I'm confused about when using a full text search in MySQL. I'm running MySQL 5.1.67. Here is my simplified table:
CREATE TABLE `contact_info` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(25) DEFAULT NULL,
`last_name` varchar(35) DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `idx_ft_first_name_last_name` (`first_name`,`last_name`)
) ENGINE=MyISAM CHARSET=latin1;
I want to query for first name and last name in a table with about 5 million rows. First name and last name are in separate columns as shown in the above table. I want to get all the rows where the first and last name are "John Smith". I don't want all the people that have John as their first name or Smith as their last name. I know I could do something like the following but because users sometimes chose to store the full name in the first_name column or in the last_name column I'm trying to setup a full text search.
SELECT ci.first_name,ci.last_name FROM contact_info ci
WHERE ci.first_name LIKE 'John%' AND ci.last_name LIKE 'Smith%'
When I run the query forcing it to use the primary key index then it returns a few hundred records for John Smith, this is what I want but it is slow and takes about 5 seconds. Here is the query:
SELECT ci.first_name,ci.last_name FROM contact_info ci USE INDEX(PRIMARY)
WHERE MATCH(ci.first_name,ci.last_name) AGAINST ('"John Smith"' IN BOOLEAN MODE);
When I run the query with the index the optimizer prefers, it doesn't return anything.
SELECT ci.first_name,ci.last_name FROM contact_info ci USE INDEX(idx_ft_first_name_last_name)
WHERE MATCH(ci.first_name,ci.last_name) AGAINST ('"John Smith"' IN BOOLEAN MODE);
Why isn't it returning anything? Is it not possible to query for "John Smith" across two columns with a full text index like this?