Indexing rule - use OR less, it will invalidate the index when connecting

 

Mysql series article home page 

 

===============

 

1 Prepare data

1.1 Create a table

DROP TABLE IF EXISTS staff;
CREATE TABLE IF NOT EXISTS staff (
    id INT PRIMARY KEY auto_increment,
    name VARCHAR(50),
    age INT,
    pos VARCHAR ( 50 ) COMMENT ' Position ' ,
    salary DECIMAL(10,2)
);

1.2 Insert data

INSERT INTO staff(name, age, pos, salary) VALUES('Alice', 22, 'HR', 5000);

2 Test & Explain Analysis

2.1 Create an index

CREATE INDEX idx_nameAgePos ON staff(name, age, pos);

2.2 Testing

EXPLAIN SELECT * FROM staff WHERE name = 'Alice' or name = 'Bob';

Result: type=all, index invalidation, full table scan.

3 Conclusion

Index invalidation when OR join

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773826&siteId=291194637