Рассмотрим следующие два запроса:
select a.*, c.*
from account a
join customer c on a.customer_id = c.id
join import i on a.import_id = i.id
join import_bundle ib on i.import_bundle_id = ib.id
и
select a.*, c.*
from account a
join customer c on a.customer_id = c.id
join import i on a.import_id = i.id
join import_bundle ib on i.import_bundle_id = ib.id
where ib.id = 8
Первый запрос быстрый, а второй супер медленный.Есть идеи почему?Я предполагаю, что мне нужен индекс или что-то, но я не понимаю, как работают индексы.Я использую MySQL.
Вот что произойдет, если я сделаю EXPLAIN
для второго запроса:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE ib const PRIMARY PRIMARY 8 const 1 Using index
1 SIMPLE c ALL PRIMARY 144858
1 SIMPLE a ref fk_account_customer_id,fk_account_import_id fk_account_customer_id 8 mcif.c.id 2
1 SIMPLE i eq_ref PRIMARY,import_bundle_id PRIMARY 8 mcif.a.import_id 1 Using where
Хотя я не знаю, как это интерпретировать.
Редактировать: вот что я в итоге использовал:
select a.*,
c.*
from account a
join customer c on a.customer_id = c.id
join (select id,
import_bundle_id
from import
where import_bundle_id = 8) i on a.import_id = i.id
join import_bundle ib on i.import_bundle_id = ib.id
Добавление индекса на import_bundle.id
ничего не сделало.