Skip to content

PERF: Cache full column counts for fetchone - #829

Draft
Jahnvi Thakkar (jahnvi480) wants to merge 3 commits into
mainfrom
jahnvi/candidate-a-fetchone-column-count
Draft

Jahnvi Thakkar (jahnvi480) wants to merge 3 commits into
mainfrom
jahnvi/candidate-a-fetchone-column-count

Conversation

@jahnvi480

@jahnvi480 Jahnvi Thakkar (jahnvi480) commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Work Item / Issue Reference

AB#48364


Summary

Draft Candidate A experiment for evaluation with the existing CI performance report; not ready to merge and no measured speedup claimed.

  • Reuse the full result column count in native FetchOne_wrap within the existing statement metadata generation, instead of querying it after every successful row fetch.
  • Keep full cardinality separate from partial SQLGetData metadata; publish only for the matching generation and reset on existing invalidation.
  • Preserve unbinding, diagnostics, error handling, Python APIs and uncached explicit column-count calls.
  • Add nine subprocess-isolated native scenarios covering reuse, partial metadata, changed shapes, nextset, fetch/count/decode failures, generation changes and mixed fetch APIs.

Both fresh Linux x86_64 CPython 3.13 Release/profiling-OFF builds and imports succeeded. Local correctness execution and timing remain unrun because the required SQL-side FD inspection was permission-denied. The new tests and performance effect still require validation.

The CI report will be assessed per workload, including unchanged-path regressions. Profiling-enabled attribution will not be presented as a measured shipped Release-OFF speedup.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 09:00
@github-actions github-actions Bot added the pr-size: medium Moderate update size label Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

PR Performance Report

✅ No regression detected

No consistent slowdowns detected across all 2 environments.

0 IMPROVEMENTS 0 SLOWDOWNS 2/2 ENVIRONMENTS

Coverage: 2 of 2 environments completed. Advisory result; does not block merging.

Performance diagnostics

Phase times are inclusive diagnostics and must not be added together. They identify where measured time changed, not why it changed.

Unix / SQL Server 2022

Row-by-row fetching: py::fetchone::row_wrap +0.010 ms; ddbc::AppendDiagRecords::SQLGetDiagRec_call +0.002 ms; ddbc::SQLDescribeCol::driver_call +0.000 ms. Call changes: ddbc::SQLNumResultCols_wrap (1000 -> 1 calls).

Unix / SQL Server 2025

Row-by-row fetching: ddbc::AppendDiagRecords::SQLGetDiagRec_call +0.001 ms; ddbc::SQLDescribeCol::driver_call +0.000 ms. Call changes: ddbc::SQLNumResultCols_wrap (1000 -> 1 calls).

All database tasks and timings

Unix / SQL Server 2022

Database task Before After Paired change Result
Connection opening 10.196 ms 10.220 ms +1.5% no signal
SELECT queries 1.058 ms 1.060 ms +0.4% no signal
Row insertion 34.139 ms 34.007 ms -0.2% no signal
Executemany inserts 153.530 ms 156.583 ms +0.8% no signal
Fetch-all queries 120.409 ms 119.649 ms -1.1% no signal
Row-by-row fetching 14.632 ms 14.115 ms -4.6% no signal
Batched row fetching 117.404 ms 121.402 ms +4.1% no signal
Transaction commit and rollback 112.303 ms 112.434 ms -1.3% no signal
Arrow row fetching 94.240 ms 93.747 ms -0.5% no signal
100,000-row insertion 443.957 ms 450.539 ms +1.0% no signal
Row fetching in batches of 100 121.333 ms 121.768 ms +0.1% no signal
Row fetching in batches of 10,000 134.574 ms 123.399 ms -1.0% no signal
Repeated positional queries 33.270 ms 33.322 ms +0.9% no signal
Repeated named-parameter queries 35.563 ms 34.988 ms -2.3% no signal
Legacy 100,000-row insertion 345.946 ms 343.815 ms +0.2% no signal
Insertion with explicit input sizes 478.569 ms 477.239 ms -0.5% no signal
Joined aggregation queries 181.419 ms 210.656 ms +15.2% no signal
Large joined-result fetching 175.055 ms 174.899 ms -0.3% no signal
1.2-million-row fetching 3504.275 ms 3474.137 ms -0.5% no signal
Common table expression queries 5.306 ms 5.327 ms -0.3% no signal
256 KiB VARCHAR(MAX) / fetchall() 1.256 ms 1.297 ms +2.7% no signal

Unix / SQL Server 2025

