I have the following database structure:
create table Accounting ( Channel, Account ) create table ChannelMapper ( AccountingChannel, ShipmentsMarketPlace, ShipmentsChannel ) create table AccountMapper ( AccountingAccount, ShipmentsComponent ) create table Shipments ( MarketPlace, Component, ProductGroup, ShipmentChannel, Amount )
I have the following query running in these tables, and I'm trying to optimize the query to run as quickly as possible:
select Accounting.Channel, Accounting.Account, Shipments.MarketPlace from Accounting join ChannelMapper on Accounting.Channel = ChannelMapper.AccountingChannel join AccountMapper on Accounting.Accounting = ChannelMapper.AccountingAccount join Shipments on ( ChannelMapper.ShipmentsMarketPlace = Shipments.MarketPlace and ChannelMapper.AccountingChannel = Shipments.ShipmentChannel and AccountMapper.ShipmentsComponent = Shipments.Component ) join (select Component, sum(amount) from Shipment group by component) as Totals on Shipment.Component = Totals.Component
How to make this request as fast as possible? Should I use indexes? If so, in which columns should tables be indexed?
Here is an image of my query plan:

Thanks,

optimization sql database mysql
Attilah
source share