I have the below query… It works but it runs extremely slow. Was hoping someone might be able to give me a few tips to improve execution time?
SELECT tb_clients.*, tb_clients_phone_fax.*
FROM tb_clients, tb_clients_phone_fax
WHERE tb_clients.client_id=tb_clients_phone_fax.client_id
AND MATCH (client_company,client_description,client_keywords) AGAINST ('test') > 0
AND CONCAT(client_address,' ',client_city,', ',client_state,' ',client_zip) LIKE '%brooklyn%'
LIMIT 10;
EDIT:
Here is the table info:
CREATE TABLE `tb_clients` (
`client_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`client_company` varchar(254) NOT NULL,
`client_address` varchar(254) NOT NULL,
`client_street` varchar(254) NOT NULL,
`client_city` varchar(254) NOT NULL,
`client_state` varchar(254) NOT NULL,
`client_zip` varchar(45) NOT NULL,
`client_email` varchar(254) NOT NULL,
`client_website` varchar(254) NOT NULL,
`client_description` text NOT NULL,
`client_keywords` text NOT NULL,
PRIMARY KEY (`client_id`) USING BTREE,
FULLTEXT KEY `client_company` (`client_company`,`client_description`,`client_keywords`)
) ENGINE=MyISAM AUTO_INCREMENT=68347 DEFAULT CHARSET=latin1;
AND
CREATE TABLE `tb_clients_phone_fax` (
`client_phone_fax_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`client_id` int(11) unsigned NOT NULL,
`client_phone_1` varchar(45) NOT NULL,
`client_phone_2` varchar(45) NOT NULL,
`client_phone_3` varchar(45) NOT NULL,
`client_fax_1` varchar(45) NOT NULL,
`client_fax_2` varchar(45) NOT NULL,
`client_fax_3` varchar(45) NOT NULL,
PRIMARY KEY (`client_phone_fax_id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=33944 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
tb_clients.client_id and tb_clients_phone_fax.client_id should be indexed.
But the main problem seems to be the two strings comparisons, MATCH and the LIKE with the CONCAT. Are you sure there is no other war around it? like, avoiding concatenating all the address fields before doing the LIKE statement?
UPDATE: It seems that tb_clients_phone_fax.client_id is not indexed, it would improve the performance if it’s indexed.