Database task Before After Paired change Result
Connection opening 97.235 ms 97.525 ms -0.4% no signal
SELECT queries 1.092 ms 1.065 ms -2.4% no signal
Row insertion 34.792 ms 34.661 ms -0.1% no signal
Executemany inserts 150.453 ms 150.595 ms -0.4% no signal
Fetch-all queries 123.431 ms 120.954 ms -1.9% no signal
Row-by-row fetching 14.902 ms 14.148 ms -4.7% no signal
Batched row fetching 119.425 ms 116.390 ms -2.1% no signal
Transaction commit and rollback 116.174 ms 114.975 ms -0.8% no signal
Arrow row fetching 92.821 ms 93.974 ms -0.4% no signal
100,000-row insertion 439.389 ms 438.041 ms -0.3% no signal
Row fetching in batches of 100 121.457 ms 123.372 ms +2.0% no signal
Row fetching in batches of 10,000 159.062 ms 134.043 ms -15.7% no signal
Repeated positional queries 33.523 ms 33.444 ms -0.1% no signal
Repeated named-parameter queries 36.580 ms 36.713 ms -0.2% no signal
Legacy 100,000-row insertion 356.011 ms 351.167 ms -1.4% no signal
Insertion with explicit input sizes 480.166 ms 475.570 ms -0.3% no signal
Joined aggregation queries 159.875 ms 160.934 ms +1.0% no signal
Large joined-result fetching 180.557 ms 180.661 ms -0.7% no signal
1.2-million-row fetching 3564.969 ms 3536.745 ms -1.0% no signal
Common table expression queries 5.110 ms 5.117 ms +0.2% no signal
256 KiB VARCHAR(MAX) / fetchall() 1.475 ms 1.536 ms -0.3% no signal
Build and measurement details

ADO build 179702

PR head: 6fdfe893a065fcff60580288c16aacc696d4c787
Base: c5831908977f0b8b355fda6f71d86629855d46aa
Measured merge: 721171d829c96df021322d61150e3e4be3be880e

  • Unix / SQL Server 2022: Python 3.12.3, x86_64, SQL 16.0.4295.3; 5 paired comparisons and 1 warmup.
  • Unix / SQL Server 2025: Python 3.12.3, x86_64, SQL 17.0.5005.3; 5 paired comparisons and 1 warmup.

A consistent change requires more than 20% median paired movement, at least 1 ms between the median runtimes, and at least 80% of pairs exceeding the relative threshold in the same direction. A slowdown without enough pair agreement is reported as inconsistent.

The displayed change is the median of paired before-and-after ratios. It is not recalculated from the two displayed median runtimes.

Both revisions use profiling-enabled builds on the same agent and database, with alternating order and discarded warmups. Results are diagnostic and do not represent production-wheel latency.

Raw samples and logs are attached to the ADO run as profiler-* artifacts.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The native hot-path change still requires live correctness and performance validation, as acknowledged by the draft description.

Review effort: Balanced
Findings: None

What changed in this PR

Introduces an experimental native cache to avoid repeated column-count queries during fetchone.

Changes:

  • Caches full column counts by metadata generation.
  • Reuses cached counts while preserving invalidation and error handling.
  • Adds nine subprocess-isolated integration scenarios.
File Description
mssql_python/​pybind/​ddbc_bindings.cpp Uses the cached count in FetchOne_wrap.
mssql_python/​pybind/​result_metadata.hpp Stores and invalidates full column counts.
tests/​test_fetch_settings_cache.py Tests reuse, invalidation, failures, and mixed fetching.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Use a separate connection for the cross-handle assertion without requiring MARS. Preserve all fetch and native call-count assertions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 10:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The native hot-path change remains an explicitly unvalidated draft with correctness tests and performance measurements still pending.

Review effort: Balanced
Findings: None

Copilot AI balanced review requested due to automatic review settings October 1, 2026 10:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The native cache behavior and performance impact remain unvalidated by runtime execution.

Review effort: Balanced
Findings: None

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

84%


📈 Total Lines Covered: 9426 out of 11097
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/pybind/result_metadata.hpp (100%)

Summary

  • Total: 8 lines
  • Missing: 0 lines
  • Coverage: 100%

📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.performance_counter.hpp: 0.7%
mssql_python.pybind.logger_bridge.cpp: 57.9%
mssql_python.pybind.ddbc_bindings.h: 62.6%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 79.5%
mssql_python.pybind.connection.connection_pool.cpp: 82.3%
mssql_python.pybind.connection.connection.cpp: 83.1%
mssql_python.logging.py: 86.2%
mssql_python.pooling.py: 90.1%
mssql_python.pybind.fetch_temporal.hpp: 92.1%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants