在 MySQL 中强制使用索引进行连接的语法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1272399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:52:18  来源:igfitidea点击:

What is the syntax to force the use of an index for a join in MySQL

mysqlsyntaxjoin

提问by Drarok

The use of the "FORCE/USE/IGNORE INDEX" when doing a straightforward select is well-documented, but it's not clear from the documentation how to do it for a JOIN.

在进行直接选择时使用“FORCE/USE/IGNORE INDEX”是有据可查的,但文档中不清楚如何为 JOIN 执行此操作。

How do you force a specific index to be used for a joined table?

您如何强制将特定索引用于连接表?

回答by Drarok

The FORCE/USE/IGNORE goes after the table name you are joining, and after the alias if you're using one.

FORCE/USE/IGNORE 在您加入的表名之后,如果您使用的是别名,则在别名之后。

SELECT
  t1.`id` AS `id_1`,
  t2.`id` AS `id_2`
FROM
  `table1` t1
LEFT OUTER JOIN
  `table2` t2
  FORCE INDEX FOR JOIN (`table1_id`)
  ON (t2.`table1_id` = t1.`id`)