<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查询?索引?

        How to speed up sql queries ? Indexes?(如何加快sql查询?索引?)

        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查询?索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)(超出最大存储过程、函数、触发器或视图嵌套级别(限制 32))
                  How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                  How to debug stored procedure in VS 2015?(如何在 VS 2015 中调试存储过程?)
                  Set the variable result, from query(设置变量结果,来自查询)
                  What is dynamic SQL?(什么是动态 SQL?)
                  Mysql - How to quit/exit from stored procedure(Mysql - 如何退出/退出存储过程)

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

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

                      <tbody id='NKgVw'></tbody>
                        <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>