Внешний ключ без индекса
Внешний ключ без индекса
Карточка члена экипажа показывает его вахты. Открывается медленно у всех, хотя вахт у человека — десятки.
crew_id — внешний ключ на ls_crew. PostgreSQL создаёт индекс под ПЕРВИЧНЫЙ ключ, но под внешний — никогда: связь объявлена, а дороги по ней нет. Двести тысяч вахт читаются целиком ради сорока строк.
Постройте недостающую дорогу — и заодно избавьте план от сортировки.
Speed this query up with an index
SELECT shift_id, duty_day, deck, hours FROM ls_shifts WHERE crew_id = 4242 ORDER BY duty_day
Just CREATE/ALTER/DROP — no trailing SELECT needed.
What counts as solved
You may only change the schema: indexes and ANALYZE. The query stays as the task wrote it — no need to send it back.
- the plan has no Seq Scan on ls_shifts
- the query reads at most 134 8 KB pages
Grading runs on a larger dataset than the samples show — the answer has to hold at scale.
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign inFocus radio
Paused · SomaFM · Fluid