MySQL 最左前缀(Leftmost Prefix) & 组合索引(复合索引,多列索引)

时间:2022-08-30 15:26:44

资料来源于网络,仅供参考学习。

CREATE TABLE test(
a INT,
b INT,
c INT,
KEY idx(a,b,c)
);

优: SELECT * FROM test WHERE a=10 AND b>50;
差: SELECT * FROM test WHERE b>50;

优: SELECT * FROM test ORDER BY a;
差: SELECT * FROM test ORDER BY b;
差: SELECT * FROM test ORDER BY c;

优: SELECT * FROM test WHERE a=10 ORDER BY a;
优: SELECT * FROM test WHERE a=10 ORDER BY b;
差: SELECT * FROM test WHERE a=10 ORDER BY c;

优: SELECT * FROM test WHERE a>10 ORDER BY a;
差: SELECT * FROM test WHERE a>10 ORDER BY b;
差: SELECT * FROM test WHERE a>10 ORDER BY c;

优: SELECT * FROM test WHERE a=10 AND b=10 ORDER BY a;
优: SELECT * FROM test WHERE a=10 AND b=10 ORDER BY b;
优: SELECT * FROM test WHERE a=10 AND b=10 ORDER BY c;

优: SELECT * FROM test WHERE a=10 AND b=10 ORDER BY a;
优: SELECT * FROM test WHERE a=10 AND b>10 ORDER BY b;
差: SELECT * FROM test WHERE a=10 AND b>10 ORDER BY c;