使用索引和include

CREATE INDEX idx_customerMissingOrNoShipment ON #customerMissingOrNoShipment(BpaMainPkey)
INCLUDE(CustomerID,MissingValidFrom,MissingValidThru)
//一般on,where,and条件用的多的字段放索引里
--select 和 order by、join
--and 的语句里的字段 放在索引里
的字段 按照顺序放在include里 --索引是根据顺序触发的

 

复制代码
--join on的用include
--and的用 index
INNER JOIN BpaRole WITH (NOLOCK) ON BpaRole.BpaMainPKey = BpaMain.PKey
    AND BpaRole.Status <> 'd'
    AND BpaRole.SalesOrg = @salesOrg

--sql执行计划推荐的加索引
USE [PGTSTCustCAS103DB]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[BpaRole] ([SalesOrg],[Status])
INCLUDE ([BpaMainPKey],[BpaRoleMetaPKey])
GO
复制代码

 

复制代码
--select的include和on的 索引先后顺序,  join在select后

SELECT BpaAddress.Street + ' '+ BpaAddress.City + ', ' + BpaAddress.CountryState + ' ' + BpaAddress.ZipCode    AS [Ship-To Address] FROM BpaAddress

LEFT OUTER JOIN BpaAddress WITH (NOLOCK) ON BpaAddress.BpaMainPKey = customerWithMissingOrNoShipment.BpaMainPkey
    AND BpaAddress.Status <> 'd'

/*
USE [PGTSTCustCAS103DB]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[BpaAddress] ([Status])
INCLUDE ([CountryState],[ZipCode],[City],[Street],[BpaMainPKey])
GO
*/
复制代码

 

posted @   阿玛  阅读(1523)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示