Dla mnie to nie to samo, MySql 5.7 8 vCPU, 52 GB RAM
Poniższe zapytanie zajmuje ~ 30 sekund, nie jestem pewien, dlaczego
Tabela transakcji zawiera 24 257 151 rekordów
Tabela aktywności zawiera 18 603 665 rekordów
Tabela zakupów zawiera 13 911 705 rekordów
Wszystkie wymagane indeksy są na miejscu
SELECT
`trx`.`transaction_pk`,
`trx`.`created`,
`trx`.`updated`,
`p`.`amount`,
`trxst`.`name`,
COALESCE ( a.units, 0 ) AS units
FROM
`transaction` AS `trx`
INNER JOIN `transaction_sub_type` AS `trxst` ON `trx`.`transaction_sub_type_fk`= `trxst`.`transaction_sub_type_pk`
left JOIN `activity` AS `a` ON `a`.`transaction_fk` = `trx`.`transaction_pk`
LEFT JOIN `purchases` AS `p` ON `p`.`transaction_fk` = `trx`.`transaction_pk`
WHERE
`trx`.`entity_fk` IN ( 1234)
AND `trx`.`transaction_sub_type_fk` IN (
2, 4, 5, 15, 16, 33, 37, 38, 85, 86, 87, 88, 102, 103
)
ORDER BY
`trx`.`transaction_pk` DESC LIMIT 100 OFFSET 0;
Po zastąpieniu następującego wiersza:
INNER JOIN `transaction_sub_type` AS `trxst` ON `trx`.`transaction_sub_type_fk`= `trxst`.`transaction_sub_type_pk`
z LEFT JOIN
LEFT JOIN `transaction_sub_type` AS `trxst` ON `trx`.`transaction_sub_type_fk`= `trxst`.`transaction_sub_type_pk`
to samo zapytanie zajmuje ~ 0,046s
Wyjaśnij przed:
1 SIMPLE trxst ALL PRIMARY 101 37.62 Using where; Using temporary; Using filesort
1 SIMPLE trx ref transaction_sub_type_fk,entity_fk transaction_sub_type_fk 4 trxst.transaction_sub_type_pk 2548 0.36 Using where
1 SIMPLE a ref transaction_fk transaction_fk 4 trx.transaction_pk 1 100
1 SIMPLE p ref transaction_fk transaction_fk 4 trx.transaction_pk 1 100
Wyjaśnij po:
1 SIMPLE trx ref transaction_sub_type_fk,entity_fk entity_fk 4 const 81474 83.65 Using where
1 SIMPLE trxst eq_ref PRIMARY PRIMARY 4 trx.transaction_sub_type_fk 1 100
1 SIMPLE a ref transaction_fk transaction_fk 4 trx.transaction_pk 1 100
1 SIMPLE p ref transaction_fk transaction_fk 4 trx.transaction_pk 1 100