Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadStreamForwards times out for long streams [MS SqlServer] #533

Open
yreynhout opened this issue Sep 10, 2021 · 0 comments
Open

ReadStreamForwards times out for long streams [MS SqlServer] #533

yreynhout opened this issue Sep 10, 2021 · 0 comments
Assignees

Comments

@yreynhout
Copy link
Member

yreynhout commented Sep 10, 2021

Today a former client brought to my attention that they were observing timeouts while reading a long stream using the ReadStreamForwards method. They were using a reasonable page size so the culprit was not trying to read with a page size of Int32.MaxValue. The query execution plan revealed the following:

image

A key lookup was performed which cost 99% of the time to perform the query - that didn't look right. It appears that all event rows in that particular stream were being read (1.8 million in this particular case - even if you frown upon that size, it may hit you at a lower number too) as part of the key lookup, explaining the time it took and thus the command timeout which kicked in as a consequence. The reason this happens is because it's missing an index on StreamIdInternal in the Messages table. Adding this

CREATE NONCLUSTERED INDEX [IX_Messages_StreamIdInternal] ON dbo.Messages ([StreamIdInternal])

will fix the slowness of this particular query:

     SELECT TOP(@count)
            dbo.Messages.StreamVersion,
            dbo.Messages.Position,
            dbo.Messages.Id,
            dbo.Messages.Created,
            dbo.Messages.[Type],
            dbo.Messages.JsonMetadata,
            dbo.Messages.JsonData
       FROM dbo.Messages
      WHERE dbo.Messages.StreamIdInternal = @streamIdInternal AND dbo.Messages.Position >= @position
   ORDER BY dbo.Messages.Position;

Courtesy of @ArneD and @koenmetsu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant