Skip to content

Avoid parameter sniffing for exists subquery#309

Merged
ahmad-moussawi merged 3 commits intosqlkata:masterfrom
yoosoopov:patch-1
Oct 22, 2019
Merged

Avoid parameter sniffing for exists subquery#309
ahmad-moussawi merged 3 commits intosqlkata:masterfrom
yoosoopov:patch-1

Conversation

@yoosoopov
Copy link
Copy Markdown
Contributor

parameterized top (@p0) in subquery leeds to performance issue

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

Do you have any reference/benchmarks for that?

@yoosoopov
Copy link
Copy Markdown
Contributor Author

Do you have any reference/benchmarks for that?

image

I have a 1:N relationship table, parameterized exists subquery leeds to full scan of referenced table

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

I appreciate if you can post some reproducible code

@yoosoopov
Copy link
Copy Markdown
Contributor Author

I appreciate if you can post some reproducible code

/*

-- create table a
select a.[object_id], a.[name] into a from sys.objects as a, sys.objects as b

-- create table b
select a.[object_id] into b from sys.objects as a

-- create indexes 
create index ix_a_0 on a([object_id])
create index ix_b_0 on b([object_id])

*/

set statistics io on

declare @top as int

set @top = 1

select top 100 * from a where 
    exists (select top (1) 1 from b where b.[object_id] = 4 and a.[object_id] = b.[object_id])

select top 100 * from a where 
   exists (select top (@top) 1 from b where b.[object_id] = 4 and a.[object_id] = b.[object_id])

(0 rows affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'a'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(0 rows affected)
Table 'b'. Scan count 2972176, logical reads 5954696, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'a'. Scan count 1, logical reads 30564, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

@yoosoopov
Copy link
Copy Markdown
Contributor Author

select 1 is equal to select top(1) 1 in performance

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

Thanks a lot

@ahmad-moussawi ahmad-moussawi merged commit d098a4e into sqlkata:master Oct 22, 2019
@regme
Copy link
Copy Markdown

regme commented Oct 22, 2019

Seems like there is some problem with CI build for NuGet? 😭
How can we get an updated package?

@yoosoopov yoosoopov deleted the patch-1 branch October 23, 2019 06:31
@ahmad-moussawi
Copy link
Copy Markdown
Contributor

@regme it seems the build is ok, and the NuGet packages are published as excepted, are you still facing this issue?

@regme
Copy link
Copy Markdown

regme commented Oct 23, 2019

@regme it seems the build is ok, and the NuGet packages are published as excepted, are you still facing this issue?

I was expecting to find an updated version of the NuGet package with changes from PR #309
But the latest available version of the package is 1.1.8-ci-712 built on Wed, 07 Aug 2019

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

Nightly build packages are hosted on MyGet, https://www.myget.org/feed/sqlkata/package/nuget/SqlKata.Execution/1.1.8-0-717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants