Skip to content

Fix bad indentation in code snippet #205

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

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 60 additions & 60 deletions docs/performance/inserts/sequences.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,66 +131,66 @@ ID for the record we are inserting into the ``mytable`` table.

.. code:: python

# /// script
# requires-python = ">=3.8"
# dependencies = [
# "records",
# "sqlalchemy-cratedb",
# ]
# ///

import time

import records

db = records.Database("crate://")
sequence_name = "mysequence"

max_retries = 5
base_delay = 0.1 # 100 milliseconds

for attempt in range(max_retries):
select_query = """
SELECT last_value,
_seq_no,
_primary_term
FROM sequences
WHERE name = :sequence_name;
"""
row = db.query(select_query, sequence_name=sequence_name).first()
new_value = row.last_value + 1

update_query = """
UPDATE sequences
SET last_value = :new_value
WHERE name = :sequence_name
AND _seq_no = :seq_no
AND _primary_term = :primary_term
RETURNING last_value;
"""
if (
str(
db.query(
update_query,
new_value=new_value,
sequence_name=sequence_name,
seq_no=row._seq_no,
primary_term=row._primary_term,
).all()
)
!= "[]"
):
break

delay = base_delay * (2**attempt)
print(f"Attempt {attempt + 1} failed. Retrying in {delay:.1f} seconds...")
time.sleep(delay)
else:
raise Exception(f"Failed after {max_retries} retries with exponential backoff")

insert_query = "INSERT INTO mytable (id, field1) VALUES (:id, :field1)"
db.query(insert_query, id=new_value, field1="abc")
db.close()
# /// script
# requires-python = ">=3.8"
# dependencies = [
# "records",
# "sqlalchemy-cratedb",
# ]
# ///

import time

import records

db = records.Database("crate://")
sequence_name = "mysequence"

max_retries = 5
base_delay = 0.1 # 100 milliseconds

for attempt in range(max_retries):
select_query = """
SELECT last_value,
_seq_no,
_primary_term
FROM sequences
WHERE name = :sequence_name;
"""
row = db.query(select_query, sequence_name=sequence_name).first()
new_value = row.last_value + 1

update_query = """
UPDATE sequences
SET last_value = :new_value
WHERE name = :sequence_name
AND _seq_no = :seq_no
AND _primary_term = :primary_term
RETURNING last_value;
"""
if (
str(
db.query(
update_query,
new_value=new_value,
sequence_name=sequence_name,
seq_no=row._seq_no,
primary_term=row._primary_term,
).all()
)
!= "[]"
):
break

delay = base_delay * (2**attempt)
print(f"Attempt {attempt + 1} failed. Retrying in {delay:.1f} seconds...")
time.sleep(delay)
else:
raise Exception(f"Failed after {max_retries} retries with exponential backoff")

insert_query = "INSERT INTO mytable (id, field1) VALUES (:id, :field1)"
db.query(insert_query, id=new_value, field1="abc")
db.close()

.. _extremely fast ingestion speeds: https://cratedb.com/blog/how-we-scaled-ingestion-to-one-million-rows-per-second

Expand Down
2 changes: 1 addition & 1 deletion docs/performance/inserts/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ testing purposes, called something like ``my_table_test``.
Generating fake data
--------------------

IF you don't have data already, you can generate fake data. But do note that
If you don't have data already, you can generate fake data. But do note that
any differences between fake data and real data may produce significant
performance differences.

Expand Down