Hi Guys!
I'm building a search on one of my sites and I have the following MySQL query. It's currently taking 9.5 sec in phpMyadmin to execute the code, but actually takes even longer when run on the actual site. Do I have the right database structure of tables etc.
PHP Code:
SELECT users . * , ui.industry, jt.job_type
FROM users
LEFT OUTER JOIN (
SELECT user_id, job_type
FROM users_job_types
LEFT JOIN job_types ON job_types.id = users_job_types.job_type_id
GROUP BY user_id
) AS jt ON jt.user_id = users.id
LEFT OUTER JOIN (
SELECT user_id, industry
FROM users_industries
LEFT JOIN industries ON industries.id = users_industries.industry_id
GROUP BY user_id
) AS ui ON ui.user_id = users.id
WHERE users.status = '1'
AND users.cv_hide = '0'
ORDER BY cv_date DESC
Below are the create statements:
PHP Code:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) CHARACTER SET utf8 NOT NULL,
`cv_date` datetime DEFAULT NULL,
`cv_hide` enum('0','1') CHARACTER SET utf8 NOT NULL DEFAULT '0',
`status` enum('0','1') CHARACTER SET utf8 DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1292692 ;
CREATE TABLE IF NOT EXISTS `job_types` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`job_type` varchar(100) NOT NULL,
`job_type_description` text,
`job_type_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1006 ;
CREATE TABLE IF NOT EXISTS `users_job_types` (
`user_id` int(11) NOT NULL,
`job_type_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `job_type_id` (`job_type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `users_industries` (
`user_id` int(11) NOT NULL,
`industry_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `industry_id` (`industry_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `industries` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`industry` varchar(100) NOT NULL,
`industry_description` text,
`industry_url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1052 ;
Please let me know if you know any way this can be speeded up.
Thanks.
Bookmarks