我有一个代码,可以在普通PC上在大约15秒内返回大约100万条记录。
select joined.* , eset.PageNo from (select * from
(
select e.EntityTypeID , a.EntityID , a.AttributeValue , ea.AttributeName
from
(SELECT T1.EntityID,
T1.AttributeID,
STUFF(( SELECT ', ' + T22.OptionText
FROM [POC].[dbo].[EntityAttributeOptionValues] t2
inner join EntityAttributeOptions t22 on
t22.ID = t2.OptionValueID
WHERE T2.AttributeID = T1.AttributeID
AND T2.EntityID = T1.EntityID
-- ANd t2.EntityID between 5060 and 56598
FOR XML PATH('')
), 1, 2, '') [AttributeValue]
FROM [POC].[dbo].[EntityAttributeOptionValues] t1
inner join EntityAttributeOptions t11 on
t11.ID = t1.OptionValueID
--where t1.EntityID between 5060 and 56598
GROUP BY T1.EntityID, T1.AttributeID
) a
inner join Entities e on e.ID = a.EntityID
inner join EntityAttributes ea on ea.ID = a.AttributeID
--where e.EntityTypeID = 9
) options
union all
(select EntityAttributes.EntityTypeID, EntityAttributeValues.EntityID as 'EntityID' , EntityAttributeValues.AttributeValue,EntityAttributes.attributeName from EntityAttributeValues
inner join EntityAttributes on EntityAttributes.ID = EntityAttributeValues.AttributeID
)) joined
inner join Entity_Paged_View_With_Set eset on eset.ID = joined.EntityID
此查询将返回名为EntityTypeID, EntityID, AttributeValue, AttributeName, PageNo
的五列。
EntityTypeID EntityID AttributeValue AttributeName PageNo
1 5790 Musics, Paintings Hobbies 286
1 11673 Musics, Paintings Hobbies 580
1 30595 Males Gender 1527
1 31860 No Want Bus Facility 1590
1 39755 Males Gender 1985
1 41020 No Want Bus Facility 2048
1 47599 Males Gender 2377
1 48864 No Want Bus Facility 2440
现在,当我尝试大约100行时,大约需要3秒钟。我想按照
过滤此查询pageNo = 1 and EntityTypeID = 1
or
pageNo = 1 and EntityTypeID = 3
or
pageNo = 5632 and EntityTypeID = 1
等等。
请您说明我应该在哪里申请条件。即使是order by子句也会大大降低性能。
union all之前的上部查询很慢。但后者很快。
我应该在哪里应用优化查询的条件,以便执行
只需不到一秒钟