Att*_*lah 10 mysql sql database optimization
我有以下数据库结构:
create table Accounting
(
Channel,
Account
)
create table ChannelMapper
(
AccountingChannel,
ShipmentsMarketPlace,
ShipmentsChannel
)
create table AccountMapper
(
AccountingAccount,
ShipmentsComponent
)
create table Shipments
(
MarketPlace,
Component,
ProductGroup,
ShipmentChannel,
Amount
)
Run Code Online (Sandbox Code Playgroud)
我在这些表上运行以下查询,并且我正在尝试优化查询以尽可能快地运行:
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
Run Code Online (Sandbox Code Playgroud)
如何使此查询尽可能快地运行?我应该使用索引吗?如果是这样,我应该索引哪些表的哪些列?
这是我的查询计划的图片:
谢谢,
Bar*_*nka 24
索引对任何数据库都至关重要.
用"外行"术语来说,索引是......好吧,恰恰相反.您可以将索引视为存储两件事的第二个隐藏表:排序数据和指向其在表中位置的指针.
关于创建索引的一些拇指规则:
where
条件的每个字段上创建索引.double
除非绝对必要,否则请避免在字段上创建索引.varchar
字段上创建索引,除非绝对必要.我建议你阅读这篇:http://dev.mysql.com/doc/refman/5.5/en/using-explain.html