我有一张包含以下表格的数据库:
员工可以拥有多个职位,公司可以拥有多名员工。
我正在尝试搜索lt_employee_title
表和company
表,并从employee_id
表中获取employee
。
表结构:
CREATE TABLE `employee` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) DEFAULT NULL,
PRIMARY KEY (`employee_id`),
KEY `employee_id` (`company_id`)
) ENGINE=InnoDB;
CREATE TABLE `company` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`URL` varchar(100) DEFAULT NULL,
`company_name` varchar(100) DEFAULT NULL,
`date_created` date DEFAULT NULL,
`date_updated` datetime DEFAULT NULL,
`address1` varchar(45) DEFAULT NULL,
`address2` varchar(45) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`phone2` varchar(20) DEFAULT NULL,
`fax` varchar(20) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`major_city_id` int(11) DEFAULT NULL,
`region_code` varchar(10) DEFAULT NULL,
`zip` varchar(15) DEFAULT NULL,
`country_code` varchar(2) DEFAULT NULL,
`archived` tinyint(4) NOT NULL DEFAULT '0',
`enabled` enum('yes','no') DEFAULT 'yes',
PRIMARY KEY (`company_id`),
KEY `country_code` (`country_code`),
KEY `employee` (`number_of_employees_id`),
KEY `phone` (`phone`(3)),
KEY `state` (`region_code`),
KEY `archived` (`archived`)
) ENGINE=InnoDB;
CREATE TABLE `lt_employee_title` (
`employee_id` int(11) NOT NULL,
`title_id` int(11) NOT NULL,
UNIQUE KEY `unique_field` (`employee_id`,`title_id`),
KEY `employee_id` (`employee_id`),
KEY `title_id` (`title_id`),
KEY `both` (`title_id`,`employee_id`),
KEY `both2` (`employee_id`,`title_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$;
此查询很快:
SELECT SQL_NO_CACHE
*
FROM lt_employee_title AS let
JOIN employee AS ee
ON ee.employee_id = let.employee_id
JOIN company AS com
ON com.company_id = ee.company_id
WHERE let.title_id = 9
AND com.region_code IN('AL', 'AK', 'AZ')
AND com.archived = 0
LIMIT 125;
但这些不是:
SELECT SQL_NO_CACHE
COUNT(*)
FROM lt_employee_title AS let
...
SELECT SQL_NO_CACHE SQL_CALC_FOUND_ROWS
*
FROM lt_employee_title AS let
...
添加任何类型的计数的差异将时间从0.016秒移动到4.0秒。我们有大约250,000个标题,900,000个公司条目和超过100万个员工记录,但将会增长。
当我添加count()
或SQL_CALC_FOUND_ROWS
或ORDER BY com.number_of_employees_id
时,EXPLAIN正在使用临时表和file_sort,这就是速度降低的原因。有没有办法更有效地执行此SQL?
使用“SQL_CALC_FOUND_ROWS”进行解释
1 SIMPLE company ref PRIMARY,state,created,updated,employee_number,archived state 33 const 210864 Using where; Using filesort
1 SIMPLE employee ref PRIMARY,employee_id,created,updated employee_id 5 lead411_main.company.company_id 1 Using where; Using index
1 SIMPLE lt_employee_title eq_ref unique_field,employee_id,title_id,both,both2 unique_field 8 lead411_main.employee.employee_id,const 1 Using index
解析OUT“SQL_CALC_FOUND_ROWS”
1 SIMPLE company index PRIMARY,state,created,updated,employee_number,archived employee 5 986 Using where
1 SIMPLE employee ref PRIMARY,employee_id,created,updated employee_id 5 lead411_main.company.company_id 1 Using where; Using index
1 SIMPLE lt_employee_title eq_ref unique_field,employee_id,title_id,both,both2 unique_field 8 lead411_main.employee.employee_id,const 1 Using index