(root@localhost) [yandb]> show create table orders \G *************************** 1. row *************************** Table: orders Create Table: CREATE TABLE `orders` ( `order_id` int NOT NULL, `user_id` int DEFAULT NULL, `product_id` int DEFAULT NULL, `order_date` datetime DEFAULT NULL, PRIMARY KEY (`order_id`), KEY `user_id` (`user_id`), KEY `product_id` (`product_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.01 sec)
发现删除列之后,复合索引会变成普通索引。
但是,相同案例,在 PostgreSQL 数据库中,则有不同表现。
1 2 3 4 5
postgres=# select version(); version --------------------------------------------------------------------------------------------------------- PostgreSQL 15.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit (1 row)
创建测试表:
1 2 3 4 5 6 7 8 9
create table orders ( order_id int primary key, user_id int, product_id int, quantity int, order_date date ); create index idx1 on orders(user_id); create index idx2 on orders(product_id, quantity);