建表语句👇
CREATE TABLE `tweets` (
`id` int NOT NULL AUTO_INCREMENT,
`content` varchar(255) NOT NULL,
`created_at` datetime(6) NOT NULL,
`user_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `tweets_tweet_user_id_created_at_d07f7061_idx` (`user_id`, `created_at`),
) ENGINE = InnoDB
查询语句👇
SELECT
*
FROM
tweet
WHERE
(
user_id = 1
OR user_id = 10
)
AND created_at < '2021-11-11 00:00:00';
👆 这样使用 or
连接查询会走索引吗?可能会走还是一定不走?
SELECT
*
FROM
tweets
WHERE
user_id in (1, 2)
AND created_at < '2021-11-11 00:00:00';
👆 改成 in
连接又如何?
SELECT
*
FROM
tweet
WHERE
user_id BETWEEN (
1
AND 10
)
AND created_at < '2021-11-11 00:00:00';
👆 between
关键字又如何呢?
忽略语义变化的影响
or 连接查询不会走索引;
改成 in 连接的话,使用到了user_id索引,由于created_at 使用了范围条件导致后面索引失效,整体上来说用到了部分索引,优于or连接的