简体   繁体   English

在 MySQL 中使用 OR 避免对简单连接进行全表扫描

[英]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.所以我在每个表的列position上添加了两个索引。 Now I need to look for a range of position in BOTH table (by joining then and apply an OR query)现在我需要在 BOTH 表中查找一系列位置(通过加入 then 并应用 OR 查询)

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如果我更改为AND如果给我预期的范围索引扫描

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 ?但我需要这里的OR语句......我怎么能让 mysql 对 OR 语句使用范围扫描索引? 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).我可以在这里改进我​​的索引table1_id (我考虑了positiontable1_id上的多值索引 - 外键,但它没有帮助,它执行了全表扫描)。

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 )

Presumably you want to avoid the table scan for reasons of performance.据推测,出于性能原因,您希望避免表扫描。

So try swapping your OR for a UNION operation.因此,尝试将 OR 交换为 UNION 操作。

First, get the set of table1.id values you need with a subquery, like so.首先,使用子查询获取所需的table1.id值集,如下所示。

    SELECT id FROM table_1 WHERE position BETWEEN 5000 AND 5500
    UNION
    SELECT table1_id FROM table_2 WHERE position BETWEEN 5000 AND 5500

The second part of the UNION retrieves the table_1.id values you need from table_2, by SELECTing the FK column. UNION 的第二部分通过选择 FK 列从 table_2 中检索您需要的 table_1.id 值。

Next, use that subquery to get your rows from table1 .接下来,使用该子查询从table1获取您的行。

SELECT * FROM table_1
 WHERE id IN (
    SELECT id FROM table_1 WHERE position BETWEEN 5000 AND 5500
    UNION
    SELECT table1_id FROM table_2 WHERE position BETWEEN 5000 AND 5500           
)

To make this quicker, add this compound index on table_2.为了加快速度,请在 table_2 上添加此复合索引。

CREATE INDEX ix_table_2_position_table1id 
          ON table_2 (position, table1_id);

Notice that neither of your two single-column indexes on table2 are useful for this query.请注意,table2 上的两个单列索引都不适用于此查询。

Performance problems with OR can often be solved with UNION : OR性能问题通常可以用UNION解决:

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
SELECT *
FROM table_1
INNER JOIN table_2 ON table_1.id = table_2.table1_id
WHERE table_2.position BETWEEN 5000 AND 5500;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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