Skip to content

perf(bq_driver): optimize data fetch and TTFB for JSON datatype and large result sets - #1714

Open
Anshu6250 wants to merge 5 commits into
googleapis:mainfrom
Anshu6250:perf_improve
Open

Anshu6250 wants to merge 5 commits into
googleapis:mainfrom
Anshu6250:perf_improve

Conversation

@Anshu6250

@Anshu6250 Anshu6250 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

This PR optimizes data fetch performance not only for the JSON datatype, but also across other BigQuery data types and large result sets by introducing support for the HTAPI_ActivationThreshold connection/DSN property, caching bound descriptors for SQLBindCol, and zero-copy JSON parsing with the High-Throughput API (Storage Read API).

NOTE:

  • Wide Table Verification (1,000 Columns): Verified fetching a 1,000-column table (bigquery-devtools-drivers.kirltest.table_1000cols_1millionrows) at 10k, 100k, and 1M rows. Overall fetch speed remains comparable to main (575,520 ms total vs 563,358 ms on main: 10k 20,647 ms vs 21,260 ms, 100k 75,289 ms vs 69,339 ms, 1M 479,574 ms vs 472,755 ms), while 1M TTFB improved from 14,474 ms (main) to 6,608 ms (perf_improve), confirming the max_results(1) gating does not adversely impact wide-column queries.
  • Cross-Datatype Impact: Because large-query HTAPI gating (set_max_results(1)) and bound-descriptor caching apply across all column types, 1M-row queries on other data types also improve substantially over main (e.g., INT64 22,942 ms → 8,036 ms, STRING 22,721 ms → 8,027 ms, NUMERIC 23,627 ms → 9,219 ms, RECORD/STRUCT 20,663 ms → 7,350 ms, ARRAY<STRING> 29,381 ms → 8,665 ms).

Problem

  • Slow Time to First Byte (TTFB) for JSON & Other Large Queries: When fetching large datasets (e.g., 1M rows), queries on main took ~16.2–17.9 seconds (and ~13–25 seconds across other data types) for TTFB because the driver attempted to retrieve initial data pages over the standard BigQuery REST API JSON response buffer before transitioning to HTAPI.
  • Unparsed Activation Threshold: The driver ignored HTAPI_ActivationThreshold (even when explicitly set in connection strings like AllowHtapiForLargeResults=1;HTAPI_ActivationThreshold=0;), preventing queries from transitioning to HTAPI at the intended threshold.
  • Inner-Loop Map Lookups in SQLBindCol: WriteRowset performed repeated ard.HasDescriptorRecord() and ard.GetDescriptorRecord() map lookups for every cell, adding substantial iteration overhead across large queries.
  • Buffer Copying in JSON REST Parsing: ProcessResultSetRows re-inspected column schemas per cell, copied cell strings by value, and copied rows instead of moving them into the result set.

Solution

  1. Support HTAPI_ActivationThreshold:
    • Added htapi_activation_threshold to struct Dsn (defaulting to 3).
    • Parsed HTAPI_ACTIVATIONTHRESHOLD from DSN and connection strings.
  2. Dynamic Query Gating (set_max_results(1)):
    • Added a zero-allocation query limit extraction helper (ExtractQueryLimit) using absl::string_view.
    • When AllowHtapiForLargeResults=1, evaluate the query limit against the block threshold:
      • Large queries (>100k rows) or unbounded queries (SELECT ...): Sets query_request.set_max_results(1). BigQuery immediately returns schema and query metadata without serializing rows into REST JSON buffers, allowing the driver to instantly transition to the Storage Read API (Arrow streaming).
      • Small queries (≤100k rows, e.g., 10k, 100k): Kept on the fast REST path, avoiding the ~3.5-second Storage API session creation overhead.
  3. Connection String Override:
    • Updated SQLDriverConnectInternal to allow connection string parameters to properly populate and override dsn_section.
  4. Pre-computed Column Descriptors (odbc_sql_fetch.cc):
    • Cached bound column descriptors (BoundColInfo) before the row iteration loop in WriteRowset, eliminating millions of per-cell std::map lookups (HasDescriptorRecord/GetDescriptorRecord).
  5. Zero-Copy REST JSON Processing (odbc_internal_commons.cc):
    • Pre-computed column types, pre-reserved vector memory, and utilized std::move when populating result set rows to eliminate deep buffer copies.

Performance Benchmark Results

1. SQLBindCol

