cursor.execute() with many bound parameters (~2000) is ~14x slower than pyodbc
メンテナーはふだん 2 日以内に返信
@bewithgaurav がすでに取り組んでいます。
2026年4月9日 から。
評価
この issue はまだ評価されていません。
説明
Describe the bug
When executing a multi-row INSERT with ~2000 bound parameters (the kind SQLAlchemy's insertmanyvalues generates), cursor.execute() is about 14x slower than the equivalent call through pyodbc.
cursor.executemany() is much closer between drivers (~1.6x), so the issue is specific to single cursor.execute() calls with large parameter counts.
To reproduce
import time
import mssql_python
import pyodbc
N = 100_000
COLS = 2
# ~1049 rows per INSERT, ~2098 params per call, ~95 calls total
ROWS_PER_BATCH = 2099 // COLS
SQL = "INSERT INTO bench (id, name) VALUES " + ",".join(
[f"(?, ?)"] * ROWS_PER_BATCH
)
PARAMS = [None] * (ROWS_PER_BATCH * COLS)
# --- mssql-python ---
conn = mssql_python.connect(
"SERVER=localhost;DATABASE=master;UID=sa;PWD=YourPassword;"
"Encrypt=yes;TrustServerCertificate=yes;"
)
cursor = conn.cursor()
cursor.execute(
"IF OBJECT_ID('bench') IS NOT NULL DROP TABLE bench;"
"CREATE TABLE bench (id INT, name VARCHAR(50))"
)
conn.commit()
t0 = time.perf_counter()
for _ in range(N // ROWS_PER_BATCH):
cursor.execute(SQL, PARAMS)
conn.commit()
ms_time = time.perf_counter() - t0
cursor.close()
conn.close()
# --- pyodbc ---
conn2 = pyodbc.connect(
"DRIVER={ODBC Driver 18 for SQL Server};"
"SERVER=localhost;DATABASE=master;UID=sa;PWD=YourPassword;"
"Encrypt=yes;TrustServerCertificate=yes;"
)
cursor2 = conn2.cursor()
cursor2.execute("TRUNCATE TABLE bench")
conn2.commit()
t0 = time.perf_counter()
for _ in range(N // ROWS_PER_BATCH):
cursor2.execute(SQL, PARAMS)
conn2.commit()
py_time = time.perf_counter() - t0
cursor2.close()
conn2.close()
print(f"mssql-python: {ms_time:.2f}s ({N / ms_time:,.0f} rows/s)")
print(f"pyodbc: {py_time:.2f}s ({N / py_time:,.0f} rows/s)")
Output
mssql-python: 11.87s (8,427 rows/s)
pyodbc: 0.77s (130,000 rows/s)
Context
This came up while working on SQLAlchemy integration. SA 2.x uses insertmanyvalues by default, which generates batched multi-row INSERTs with ~2098 parameters per call (limited by SQL Server's 2100 parameter cap).
For reference, cursor.executemany() is much faster on this driver (~50K rows/s) and cursor.bulkcopy() is excellent (~390K rows/s). It's really just the many-parameter cursor.execute() path that's slow.
Environment
- mssql-python: 1.4.0
- pyodbc: 5.2.0
- SQL Server: 2022
- Python: 3.12
- OS: Ubuntu 24.04
- 主要言語
- Python
- スター
- 478
- フォーク
- 62
- 平均マージ
- 3日 19分
- マージ済み PR(30日)
- 28
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/mssql-python のほかの issue
-
area: bulk-copy triage needed
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
microsoft/mssql-python#843 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
triage needed
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
microsoft/mssql-python#846 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
macOS: encrypted connections fail after Homebrew moved the `openssl` alias to `openssl@4`対応中かも @bewithgaurav が 15 日前に担当しました。 オープンarea: packaging-platform bug triage needed
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
microsoft/mssql-python#826 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
-
Announcement: mssql-python is coming to Django through mssql-django 2.0再び着手できるかも @bewithgaurav が 42 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンannouncement
microsoft/mssql-python#748 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
-
Connection pool can briefly exceed max_size when pooling is disabled under load対応中かも @bewithgaurav が 24 日前に担当しました。 オープンarea: connectivity-auth bug triage done under development
難易度 4/5 3〜5日 初心者へのやさしさ 62/100
microsoft/mssql-python#746 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 2 日以内に返信
microsoft/mssql-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
aws-samples/sample-per-model-bedrock#243 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
FuRongJun-1999/dsh-memory#404 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
bug frontend
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
PennyLaneAI/catalyst#3337 ·
メンテナーはふだん 1 日以内に返信
-
Workflow.schedule stops when a cron deadline passes before sleep対応中かも @HuaTNA が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信