mysqlsqljoinindexingsql-optimization

Avoid full table scan on a simple join using OR in MySQL


I have this schema

create table table1
(
    id        int          auto_increment primary key,
    name      varchar(2)   null,
    position  int          null,
);

create index table1_position
    on table1 (position);


create table table_2
(
    id              int auto_increment primary key,
    table1_id       int          null,
    position        int          null,
    constraint table_2_ibfk_1
        foreign key (table1_id) references table1 (id)
);

create index ix_table_2_position
    on table_2 (position);

create index table1_id
    on table_2 (table1_id);

So I added two index on column position of each tables. Now I need to look for a range of position in BOTH table (by joining then and apply an OR query)

so I have this query

SELECT *
FROM table_1
    INNER JOIN table_2 ON table_1.id = table_2.table1_id
WHERE table_1.position BETWEEN 5000 AND 5500
   OR table_2.position BETWEEN 5000 AND 5500

But the Explain query output give me ALL (Full table scan)

id             1
select_type    SIMPLE
table          table_1
partitions
type           ALL
possible_keys  PRIMARY,table1_position
key
key_len
ref
rows           9929
filtered       100.0
Extra

If I change to an AND if give me the expected Range index scan

SELECT *
FROM table_1
    INNER JOIN table_2 ON table_1.id = table_2.table1_id
WHERE table_1.position BETWEEN 5000 AND 5500
   AND table_2.position BETWEEN 5000 AND 5500
id             1
select_type    SIMPLE
table          table_1
partitions
type           range
possible_keys  PRIMARY,pos_idx2
key            pos_idx2
key_len        5
ref
rows           1
filtered       100.0
Extra          Using index condition

But I need the OR statement here...How could I have mysql use a range scan index for an OR statement ? Could I improve my indexes here (I thought about a multi-values index on both position and table1_id -the foreign key, but it did'nt help and it performed a full table scan).


Solution

  • SELECT *
    FROM table_1
    INNER JOIN table_2 ON table_1.id = table_2.table1_id
    WHERE table_1.position BETWEEN 5000 AND 5500
    
    UNION ALL
    
    SELECT *
    FROM table_1
    INNER JOIN table_2 ON table_1.id = table_2.table1_id
    WHERE table_1.position NOT BETWEEN 5000 AND 5500
      AND table_2.position BETWEEN 5000 AND 5500
    

    Also test

    SELECT *
    FROM table_1
    INNER JOIN table_2 ON table_1.id = table_2.table1_id
    WHERE NOT (    table_1.position NOT BETWEEN 5000 AND 5500
               AND table_2.position NOT BETWEEN 5000 AND 5500 )