Commit 01db167
authored
Add ODBC-native row-wise array fetch for fast bulk retrieval (#511)
Customers on high-latency links wait the longest when retrieving many
rows because each row is its own `SQLFetch` round-trip — fetching 1000
rows costs 1000 round-trips. This adds the read-side counterpart of the
existing `CreateAll`/`UpdateAll` batch-write path:
`Query<Record>().All()`/`Range()` (and two-record JOIN tuples) now bind
result columns row-wise directly into the caller's `std::vector<Record>`
storage and pull whole row blocks per `SQLFetchScroll`, collapsing the
round-trips from N to ~`ceil(N/depth)`. Values land in place
(zero-copy); results are byte-identical to the per-row path.
The fast path is transparent and gated — a record qualifies when every
result column is row-bindable (the same `SqlRowBindableColumn` set the
write side uses: primitives, date/time/datetime, numeric, char
fixed-capacity strings, and non-numeric optionals of those) and the
driver supports row-array fetching. Anything else (growable
strings/binary, GUID, variant) transparently falls back to the unchanged
per-row path. Verified against SQLite, SQL Server 2022 and PostgreSQL.
## Changes
- **`SqlStatement::FetchAllRowWise`** — new low-level primitive
mirroring `ExecuteBatchNativeRowWise`: sets `SQL_ATTR_ROW_BIND_TYPE =
sizeof(Record)` + `SQL_ATTR_ROW_ARRAY_SIZE`, grows-and-rebinds the
destination per block, clamps depth to a memory budget, and restores
single-row statement state via a `Finally` guard on every exit
(including exceptions). Nullable columns use an over-allocated
row-strided NULL indicator; optionals are pre-engaged and reset on NULL.
Char fixed strings bind `SQL_C_CHAR` inline with a per-row length/trim
fixup.
- **`SqlConnection::SupportsNativeRowArrayFetch`** and
**`RoundTripsNarrowTextByteExact`** — driver capability checks kept on
the connection. The latter carves out PostgreSQL (whose psqlODBC
transcodes `SQL_C_CHAR` through the client codepage), so records
carrying a fixed-capacity string fall back to the per-row wide path
there rather than risk mangling non-ASCII bytes.
- **`DataMapper` retrieval wiring** — compile-time eligibility
(`CanRowWiseFetchRecord`/`CanRowWiseFetchTuple` plus the narrow-text
carve-out) and the `ReadResults` branch for both single-record and
two-record tuple result sets.
- **`RowWiseFetchTests`** — covers fixed/nullable/temporal/fixed-string
columns, NULL/empty/full-capacity values, multi-block boundaries, empty
results, `Where`/`Range`, statement reuse and the `std::string`
fallback, asserting via a block-fetch-counting logger that the fast path
actually ran (and that fixed-string records fall back on PostgreSQL). A
hidden opt-in benchmark (`[.rowwisefetchbench]`) reproduces the
comparison below.
## Performance
The structural win is round-trip count: for 200k rows the row-wise path
issues **~196 `SQLFetchScroll` calls instead of 200,000 `SQLFetch`
calls** (~1000x fewer round-trips). Measured end-to-end (release build,
median of 5 reps, `Query<>().All()` vs the per-row fallback, both
materializing the same `std::vector<Record>`):
| Backend | Transport | per-row `SQLFetch` | row-wise block fetch |
speedup |
|---|---|---|---|---|
| SQLite | in-process | 643 ms / 500k | 541 ms | **1.19x** |
| PostgreSQL | localhost TCP | 167 ms / 200k | 109 ms | **1.53x** |
| SQL Server 2022 | localhost TCP | 134 ms / 200k | 67 ms | **2.01x** |
The speedup scales with per-round-trip transport cost. In-process SQLite
has no socket, so its ~1.2x is purely reduced per-call CPU/ODBC overhead
— the floor, and a latency-independent constant. Over a localhost socket
(sub-millisecond RTT) it is already 1.5-2x.
On a high-latency link the round-trip term dominates: wall-clock ≈
round-trips × RTT, so the per-row path pays ~N × RTT while the block
path pays ~`ceil(N/depth)` × RTT, and the speedup approaches the
effective array depth (up to **~1000x** for narrow records, less for
wide rows that clamp to a smaller depth, and divided by whatever the
driver already prefetches per fetch). For example, modelled at 50 ms RTT
for 100k rows: ~83 min (per-row) vs ~5 s (block). The local numbers
above are a conservative lower bound demonstrating the mechanism; the
round-trip-count reduction is what carries the win to the WAN case.13 files changed
Lines changed: 2564 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
109 | 128 | | |
110 | 129 | | |
111 | 130 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
35 | 67 | | |
36 | 68 | | |
37 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
747 | 961 | | |
748 | 962 | | |
749 | 963 | | |
| |||
1268 | 1482 | | |
1269 | 1483 | | |
1270 | 1484 | | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
1271 | 1498 | | |
1272 | 1499 | | |
1273 | 1500 | | |
| |||
1293 | 1520 | | |
1294 | 1521 | | |
1295 | 1522 | | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
1296 | 1534 | | |
1297 | 1535 | | |
1298 | 1536 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
16 | 26 | | |
17 | 27 | | |
18 | 28 | | |
| |||
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
65 | 82 | | |
66 | 83 | | |
67 | 84 | | |
| |||
0 commit comments