What is the fastest way to join multiple tables matching specific column values ​​in MySQL

I have 3 tables that look like this:

CREATE TABLE big_table_1 (
 id   INT(11),
 col1 TINYINT(1),
 col2 TINYINT(1),
 col3 TINYINT(1),
 PRIMARY KEY (`id`)
)

And so on for big_table_2 and big_table_3. The values ​​of col1, col2, col3 are 0, 1, or null.

I am looking for an identifier whose col1 value is 1 in each table. I join them as follows, using the simplest way that I can think of:

SELECT t1.id 
FROM big_table_1 AS t1
INNER JOIN big_table_2 AS t2 ON t2.id = t1.id
INNER JOIN big_table_3 AS t3 ON t3.id = t1.id
WHERE t1.col1 = 1
AND t2.col1 = 1
AND t3.col1 = 1;

With 10 million rows in the table, the query takes about 40 seconds to execute on my machine:

407231 rows in set (37.19 sec)

Explain the results:

+----+-------------+-------+--------+---------------+---------+---------+--------------+----------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref          | rows     | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+--------------+----------+-------------+
|  1 | SIMPLE      | t3    | ALL    | PRIMARY       | NULL    | NULL    | NULL         | 10999387 | Using where |
|  1 | SIMPLE      | t1    | eq_ref | PRIMARY       | PRIMARY | 4       | testDB.t3.id |        1 | Using where |
|  1 | SIMPLE      | t2    | eq_ref | PRIMARY       | PRIMARY | 4       | testDB.t3.id |        1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+--------------+----------+-------------+

If I declare an index on col1, the result will be slightly slower:

407231 rows in set (40.84 sec)

I also tried the following query:

SELECT t1.id
FROM (SELECT distinct ta1.id FROM big_table_1 ta1 WHERE ta1.col1=1) as t1
WHERE EXISTS (SELECT ta2.id FROM big_table_2 ta2 WHERE ta2.col1=1 AND ta2.id = t1.id)
AND EXISTS (SELECT ta3.id FROM big_table_3 ta3 WHERE ta3.col1=1 AND ta3.id = t1.id);

But it is slower:

407231 rows in set (44.01 sec) [with index on col1]
407231 rows in set (1 min 36.52 sec) [without index on col1]

MySQL? , ?

: EXPLAIN Andrew ( 1 , - id col1):

+----+-------------+-------------+-------+---------------+---------+---------+------+---------+--------------------------------+
| id | select_type | table       | type  | possible_keys | key     | key_len | ref  | rows    | Extra                          |
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+--------------------------------+
|  1 | PRIMARY     | <derived3>  | ALL   | NULL          | NULL    | NULL    | NULL |  332814 |                                |
|  1 | PRIMARY     | <derived4>  | ALL   | NULL          | NULL    | NULL    | NULL |  333237 | Using where; Using join buffer |
|  1 | PRIMARY     | <derived2>  | ALL   | NULL          | NULL    | NULL    | NULL |  333505 | Using where; Using join buffer |
|  4 | DERIVED     | big_table_3 | index | NULL          | PRIMARY | 5       | NULL | 1000932 | Using where; Using index       |
|  3 | DERIVED     | big_table_2 | index | NULL          | PRIMARY | 5       | NULL | 1000507 | Using where; Using index       |
|  2 | DERIVED     | big_table_1 | index | NULL          | PRIMARY | 5       | NULL | 1000932 | Using where; Using index       |
+----+-------------+-------------+-------+---------------+---------+---------+------+---------+--------------------------------+
+4
3

( id, col1) , 1 . .

analyze table xxx (3 , )

, , mysql cbo , .

- where. join on

+1

:

SELECT t1.id 
FROM 
(SELECT id from big_table_1 where col1 = 1) AS t1
INNER JOIN (SELECT id from  big_table_2 where col1 = 1)  AS t2 ON t2.id = t1.id
INNER JOIN (SELECT id from  big_table_3 where col1 = 1)  AS t3 ON t3.id = t1.id
0

INNER JOIN ( , JOIN) , . SELECT, , .

WHERE. , , INDEX, col1. (. .)

. col1, id JOINing. INDEX(col1, id) INDEX(id, col1) .

" ", , -, .

() " ", , col%, 0,1, NULL , INDEX(col1) , , .

, INDEX(col1, ...) , .

. INDEX, .

"". , , , id col1. "" , . , , , .

( ) INDEX(col1, id), .

, , , col1 3 . .

ORDER BY, IN(...), BETWEEN...AND..., , PRIMARY KEY, LEFT JOIN ..

Selects.

ANALYZE TABLE .

0
source

All Articles