Query Size Metric main Branch Existing Driver (Simba) Our Driver (perf_improve)
jsonField 10k TTFB
Iteration
Total Time
1,193 ms
6.0 ms
2,220 ms
1,042 ms
8.7 ms
2,085 ms
1,192 ms
7.0 ms
2,086 ms
jsonField 100k TTFB
Iteration
Total Time
4,529 ms
69.0 ms
4,867 ms
3,874 ms
69.3 ms
4,742 ms
4,617 ms
74.7 ms
5,019 ms
jsonField 1M TTFB
Iteration
Total Time
16,193 ms
2,592 ms
19,080 ms
4,293 ms
5,292 ms
10,423 ms
4,348 ms
2,565 ms
7,260 ms

2. SQLGetData

Query Size Metric main Branch Existing Driver (Simba) Our Driver (perf_improve)
jsonField_SQLGetData 10k TTFB
Iteration
Total Time
1,121 ms
8.3 ms
1,460 ms
1,039 ms
11.3 ms
1,920 ms
1,098 ms
8.3 ms
1,469 ms
jsonField_SQLGetData 100k TTFB
Iteration
Total Time
4,417 ms
81.3 ms
4,842 ms
3,924 ms
87.0 ms
4,911 ms
4,621 ms
89.3 ms
5,047 ms
jsonField_SQLGetData 1M TTFB
Iteration
Total Time
16,249 ms
2,464 ms
19,033 ms
4,197 ms
5,011 ms
10,013 ms
4,082 ms
2,447 ms
6,833 ms

3. Total JSON Benchmark Suite Runtime (6 Tests)

  • main Branch: 51.5 seconds
  • Existing Driver: 34.1 seconds
  • Our Driver (perf_improve): 27.7 seconds (fastest overall)

@Anshu6250 Anshu6250 changed the title test perf(bq_driver): optimize STRUCT, RANGE, JSON, and ARRAY data fetch Nov 19, 2026
@Anshu6250
Anshu6250 force-pushed the perf_improve branch 3 times, most recently from c3d1948 to 5d25a78 Compare November 19, 2026 09:05
@Anshu6250 Anshu6250 changed the title perf(bq_driver): optimize STRUCT, RANGE, JSON, and ARRAY data fetch testing Nov 19, 2026
@Anshu6250
Anshu6250 force-pushed the perf_improve branch 2 times, most recently from 1370873 to 55bdd14 Compare November 24, 2026 05:31
@Anshu6250 Anshu6250 changed the title testing perf(bq_driver): optimize data fetch and TTFB for JSON datatype and large result sets Nov 24, 2026
std::string allow_htapi = dsn_section["ALLOWHTAPIFORLARGERESULTS"];
dsn_.allow_htapi = (allow_htapi == "1");

std::string htapi_activation_threshold =

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.

why do we need this new param? and is it supported by existing driver?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is a standard property supported by the existing driver.

We need it for:

  • Compatibility: Drop-in parity for existing DSNs, connection strings, and BI tools that already use this property.
  • Performance: Keeps small queries on the fast REST path (avoiding the ~3s Storage API session setup overhead) while transitioning large queries (1M+ rows) to HTAPI.

// This array is used to dynamically generate individual benchmark
// configurations to isolate and measure the fetch performance for each specific
// BigQuery data type.
std::vector<std::string> const kAllBqTypes2Columns = {"jsonField"};

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.

why only for json? what about others?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added other datatype as well

: block_size;
int64_t query_limit = ExtractQueryLimit(query_str);
if (query_limit < 0 || query_limit > row_threshold) {
query_request.set_max_results(1);

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.

as discussed last time , why do we need to set this max results ?

@Anshu6250 Anshu6250 Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have updated this specifically to address Sachin's feedback:

  • Performance Justification: Without this, BigQuery serializes megabytes of REST JSON before HTAPI engages, causing TTFB on 1M queries to take ~17.5s. With max_results(1), TTFB drops to ~4.3s (73% faster).
  • Not Hardcoded: It is dynamically gated by HTAPI_ActivationThreshold and query limits. Small queries (≤ 100k rows) do not set this and remain on the fast REST path.
  • Avoids Page-Size Assumptions: Setting 1 ensures BigQuery immediately returns the destination table metadata regardless of column count/size, allowing the driver to cleanly hand off the entire result set to the Storage Read API (Arrow).

}

namespace {
int64_t ExtractQueryLimit(absl::string_view query) {

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.

no unit test cases for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added unit testcase

int64_t row_threshold = (activation_threshold > 0)
? (activation_threshold * block_size)
: block_size;
int64_t query_limit = ExtractQueryLimit(query_str);

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.

what about cases when there's no limit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When there is no LIMIT clause, ExtractQueryLimit returns -1.

The condition (query_limit < 0 || query_limit > row_threshold) explicitly catches this (query_limit < 0) and treats it as an unbounded query, setting max_results(1) so the full result set is streamed directly via the High-Throughput Storage Read API (Arrow) instead of buffering large JSON pages over REST.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants