I've try following SQL and it was slow when I useing declare @NewCityJudge
table and join it, but it was fast when I convert table into real number and join it.
-- input id into @NewCityJudge, only one record
declare @NewCityJudge table(CountryId int)
insert into @NewCityJudge
select CountryId from ....
SELECT TOP (300) *
MyTable as b
join ComponentLanguageIndex as c on c.id = b.[key]
join ComponentCountryTags e on c.ComponentId = e.ComponentId
join @NewCityJudge as d on d.CountryId = e.CountryId -- join @NewCityJudge here
But it faster when using
SELECT TOP (300) *
MyTable as b
join ComponentLanguageIndex as c on c.id = b.[key]
join ComponentCountryTags e on c.ComponentId = e.ComponentId
where CountryId in (39)
The @NewCityJudge
always less 5 records.
The first way takes 5 seconds, The second way takes 500 ms.
Thanks
PS. It was fast when using #NewCityJudge temp table, but I afraid it cause some transaction issue
As opposed to a Join, you could use the following:
SELECT TOP (300) *
MyTable as b
join ComponentLanguageIndex as c on c.id = b.[key]
join ComponentCountryTags e on c.ComponentId = e.ComponentId
where e.CountryId IN (Select CountryId from @NewCityJudge)
Anytime you use TOP (#) on a table with some joins, it is less than ideal. I would also like to understand why TOP (300) ? Is this for testing purposes ? How many records are in MyTable ? If you remove TOP (300), you may find your query time issue resolved.
I solved this problem by using temp table instead of parameter.
Create table #NewCityJudge(CountryId int) insert into #NewCityJudge select CountryId from ....
SELECT TOP (300) * MyTable as b
join ComponentLanguageIndex as c on c.id = b.[key]
join ComponentCountryTags e on c.ComponentId = e.ComponentId
join #NewCityJudge as d on d.CountryId = e.CountryId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.