Bulk copy for SQLAlchemy + mssql-python — 10-50x faster writes via SQL Server's native TDS protocol
Project description
sqlalchemy-mssql-bulkcopy
Bulk copy for SQLAlchemy + mssql-python — 10–50× faster writes via SQL Server's native TDS bulk load protocol.
All cursor.bulkcopy() kwargs pass through to the driver. When
mssql-python adds new options, this package supports them automatically.
Based on the event hook approach recommended by SQLAlchemy's maintainer.
Install
The mssql+mssqlpython dialect requires SQLAlchemy ≥ 2.1.0b2,
which is not yet on PyPI. Install SA from git until then:
pip install "sqlalchemy @ git+https://github.com/sqlalchemy/sqlalchemy.git@main"
pip install sqlalchemy-mssql-bulkcopy
Once SA 2.1 is stable:
pip install sqlalchemy-mssql-bulkcopy
Requires Python ≥ 3.10, SQLAlchemy ≥ 2.1, mssql-python ≥ 1.4.
Usage
Direct (recommended)
No engine configuration needed. Returns the driver result dict.
from sqlalchemy import create_engine
from sqlalchemy_mssql_bulkcopy import bulkcopy
engine = create_engine("mssql+mssqlpython://user:pass@host/db")
with engine.begin() as conn:
result = bulkcopy(conn, df, "dbo.users", batch_size=50_000)
print(f"{result['rows_copied']} rows in {result['elapsed_time']:.2f}s")
Accepts DataFrames (columns auto-detected), lists of tuples, generators, or any iterable:
# All driver options pass through as kwargs
result = bulkcopy(
conn, df, "staging.events",
batch_size=100_000,
table_lock=True,
keep_identity=True,
check_constraints=True,
fire_triggers=True,
keep_nulls=True,
timeout=300,
use_internal_transaction=True,
)
Event hook (SA Core / ORM)
Hooks into SQLAlchemy's INSERT workflow. Per-statement opt-in via execution options:
from sqlalchemy_mssql_bulkcopy import register_bulkcopy
engine = create_engine(
"mssql+mssqlpython://user:pass@host/db",
use_insertmanyvalues=False, # required — see note below
)
register_bulkcopy(engine, batch_size=10_000, table_lock=True)
with engine.begin() as conn:
conn.execute(
users.insert().execution_options(use_bulkcopy=True),
rows,
)
Per-statement overrides use the bulkcopy_ prefix:
stmt = users.insert().execution_options(
use_bulkcopy=True,
bulkcopy_batch_size=100_000,
bulkcopy_fire_triggers=True,
)
Note:
register_bulkcopy()raisesValueErrorifuse_insertmanyvaluesis not disabled. SA 2.x defaults to multi-rowINSERT … VALUESbatching which bypassesexecutemany()entirely — the event hook never fires without this flag. This is a global engine setting: non-bulkcopy inserts will useexecutemany()instead, which is slightly slower for medium-sized batches. If that matters, usebulkcopy()directly instead.
Pandas
from sqlalchemy_mssql_bulkcopy import bulkcopy_insert_method
df.to_sql("users", engine, method=bulkcopy_insert_method,
if_exists="append", index=False)
Confirming bulkcopy was used
# Direct — return value
result = bulkcopy(conn, df, "dbo.users")
assert result["rows_copied"] == len(df)
# Event hook — on_complete callback
results = []
register_bulkcopy(engine, on_complete=lambda t, r: results.append(r))
Both approaches log at INFO level:
bulkcopy: 1000 rows -> dbo.users (0.050s)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_mssql_bulkcopy-0.1.0.tar.gz.
File metadata
- Download URL: sqlalchemy_mssql_bulkcopy-0.1.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2e8bc630343a2e70135b6f2b5590da37140cd38d0491721b9479eada766fa4a
|
|
| MD5 |
c487847f202c81804ef0f7a7ecdd262e
|
|
| BLAKE2b-256 |
dd4f91d7d465b2f001ffea231bf476dd1892d54870f2284db99822d040edebfa
|
Provenance
The following attestation bundles were made for sqlalchemy_mssql_bulkcopy-0.1.0.tar.gz:
Publisher:
publish.yml on dxdc/sqlalchemy-mssql-bulkcopy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlalchemy_mssql_bulkcopy-0.1.0.tar.gz -
Subject digest:
d2e8bc630343a2e70135b6f2b5590da37140cd38d0491721b9479eada766fa4a - Sigstore transparency entry: 1245218683
- Sigstore integration time:
-
Permalink:
dxdc/sqlalchemy-mssql-bulkcopy@f98632b57849ce8e390e964176804de860b0047f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dxdc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f98632b57849ce8e390e964176804de860b0047f -
Trigger Event:
push
-
Statement type:
File details
Details for the file sqlalchemy_mssql_bulkcopy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_mssql_bulkcopy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0955fc4e5f83a71548d6cbd3a82c90b99a23a68b9dc765945c80a84f77e0803d
|
|
| MD5 |
68167ac19c81e4148036c83a0605df7d
|
|
| BLAKE2b-256 |
96ac937008628ffb45c09509b932be3a57057b0c8653b624da8f974126defb73
|
Provenance
The following attestation bundles were made for sqlalchemy_mssql_bulkcopy-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on dxdc/sqlalchemy-mssql-bulkcopy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlalchemy_mssql_bulkcopy-0.1.0-py3-none-any.whl -
Subject digest:
0955fc4e5f83a71548d6cbd3a82c90b99a23a68b9dc765945c80a84f77e0803d - Sigstore transparency entry: 1245218700
- Sigstore integration time:
-
Permalink:
dxdc/sqlalchemy-mssql-bulkcopy@f98632b57849ce8e390e964176804de860b0047f -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dxdc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f98632b57849ce8e390e964176804de860b0047f -
Trigger Event:
push
-
Statement type: