简体   繁体   English

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

[英]Why is my query not using this index?

I have a query I turned into a view that works OK. 我有一个查询,我变成了可以正常工作的视图。 But the site_phrase sp table seems to be not using a column and goes through all the records in the table. 但是site_phrase sp表似乎未使用列,并且会遍历表中的所有记录。 Why is that? 这是为什么? Here is the query: 这是查询:

    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`

And here is a screenshot of the output the above query gets when I EXPLAIN / DESCRIBE it http://img827.imageshack.us/img827/3336/indexsql.png 这是上面的查询在我解释/描述它时输出的屏幕快照http://img827.imageshack.us/img827/3336/indexsql.png

No matter how I alter the order of the tables above and how they are joined, the 1st or 2nd table always seems to be doing some sort of table scan. 无论我如何更改上面的表的顺序以及如何将它们连接在一起,第一或第二张表似乎总是在进行某种表扫描。 In the example of the screenshot, the problem table is sp . 在屏幕快照的示例中,问题表为sp These tables are innoDB type, and there is appropriate indexes and foreign keys on all the tables I JOIN on. 这些表是innoDB类型的,在我加入的所有表上都有适当的索引和外键。 Any insights would be helpful. 任何见解都会有所帮助。

MySQL will use a full table scan if it determines that it is faster than a using index. 如果MySQL确定它比使用索引快,它将使用全表扫描。 In the case of your SP table - with only 1300 records the table scan may be just as fast as a index. 对于SP表-只有1300条记录,表扫描可能和索引一样快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM