如何加快SQL查询?指标?

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

索引对任何数据库都至关重要.

用"外行"术语来说,索引是......好吧,恰恰相反.您可以将索引视为存储两件事的第二个隐藏表:排序数据和指向其在表中位置的指针.

关于创建索引的一些拇指规则:

  1. 在连接中使用(或将要)的每个字段上创建索引.
  2. 在要执行频繁where条件的每个字段上创建索引.
  3. 避免在一切上创建索引.在每个表的相关字段上创建索引,并使用关系来检索所需的数据.
  4. double除非绝对必要,否则请避免在字段上创建索引.
  5. 避免在varchar字段上创建索引,除非绝对必要.

我建议你阅读这篇:http://dev.mysql.com/doc/refman/5.5/en/using-explain.html

  • 双精度和变量的索引在相关时很好.我们是在2013年,而不是20世纪80年代. (3认同)