mysql - Slow performance on joining table -
i trying optimize query below , have study how add index improve peformance , result still slow. query below took 20 sec run , transaction contains around 100k records , joining table transactiondetail contains around 500k records.
select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status in ('pending', 'processing', 'success', 'rejected') order issued_date desc limit 0 , 10
from query above try apply understand indexing
i added 3 index because of inner join , transaction(agent_id,distributor_id) , transaction(transaction_id)
from clause added transaction(status)
- because of order added transaction(issued_date)
but doesn't show improvement below explain
and screenshot phpmyadmin show indexing table transaction
is there way improvement query? or optimised , should focus on mysql configuration?
your query
select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status in ('pending', 'processing', 'success', 'rejected') order issued_date desc limit 0 , 10
now have indexes applied on tables good, in clause
more or
, creates real performance issue. in case of small data-set not observed in large data-set performance dropped significantly.
one method of optimizing convert in clause
union
works better or
in
( select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status = 'pending' ) union ( select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status = 'processing' ) union ( select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status = 'success' ) union ( select transaction.id .... transaction inner join agent on agent.id = transaction.agent_id inner join distributor on distributor.id = transaction.distributor_id inner join transactiondetail on transaction.id = transactiondetail.transaction_id transactiondetail.type = 'admin' , transaction.status = 'rejected' ) order issued_date desc limit 0 , 10
for resolving order may need add index as
alter table transaction add index status_created_idx(status,issued_date);
Comments
Post a Comment