简体   繁体   中英

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

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.

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

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

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`)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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