Код: Выделить всё
create table account_transactions
(
id int auto_increment primary key,
account_id int not null,
amount decimal(10, 2) not null,
type varchar(255) not null,
created_at datetime not null,
updated_at datetime not null,
constraint FK_does_not_matter
foreign key (account_id) references accounts (id),
index account_transactions_created_at (created_at),
-- + some other irrelevant columns and indexes
)
collate = utf8mb4_unicode_ci;
create table accounts
(
id int auto_increment primary key,
project_id int not null,
user_id int not null,
constraint FK_does_not_matter
foreign key (user_id) references users (id),
constraint FK_does_not_matter
foreign key (project_id) references projects (id),
-- + some other irrelevant columns and indexes
)
collate = utf8mb4_unicode_ci;
Код: Выделить всё
account_transactionsТаблица Код: Выделить всё
SELECT SUM(c0_.amount) AS sclr_0
FROM account_transactions c0_
LEFT JOIN accounts c1_ ON c0_.account_id = c1_.id
WHERE c0_.created_at >= ? -- currently '2025-12-04 00:00:00'
AND c1_.project_id = ?;
План запроса мне кажется нормальным:
Код: Выделить всё
id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra
1,SIMPLE,c1_,ref,"PRIMARY,IDX_does_not_matter",IDX_does_not_matter,4,const,270,Using index
1,SIMPLE,c0_,ref,"IDX_does_not_matter,account_transactions_created_at",IDX_does_not_matter,4,dbname.c1_.id,257,Using where
Все это работает на экземпляре AWS RDS db.t3.medium, версия движка 10.11.13 (MariaDB), и я подключаюсь к нему через Экземпляр EC2.
Эта низкая производительность вызывает у меня кучу проблем, и я действительно не понимаю, в чем проблема. Это AWS RDS? Это какая-то вина сети, хотя они предположительно находятся в одной сети AWS? Я не думаю, что мне не хватает каких-либо индексов, индексировать здесь больше нечего.
P.S. Я понятия не имею, почему в плане запроса указано rows=270/257, при выборе всех строк отображается 859 из них для рассматриваемого идентификатора проекта (но на самом деле проблема существует для всех проектов, более или менее).
Подробнее здесь: https://stackoverflow.com/questions/798 ... lue-result
Мобильная версия