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

ttl: improve row deletion performance with secondary indexes #82140

Open
otan opened this issue May 31, 2022 · 3 comments
Open

ttl: improve row deletion performance with secondary indexes #82140

otan opened this issue May 31, 2022 · 3 comments
Labels
A-row-level-ttl C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-todo sync-me-8 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@otan
Copy link
Contributor

otan commented May 31, 2022

Currently, a row-level TTL table with secondary indexes may be slower to delete, as it requires access to multiple ranges to perform the deletion.

We can improve this based on this section of the RFC:

We can improve deletion speed on secondary indexes if we split the deletes for secondary indexes and the primary index. This way, we have a lot more flexibility in terms of which writes we batch.

What this allows us to do is throw thousands of rows in a big buffer, decompose their individual writes, bucket them by range, and send a batch per range. This has two benefits, both of which are very significant:

  • we can easily build up large batches of 100s of writes for each range, which will stay batched all the way through Raft and down to Pebble.
  • these batches don’t run a distributed transaction protocol, so they hit the 1PC fast path instead of writing intents, running a 2-phase commit protocol, then cleaning up those intents.
    This is predicated on filtering out expired rows working as otherwise users could miss entries when querying the secondary index as opposed to the primary.

Jira issue: CRDB-16224

@otan otan added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-todo A-row-level-ttl labels May 31, 2022
@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 31, 2022
@otan
Copy link
Contributor Author

otan commented May 31, 2022

cc @rmloveland it may be worth documenting on the TTL docs that tables with multiple indexes may benefit from a lower delete_batch_size, as secondary indexes are on multiple ranges and a larger batch size means potentially more ranges being touched which potentially impacts cluster-wide latency. let me know if you need clarification!

@rmloveland
Copy link
Collaborator

Thanks Oliver, added this info as a comment on cockroachdb/docs#13703 to make sure it's captured with some other issues @vy-ton wrote up!

@nvanbenschoten
Copy link
Member

nvanbenschoten commented Jun 6, 2022

#79134 is starting to ask the question of what needs to change during SQL planning and execution if referential integrity is not enforced. It turns out, not much. There are a few SQL optimizations that need to be disabled, mostly related to limit pushdown across table and index joins. We also needed to disable SKIP LOCKED on tables with column families, but that might not be relevant here if we can enforce per-range consistency (a row's column families will never span ranges).

Those findings may be relevant here because it means that we don't need to push filtering of expired rows all the way down to individual index scans (which in turn requires all secondary indexes to store the expiration). Instead, we can lift this filtering higher up in the query plan. Or forgoe it entirely if we're ok with not promising that two separate queries that touch different indexes see consistent results.

craig bot pushed a commit that referenced this issue Jan 26, 2024
118308: roachtest: fix lease_preferences configure zone r=shralex a=kvoli

e2a5b35 updated the `lease_prefernces` roachtest to use `configureZone`
but incorrectly specified just "kv" as the zone target. Update the zone
target to correctly include the database qualifier.

Fixes: #118304
Informs: #117311
Release note: None

118318: ttljob: add hint to use PK in delete query r=rafiss a=rafiss

This will help avoid choosing a plan that scans a secondary index, which can lead to many KV rows being scanned and also lead to contention.

informs #82140

Release note (bug fix): Fixed a bug that could cause DELETE queries sent by the row-level TTL job to use a secondary index rather than the primary index to find the rows to delete. This could lead to some DELETE operations taking a much longer time than they should. This bug was present since v22.2.0.

Co-authored-by: Austen McClernon <austen@cockroachlabs.com>
Co-authored-by: Rafi Shamim <rafi@cockroachlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-row-level-ttl C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) docs-todo sync-me-8 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

No branches or pull requests

4 participants