<small id='EWOSR'></small><noframes id='EWOSR'>

<tfoot id='EWOSR'></tfoot>
<i id='EWOSR'><tr id='EWOSR'><dt id='EWOSR'><q id='EWOSR'><span id='EWOSR'><b id='EWOSR'><form id='EWOSR'><ins id='EWOSR'></ins><ul id='EWOSR'></ul><sub id='EWOSR'></sub></form><legend id='EWOSR'></legend><bdo id='EWOSR'><pre id='EWOSR'><center id='EWOSR'></center></pre></bdo></b><th id='EWOSR'></th></span></q></dt></tr></i><div id='EWOSR'><tfoot id='EWOSR'></tfoot><dl id='EWOSR'><fieldset id='EWOSR'></fieldset></dl></div>
        <bdo id='EWOSR'></bdo><ul id='EWOSR'></ul>

        <legend id='EWOSR'><style id='EWOSR'><dir id='EWOSR'><q id='EWOSR'></q></dir></style></legend>
      1. 如何加快sql查询?索引?

        时间:2023-10-26

        1. <legend id='T6ydM'><style id='T6ydM'><dir id='T6ydM'><q id='T6ydM'></q></dir></style></legend>

          <small id='T6ydM'></small><noframes id='T6ydM'>

          • <bdo id='T6ydM'></bdo><ul id='T6ydM'></ul>
              <tbody id='T6ydM'></tbody>

              <tfoot id='T6ydM'></tfoot>
              <i id='T6ydM'><tr id='T6ydM'><dt id='T6ydM'><q id='T6ydM'><span id='T6ydM'><b id='T6ydM'><form id='T6ydM'><ins id='T6ydM'></ins><ul id='T6ydM'></ul><sub id='T6ydM'></sub></form><legend id='T6ydM'></legend><bdo id='T6ydM'><pre id='T6ydM'><center id='T6ydM'></center></pre></bdo></b><th id='T6ydM'></th></span></q></dt></tr></i><div id='T6ydM'><tfoot id='T6ydM'></tfoot><dl id='T6ydM'><fieldset id='T6ydM'></fieldset></dl></div>

                  本文介绍了如何加快sql查询?索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下数据库结构:

                  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 on these tables and I'm trying to optimize the query to run as fast 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 do I make this query run as fast as possible ? Should I use indexes ? If so, which columns of which tables should I index ?

                  这是我的查询计划的图片:

                  Here is a picture of my query plan :

                  谢谢,

                  推荐答案

                  索引对于任何数据库都是必不可少的.

                  Indexes are essential to any database.

                  用外行"术语来说,索引是……嗯,正是如此.您可以将索引视为第二个隐藏的表,它存储两件事:排序后的数据和指向其在表中位置的指针.

                  Speaking in "layman" terms, indexes are... well, precisely that. You can think of an index as a second, hidden, table that stores two things: The sorted data and a pointer to its position in the table.

                  创建索引的一些经验法则:

                  Some thumb rules on creating indexes:

                  1. 在连接中使用(或将使用)的每个字段上创建索引.
                  2. 在要对其执行频繁 where 条件的每个字段上创建索引.
                  3. 避免为所有内容创建索引.在每个表的相关字段上创建索引,并使用关系检索所需数据.
                  4. 避免在 double 字段上创建索引,除非绝对必要.
                  5. 避免在 varchar 字段上创建索引,除非绝对必要.
                  1. Create indexes on every field that is (or will be) used in joins.
                  2. Create indexes on every field on which you want to perform frequent where conditions.
                  3. Avoid creating indexes on everything. Create index on the relevant fields of every table, and use relations to retrieve the desired data.
                  4. Avoid creating indexes on double fields, unless it is absolutely necessary.
                  5. Avoid creating indexes on varchar fields, unless it is absolutely necesary.

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

                  这篇关于如何加快sql查询?索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我应该规范化我的数据库吗? 下一篇:为什么使用临时表比嵌套查询更快?

                  相关文章

                  <legend id='NKgVw'><style id='NKgVw'><dir id='NKgVw'><q id='NKgVw'></q></dir></style></legend>

                  <small id='NKgVw'></small><noframes id='NKgVw'>

                    <i id='NKgVw'><tr id='NKgVw'><dt id='NKgVw'><q id='NKgVw'><span id='NKgVw'><b id='NKgVw'><form id='NKgVw'><ins id='NKgVw'></ins><ul id='NKgVw'></ul><sub id='NKgVw'></sub></form><legend id='NKgVw'></legend><bdo id='NKgVw'><pre id='NKgVw'><center id='NKgVw'></center></pre></bdo></b><th id='NKgVw'></th></span></q></dt></tr></i><div id='NKgVw'><tfoot id='NKgVw'></tfoot><dl id='NKgVw'><fieldset id='NKgVw'></fieldset></dl></div>
                      <tfoot id='NKgVw'></tfoot>
                      • <bdo id='NKgVw'></bdo><ul id='NKgVw'></ul>