我在为以下查询获取完整结果集时遇到问题。当我选择不同的前10000个时,我会在几秒钟内得到结果。当我将其取出时,它将持续运行1小时以上。我检查并尝试索引公司reg num + country inc等字段,但没有区别。 Toad建议添加以下索引:
USE [BW_DCF];
GO
CREATE NONCLUSTERED INDEX [idx_Nonclustered_WeccoParty_OverallStatus]
ON [CORE].[WeccoParty]
([OverallStatus])
INCLUDE (
[GtId], [CrmPartyId], [FirstName], [LastName], [LegalName], [CountryInc],
[BusinessClass], [RmFullName], [PbeFullName], [OverallClientStatus],
[OverallRpStatus], [CompanyRegNum]
)
WITH
(
PAD_INDEX = OFF,
FILLFACTOR = 100,
IGNORE_DUP_KEY = OFF,
STATISTICS_NORECOMPUTE = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
DATA_COMPRESSION = NONE
)
ON [BW_DCF_Group1];
GO
select distinct wp1.GtId,
wp1.CrmPartyId,
wp1.LegalName,
wp1.BusinessClass,
wp1.RmFullName,
wp1.PbeFullName,
wp1.OverallClientStatus,
wp1.OverallRpStatus,
wp1.FirstName,
wp1.LastName,
wp1.LegalName,
wp1.CountryInc,
wp1.CompanyRegNum,
wp2.GtId,
wp2.CrmPartyId,
wp2.LegalName,
wp2.BusinessClass,
wp2.RmFullName,
wp2.PbeFullName,
wp2.OverallClientStatus,
wp2.OverallRpStatus,
wp2.FirstName,
wp2.LastName,
wp2.LegalName,
wp2.CountryInc,
wp2.CompanyRegNum
from CORE.WeccoParty wp1
join CORE.WeccoParty wp2 on wp1.CompanyRegNum = wp2.CompanyRegNum
and wp1.CountryInc = wp2.CountryInc
and wp1.GtId <> wp2.GtId
and wp1.OverallStatus = 'Onboarded'
and wp2.OverallStatus = 'Onboarded'
答案 0 :(得分:3)
感谢您添加执行计划。我只能给你一些提示。
从CORE.WeccoParty wp1 内部哈希联接 CORE.WeccoParty wp2 on
wp1.CompanyRegNum = wp2.CompanyRegNum
或嵌套循环联接:
从CORE.WeccoParty wp1 内部循环连接在CORE.WeccoParty wp2上
wp1.CompanyRegNum = wp2.CompanyRegNum
创建索引IX_WeccoParty_1 在核心上。WeccoParty(OverallStatus,CompanyRegNum,CountryInc,GtId)
通过查看执行计划,我打赌这种合并联接可能是罪魁祸首加上在联接数据之前提取数据。
答案 1 :(得分:-4)
查询时间长通常在于溢出的RAM空间或给定的RAM空间不足的数据库。该查询是完全正常的,实际上没有任何性能提升的可能。
作为解决方案,我将为数据库提供更多可用内存。
以我的经验,我看到sql-server缺少RAM空间时会遇到很多问题。 PostgreSQL似乎可以更快地处理它。