GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve MsSql append performance by yreynhout · Pull Request #117 · SQLStreamStore/SQLStreamStore

This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Conversation

@yreynhout

@yreynhout yreynhout commented Apr 12, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

While appending a very large number - in reasonable batches - of events (in our case close to 2 million), to a stream, we noticed a performance degradation over time that manifested itself both on Windows (we used Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)) and Linux (we used Microsoft SQL Server 2017 (RTM-CU5) (KB4092643) - 14.0.3023.8 (X64)). Intrigued we started profiling the SQL statements using SQL Profiler. We noticed that the number of reads performed by SQL Server upon each append started to grow the more events were appended. The query execution plan revealed that some of the SQL statements during the append operation were performing Index Scans instead of Index Seeks.

For AppendStreamExpectedVersion.sql we

  • replaced the query for the latest stream position by a SCOPE_IDENTITY() function call yielding the same result but a lot faster, thereby eliminating the use of an index,
  • replaced the query for the latest stream version by a computation (which could also be passed in as a parameter - just didn't feel like changing the calling C# code at this point) based on the new stream messages table parameter, thereby eliminating the use of an index,
  • replaced the query for the latest json data of the metadata stream (if such a stream exists) by a query that uses the latest position of the metadata stream, thereby replacing an index scan by index seek (since position is PK of the messages table).

The first two changes, which happen within the transaction, do not have a different outcome than the previous version of these SQL statements. The latter change, outside of the transaction, switched from using the sort and top clause to an exact position read which could potentially give you a different result. I doubt this will be a problem since there's no transaction in place to give you any guarantee on the outcome of that query (other than "we think this is the latest message in the metadata stream"). Ultimately, it's being used to feed the code with the maxcount of the stream we're appending to.

Timing our own code we saw the following improvement (appending 2 million events to 1 stream in batches of a 1000 events / batch):

  • Before: 2.068.175ms
  • After: 292.974ms

I performed similar changes for the AppendStreamExpectedVersionAny.sql and AppendStreamExpectedVersionNoStream.sql.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CumpsD and ilyapalkin reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@yreynhout yreynhout changed the title [WIP] Improve MsSql append performance Improve MsSql append performance Apr 28, 2018
yreynhout added 4 commits April 28, 2018 15:59
@yreynhout
Replaced query for latest stream position by SCOPE_IDENTITY. Replaced…
5403f11
… query for latest stream version by computation based on new stream messages. Simplified metadata stream query (now uses index seek instead of scan).
@yreynhout
Applied similar changes to ExpectedAny and ExpectedNoStream sql scrip…
0ac04cc
…ts. Applied locks to ExpectedVersion script.
@yreynhout
Fixed an oversight while copy / pasting: when no stream the latest st…
d7399b7
…ream version logic can be simplified.
@yreynhout
Injected the fact there are messages or not as a parameter and made t…
048efb2
…he rest of the logic conditional based on that fact (applies to ExpectedVersion.Any/NoStream only).
@yreynhout
yreynhout force-pushed the improve-mssql-append branch from 6733244 to 048efb2 Compare April 28, 2018 14:03
@damianh damianh added the enhancement label May 2, 2018
@damianh damianh self-assigned this May 2, 2018
@damianh damianh added this to the v1.1.2 milestone May 2, 2018
@damianh
damianh merged commit f7c3045 into SQLStreamStore:master May 2, 2018
@damianh

damianh commented May 2, 2018

Copy link
Copy Markdown
Member

Merged, cheers @yreynhout !

Yeah the duplicate index can be addressed with PR for v2.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yreynhout

yreynhout commented May 8, 2018

Copy link
Copy Markdown
Contributor Author

@damianh I reran my tests this afternoon and getting numbers like 312.696ms and 424.045ms which seem to be in the lower (expected) range.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yreynhout
yreynhout commented May 8, 2018
View reviewed changes
Comment thread src/SqlStreamStore.MsSql/MsSqlScripts/AppendStreamExpectedVersionAny.sql

SELECT @latestStreamVersion = MAX(StreamVersion) + @latestStreamVersion + 1
FROM @newMessages
SET @latestStreamVersion = @latestStreamVersion

@yreynhout yreynhout May 8, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can safely be removed - PR inbound.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@damianh

damianh commented Aug 25, 2018

Copy link
Copy Markdown
Member

@yreynhout Could you add your test to the LoadTest project? I know it won't be the same but good to get some more scenarios in there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@damianh damianh

Labels

enhancement

Projects

None yet

Milestone

v1.1.2

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@yreynhout @damianh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.