优化MySQL连接查询以删除使用临时和使用索引?

时间:2011-06-27 00:07:43

标签: mysql optimization join indexing

我有ORDER BY name的查询,name上的索引被忽略。

如何优化查询以使用索引并从EXPLAIN中删除Using temporary

我启用了log-queries-not-using-indexes,我看到了这个查询数千次。

以下是查询:

SELECT l.parent_id, j.id, j.location_id, j.currency, j.frequency, ROUND((j.salary_min + j.salary_max)/2) as salary 
FROM jobs AS j
JOIN location AS l
    ON j.location_id = l.id
WHERE j.salary_min !=0 
    AND j.status != 'Rejected'      
    AND l.published =1
    AND date_sub(now(), interval 1 month) <= j.effected_date
ORDER BY l.name

解释:

+----+-------------+-------+--------+----------------------------------+---------------+---------+----------------------------+------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                    | key           | key_len | ref                        | rows | Extra                                        |
+----+-------------+-------+--------+----------------------------------+---------------+---------+----------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | j     | range  | effected_date,location_id,status | effected_date | 9       | NULL                       |  562 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | l     | eq_ref | PRIMARY                          | PRIMARY       | 4       | esljw_joomla.j.location_id |    1 | Using where                                  |
+----+-------------+-------+--------+----------------------------------+---------------+---------+----------------------------+------+----------------------------------------------+
2 rows in set (0.01 sec)

表结构:

CREATE TABLE IF NOT EXISTS `jobs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `location_id` varchar(255) NOT NULL,
  `status` varchar(255) DEFAULT NULL,
  `currency` varchar(255) DEFAULT NULL,
  `salary_min` int(11) DEFAULT NULL,
  `salary_max` int(11) DEFAULT NULL,
  `effected_date` datetime DEFAULT NULL,
  `frequency` varchar(255) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `effected_date` (`effected_date`),
  KEY `location_id` (`location_id`),
  KEY `status` (`status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10130 ;

CREATE TABLE IF NOT EXISTS `location` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=304 ;

3 个答案:

答案 0 :(得分:0)

  1. published + id综合索引添加到location表格
  2. l.published =1条件移至ON子句
  3. 这是你可以做的事情。但是你可能永远不会摆脱using temporary,因为你不是按主表排序,而是通过连接表排序。

答案 1 :(得分:0)

这是因为您首先列出了job。更改表的顺序,如下所示:

SELECT l.parent_id, j.id, j.location_id, j.currency, j.frequency, ROUND((j.salary_min + j.salary_max)/2) as salary 
FROM location AS l
JOIN jobs AS j  ON j.location_id = l.id
WHERE j.salary_min !=0 
AND j.status != 'Rejected'      
AND l.published =1
AND date_sub(now(), interval 1 month) <= j.effected_date
ORDER BY l.name

尝试并发布它的结果。

答案 2 :(得分:0)

很多时候我已经完成了查询,其中你有适当的主表作为查询中的第一个,具有良好的索引,单独添加STRAIGHT_JOIN可以修复查询。因此,根据您现有的标准,您应该熟悉日期索引,并将其作为主要标准...例如

SELECT STRAIGHT_JOIN
      L.Parent_ID, 
      J.id, 
      J.location_id, 
      J.currency, 
      J.frequency, 
      ROUND(( J.salary_min + J.salary_max) / 2 ) as Salary 
   FROM
      jobs J
         join Location L
            on J.Location_ID = L.ID
            AND L.Published = 1
   WHERE
          J.Effected_Date >= date_sub(now(), interval 1 month)
      AND J.salary_min != 0
      AND J.status != 'Rejected'
   ORDER BY 
      L.name