Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

join

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?

like image 376
Drarok Avatar asked Aug 13 '09 14:08

Drarok


People also ask

How do I force an index in MySQL?

Pre-requisites: You have to create a database table with data in a MySQL database to check the Force Index feature of MySQL. Open the terminal and connect with the MySQL server by executing the following command. Run the following command to create a database named test_db.

How do you force an index?

How the Force Index Works. The force index is calculated by subtracting yesterday's close from today's close and multiplying the result by today's volume. If closing prices are higher today than yesterday, the force is positive. If closing prices are lower than yesterday's, the force is negative.

What is the syntax for a full join in MySQL?

SELECT table1. column1, table2. column2... FROM table1 FULL JOIN table2 ON table1.

Does join use index MySQL?

MySQL only supports using a single index per join. If you want it to utilize two columns as indices in the join, you should create a single index over those two columns. Note that this isn't as bad as it seems, because an index over (a,b) doubles as an index over just a.


1 Answers

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`) 
like image 193
Drarok Avatar answered Sep 26 '22 03:09

Drarok