1

我有一个包含以下表格的数据库:

  • 员工
  • 公司
  • lt_employee_title

员工可以有多个头衔,公司可以有多个员工。

我正在尝试在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()orSQL_CALC_FOUND_ROWSORDER 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

解释没有“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  
4

0 回答 0