简体   繁体   English

非常慢的查询内部联接

[英]Terribly slow query inner join

In my application I have products and categories. 在我的应用程序中,我有产品和类别。 A product can be in multiple categories. 产品可以是多个类别。 So I have two tables: CwObject (products) and EntityObjectLink (Link between product and category). 所以我有两个表:CwObject(产品)和EntityObjectLink(产品和类别之间的链接)。 I have one query that is used a lot but even after days of tweeking it is terribly slow. 我有一个经常使用的查询,但即使经过几天的调整,它也非常慢。 There are aproximately 400K records in CwObject and 1.2M in EntityObjectLink. 在CwObject中有大约400K的记录,在EntityObjectLink中有大约1.2M的记录。

This is the query: 这是查询:

SELECT TOP (99999) 
    CwObject.* 
FROM 
    CwObject 
INNER JOIN 
    dbo.EntityObjectLink ON CwObject.CwObject_Guid = EntityObjectLink.EntityObjectLink_LinkedCwObject_Guid 
WHERE
    EntityObjectLink_LinkedCwEntity_Guid = '9a0e41d7-a472-445e-b94f-44fe1a1506b3' 
    AND CwObject_CwSiteCluster_Guid = '0f178176-9720-41c7-9528-99fdf30005e8' 
    AND CwObject_EntityType = 1 
    AND (CwObject_Predecessor_Guid IS NULL)
ORDER BY 
    CwObject_Name ASC

EntityObjectLink has a relevant clustered index: EntityObjectLink具有相关的聚簇索引:

PRIMARY KEY CLUSTERED ([EntityObjectLink_LinkedCwEntity_Guid] ASC,
                       [EntityObjectLink_LinkedCwObject_Guid] ASC)
       WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
             SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, 
             ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

The CwObject table has a relevant index: CwObject表有一个相关的索引:

NONCLUSTERED INDEX [IX_ClusterEntitytypePredecessorStatusClusteraccount] 
ON [dbo].[CwObject]([CwObject_CwSiteCluster_Guid] ASC,
                    [CwObject_EntityType] ASC,
                    [CwObject_Predecessor_Guid] ASC,
                    [CwObject_Status] ASC,
                    [CwObject_ClusterAccount_Guid] ASC)
INCLUDE (% ALL other columns%) 
       WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
             SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, 
             ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

If I use the query optimiser it tells me the query uses both indexes. 如果我使用查询优化器,它告诉我查询使用两个索引。 But I see a couple of things I don't understand: 但我看到一些我不明白的事情:

  1. It warns me that there are no column statistics for entity type and predecessorguid. 它警告我,实体类型和predecessorguid没有列统计信息。 Could it be because I just added the index? 可能是因为我刚刚添加了索引?

  2. There is a huge difference between the actual and estimated row read on the CwObjects table. CwObjects表上的实际读取行数和估计行数之间存在巨大差异。

If I look at the live query statistics the query reads 171K records in CwObjects with an index seek (why a seek?). 如果我看实况查询统计查询中读取171K记录CwObjects与索引查找(为什么求?)。 Then does a merge join on the EntityObjectLink table where it reads 2.5K records. 然后在EntityObjectLink表上进行合并连接,其中读取2.5K记录。 It would have been much more effective to do it the other way around. 以相反的方式做它会更有效。

I am really, really stuck here... Can anyone help? 我真的,真的被困在这里......任何人都可以帮忙吗?

Here is the execution plan: https://1drv.ms/u/s!AlCbN2sexrJ-hNJjeSR9cZPOEpOHww 这是执行计划: https//1drv.ms/u/s!AlCbN2sexrJ-hNJjeSR9cZPOEpOHww

UPDATE: 更新:

AtoStats are ON, a few hours old. AtoStats开启,几个小时。

Almost all time is consumed in the index seek on CwObject... 几乎所有的时间都在CwObject的索引中消耗了......

UPDATE 2: 更新2:

I forced the statistics to update on the cwObjects table. 我强制统计信息在cwObjects表上更新。 This made a huge difference! 这产生了巨大的差异! The query is almost 10 times faster! 查询速度快了近10倍!

You don't have statistics on some of the columns, hence you get this warning in your execution plan: 您没有关于某些列的统计信息,因此您在执行计划中收到此警告:

Columns With No Statistics: [compareware].[dbo].[CwObject].CwObject_EntityType; 没有统计数据的列:[compareware]。[dbo]。[CwObject] .CwObject_EntityType; [compareware].[dbo].[CwObject].CwObject_Predecessor_Guid [compareware] [DBO]。[CwObject] .CwObject_Predecessor_Guid

This results in an Actual Number of rows of 174480 while the estimated number of rows was 1779,2. 这导致实际行数为174480,而估计行数为1779,2。

Try activating the auto create statistics on the database, or create statistics on those columns manually. 尝试激活数据库上的auto create statistics ,或手动创建这些列的统计信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM