prisma / tiberius

TDS 7.2+ (Microsoft SQL Server) driver for Rust
Apache License 2.0
321 stars 119 forks source link

Clarity on DB connections/interactions when using streams #239

Closed yatesco closed 1 year ago

yatesco commented 2 years ago

Hi there - thanks for this!

When I use https://docs.rs/tiberius/latest/tiberius/struct.QueryStream.html#method.into_row_stream, does it use a cursor/fetch items in batches/fetch all results in a single query? I assume it uses a cursor, but I want to check.

When I start my app it bootstraps by replaying all events from the DB. This could be 100Ks, and I definitely don't want to load them all in one go, so I'm just checking my assumptions.

I tried to follow the code but got lost in futures somewhere :-).

Thanks!

pimeys commented 1 year ago

If you just have a QueryStream, your item can either be a set of columns, or it can be a row. When calling the into_row_stream, it skips the column infos and just returns rows. Neither of these streams load anything to the memory, you have to poll the stream to get data out. It takes X amount of bytes from the wire at a time, and fetches more when polling and the cache is completely empty.

Use into_row_stream if you just executed one query and the row data is uniform. If you have multiple queries, you have to match the item and react accordingly if you get a new column info, because the following rows are having differently shaped data.

And the streams must always be iterated completely before querying again. We flush the streams to the end in the next query, and trigger a warning to the logs.

yatesco commented 1 year ago

Thanks - that's exactly what I was looking for.