为什么我的查询不使用此索引?

时间:2010-10-01 17:00:12

标签: optimization mysql query-optimization

我有一个查询,我变成了一个可行的视图。但是site_phrase sp表似乎没有使用列并遍历表中的所有记录。这是为什么?这是查询:

    EXPLAIN SELECT
             `p`.`id`            AS `id`,
             `p`.`text`          AS `phrase`,
             `p`.`ignored`       AS `ignored_phrase`,
             `p`.`client_id`     AS `client_id`,
             `s`.`id`            AS `site_id`,
             `s`.`sub_domain`    AS `sub_domain`,
             `s`.`competitor`    AS `competitor`,
             `s`.`ignored`       AS `ignored_site`,
             `pc`.`id`           AS `pc_id`,
             `pc`.`name`         AS `pc_name`,
             `psc`.`id`          AS `psc_id`,
             `psc`.`name`        AS `psc_name`,
             `p`.`volume`         AS `volume`,
             MIN(`pos`.`position`) AS `position`,
             `pos`.`id`         AS `pos_id`
  FROM `client` c 
  JOIN client_site cs ON cs.client_id = c.id
  JOIN site s ON s.id = cs.site_id
  JOIN site_phrase sp ON sp.site_id = s.id
  JOIN phrase p ON p.id = sp.phrase_id
  JOIN `position` pos ON pos.phrase_id = sp.phrase_id
    AND pos.site_id = sp.site_id
  LEFT JOIN `phrase_sub_category` `psc`
    ON `psc`.`id` = `p`.`phrase_sub_category_id`
  LEFT JOIN `phrase_category` `pc`
    ON `pc`.`id` = `psc`.`phrase_category_id`
  GROUP BY `p`.`id`,`s`.`id`,`serp`.`id`
  ORDER BY `p`.`id`,`pos`.`position`

以下是我解析/解析它时上述查询得到的输出的屏幕截图 http://img827.imageshack.us/img827/3336/indexsql.png

无论我如何改变上表的顺序以及它们如何连接,第1或第2表似乎总是进行某种表扫描。在屏幕截图的示例中,问题表为sp。这些表是innoDB类型,并且在我加入的所有表上都有适当的索引和外键。任何见解都会有所帮助。

1 个答案:

答案 0 :(得分:1)

如果MySQL确定它比使用索引更快,它将使用全表扫描。对于SP表 - 只有1300条记录,表扫描可能与索引一样快。