【发布时间】:2021-04-03 23:02:21
【问题描述】:
这是我的表的结构。
create table bids
(
id int unsigned auto_increment
primary key,
user_id int unsigned not null,
shipment_id int unsigned not null,
amount double(8, 2) not null,
commission double(8, 2) not null,
invoice_id int unsigned null,
created_at timestamp default CURRENT_TIMESTAMP not null,
accepted_at timestamp null,
retracted_at timestamp null
)
collate = utf8mb4_unicode_ci;
create index bids_shipment_id_index
on bids (shipment_id);
create index bids_user_id_index
on bids (user_id);
create index bids_user_id_shipment_id_index
on bids (user_id, shipment_id)
表格有大约 60M 行
像这样运行一个简单的查询:SELECT * FROM bids WHERE user_id = 3344; 大约需要 2 秒。
如何让这个查询运行得更快?
【问题讨论】:
-
您已经在
user_id上建立了索引,因此您无能为力。 --- 注意索引bids_user_id_index是多余的,因为2列索引bids_user_id_shipment_id_index以user_id开头,所以你应该删除索引bids_user_id_index以提高插入性能。 -
不要不发布表格图片。将
CREATE TABLE和在本例中的CREATE INDEX语句发布为text。 -
你可以试试OPTIMIZE TABLE。这其中是否成功主要取决于被查询的表的碎片化。
-
表使用的是哪个引擎?
-
@RickJames InnoDB.