Skip to content

Commit fc9f61f

Browse files
authored
Python/SQLAlchemy: Fix multirow/batched exercise (#1189)
Large inserts without using batch are source of OOMemory, so CrateDB introduced a setting (configurable) to have a protective limit. In this spirit, large inserts using the "multirow" strategy no longer work, so accompany the test harness to only use them with "batched". In this case, large is ~50_000 records, while ~25_000 records of the shape at hand still work with traditional "multirow" inserts.
1 parent be88b9c commit fc9f61f

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

by-language/python-sqlalchemy/insert_efficient.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353

5454
import sqlalchemy as sa
5555

56-
# INSERT_RECORDS = 1275
57-
INSERT_RECORDS = 50_000
58-
# INSERT_RECORDS = 2_750_000
5956

6057
BATCHED_PAGE_SIZE = 20_000
6158

@@ -88,7 +85,7 @@ def insert_batched(engine, table, records):
8885
conn.execute(insertable, parameters=records)
8986

9087

91-
def run_example(dburi: str, variant: str):
88+
def run_example(dburi: str, variant: str, record_count: int):
9289
metadata = sa.MetaData()
9390
table = sa.Table(
9491
"testdrive",
@@ -98,7 +95,7 @@ def run_example(dburi: str, variant: str):
9895
)
9996

10097
# Create 275 test records.
101-
records = [{"id": i, "name": f"foo_{i}"} for i in range(1, INSERT_RECORDS + 1)]
98+
records = [{"id": i, "name": f"foo_{i}"} for i in range(1, record_count + 1)]
10299

103100
# Run multi-row insert, with a specified batch-/page-size.
104101
engine = sa.create_engine(dburi, insertmanyvalues_page_size=BATCHED_PAGE_SIZE, echo=True)
@@ -119,7 +116,7 @@ def run_example(dburi: str, variant: str):
119116
print("Number of records:", result.scalar_one())
120117

121118

122-
def run_database(database: str, variant: str):
119+
def run_database(database: str, variant: str, record_count: int):
123120
if database == "sqlite":
124121
dburi = "sqlite:///:memory:"
125122
elif database == "postgresql":
@@ -129,10 +126,17 @@ def run_database(database: str, variant: str):
129126
else:
130127
raise ValueError(f"Unknown database: {database}")
131128

132-
run_example(dburi, variant)
129+
run_example(dburi, variant, record_count)
133130

134131

135132
if __name__ == "__main__":
133+
"""
134+
Invocation examples:
135+
136+
python insert_efficient.py cratedb multirow 25000
137+
python insert_efficient.py cratedb batched 50000
138+
"""
136139
database = sys.argv[1]
137140
variant = sys.argv[2]
138-
run_database(database, variant)
141+
record_count = int(sys.argv[3])
142+
run_database(database, variant, record_count)

by-language/python-sqlalchemy/test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ def run(command: str):
88

99

1010
def test_insert_efficient_multirow():
11-
cmd = "time python insert_efficient.py cratedb multirow"
11+
insert_records = 25_000
12+
cmd = f"time python insert_efficient.py cratedb multirow {insert_records}"
1213
run(cmd)
1314

1415

1516
def test_insert_efficient_batched():
16-
cmd = "time python insert_efficient.py cratedb batched"
17+
insert_records = 50_000
18+
cmd = f"time python insert_efficient.py cratedb batched {insert_records}"
1719
run(cmd)
1820

1921

2022
def test_insert_efficient_unknown(capfd):
21-
cmd = "time python insert_efficient.py cratedb unknown"
23+
cmd = "time python insert_efficient.py cratedb unknown 1000"
2224
with pytest.raises(subprocess.CalledProcessError):
2325
run(cmd)
2426
out, err = capfd.readouterr()

0 commit comments

Comments
 (0)