【发布时间】:2021-05-06 21:40:54
【问题描述】:
假设我有一个定义了这个主键的表
CREATE TABLE `my_composite_table` (
`table_id` bigint(20) unsigned NOT NULL,
`first_id` bigint(20) unsigned NOT NULL,
`second_id` bigint(20) unsigned NOT NULL,
`third_id` bigint(20) unsigned NOT NULL,
`create_date` int(10) unsigned NOT NULL,
`update_date` int(10) unsigned NOT NULL,
PRIMARY KEY (`first_id`, `second_id`, `third_id`, `table_id`)
)
根据mysql index prefix。我可以像这样高效地查询这张表
SELECT *
FROM my_composite_table
WHERE first_id = 1 AND second_id = 2 AND third_id = 3 AND table_id = 4;
SELECT * FROM my_composite_table
WHERE first_id = 1 AND second_id = 2 AND third_id = 3;
SELECT *
FROM my_composite_table
WHERE first_id = 1 AND second_id = 2;
SELECT *
FROM my_composite_table
WHERE first_id = 1;
我的问题是这条规则是否同样适用于复合主键。我想进行这样的查询:
SELECT *
FROM my_composite_table
WHERE first_id = 1 AND table_id = 4;
查询会使用主键索引,还是只需要为这两个字段创建索引?
【问题讨论】: