> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to increase the chunk size of query_arrow_stream in clickhouse-connect

> Each chunk of query_arrow_stream is one Arrow record batch written by the server. See which ClickHouse settings make the batches larger for tables and for Parquet files.

The `query_arrow_stream` method of [ClickHouse Connect](https://clickhouse.com/docs/integrations/language-clients/python/advanced-querying.md#streaming-queries) returns a PyArrow `RecordBatch` on each iteration.
The client doesn't choose the size of these batches: ClickHouse writes one record batch for every block of the query result, so small blocks give small chunks.

To get larger chunks, pass [`output_format_arrow_record_batch_size`](https://clickhouse.com/docs/reference/settings/formats/output-format#output_format_arrow_record_batch_size) in the `settings` of the query. It makes the server combine small blocks into batches of about that many rows. The setting exists since ClickHouse 26.9 (and in 26.8 starting from 26.8.7). On older servers, raise the settings that limit the size of the blocks, described below.

## Example

Create a table with a wide `String` column:

```sql theme={null}
CREATE TABLE page_views
(
    id UInt64,
    user_id UInt32,
    url String,
    payload String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO page_views
SELECT
    number AS id,
    number % 1000 AS user_id,
    concat('https://example.com/page/', toString(number % 500)) AS url,
    repeat('x', 1000) AS payload
FROM numbers(200000);
```

Count the rows of every batch with and without the setting:

```python theme={null}
import clickhouse_connect

client = clickhouse_connect.get_client(host='localhost', username='default', password='password')


def batch_sizes(query, settings=None):
    with client.query_arrow_stream(query, settings=settings) as stream:
        return [batch.num_rows for batch in stream]


query = 'SELECT * FROM page_views'
print(batch_sizes(query)[:3])
print(batch_sizes(query, {'output_format_arrow_record_batch_size': 100_000}))
```

The following numbers of rows per batch were measured on the `ArrowStream` output of ClickHouse 26.12 for this table (the last batch of a result can be smaller):

| Query | Settings | Batches | Rows per batch |
| - | - | - | - |
| `SELECT * FROM page_views` | defaults | 25 | 8192 |
| `SELECT * FROM page_views` | `output_format_arrow_record_batch_size = 100000` | 2 | 106496 and 93504 |
| `SELECT * FROM page_views` | `preferred_block_size_bytes = 0` | 4 to 6 | up to 65409 |
| `SELECT id, user_id, url FROM page_views WHERE user_id = 7` | defaults | 12 | 16 to 19 |
| `SELECT id, user_id, url FROM page_views WHERE user_id = 7` | `output_format_arrow_record_batch_size = 65409` | 1 | 200 |

## Combine blocks in the Arrow output

`output_format_arrow_record_batch_size` sets a target number of rows per record batch and [`output_format_arrow_record_batch_size_bytes`](https://clickhouse.com/docs/reference/settings/formats/output-format#output_format_arrow_record_batch_size_bytes) a target size in bytes. Both are `0` by default, which means one record batch per block. With a target:

* Blocks are collected until one of the targets is reached, and then written as one batch.
* The targets never split a block. With a row target, a combined batch holds fewer than twice that many rows.
* A batch is split only by [`output_format_arrow_max_record_batch_size_bytes`](https://clickhouse.com/docs/reference/settings/formats/output-format#output_format_arrow_max_record_batch_size_bytes), a limit on the `String`, binary and `Array` data of one batch. Its default, `2147483646`, is the largest buffer that Arrow can address, so it matters only for batches with about 2 GiB of such data, or when you lower it.
* The server buffers the blocks, so it uses more memory and the first batch arrives later. A result that never reaches the target arrives as one batch when the query finishes.

Use it in particular when a selective `WHERE` leaves a few rows in every block, as in the last two rows of the table above: the block size settings below don't merge such blocks.

## Settings that limit the size of a block

For a query that reads a `MergeTree` table:

* [`max_block_size`](https://clickhouse.com/docs/reference/settings/session-settings/max#max_block_size) (default `65409`) is the maximum number of rows in a block.
* [`preferred_block_size_bytes`](https://clickhouse.com/docs/reference/settings/session-settings/preferred#preferred_block_size_bytes) (default `1000000`) lowers the number of rows when the rows are wide, but not below one granule. That is why the example returns batches of 8192 rows by default. Set it to `0` to turn this limit off.
* A filter removes rows from every block, and the small blocks that remain aren't merged before the output.

For a query that reads Parquet files with the `s3`, `url` or `file` table function, the Parquet reader has its own settings, and `max_block_size` isn't used:

* [`input_format_parquet_max_block_size`](https://clickhouse.com/docs/reference/settings/formats/input-format#input_format_parquet_max_block_size) (default `65409`) is the maximum number of rows.
* [`input_format_parquet_prefer_block_bytes`](https://clickhouse.com/docs/reference/settings/formats/input-format#input_format_parquet_prefer_block_bytes) (default `16744704`) is the preferred size of a block in bytes. On a server with a memory limit, ClickHouse also caps it at a small fraction of that limit, so very large values have no further effect.
* A block never contains rows of more than one row group of the file.

Measured for a Parquet file with 1,000,000 rows in one row group, created by the first statement and read by the second:

```sql theme={null}
INSERT INTO FUNCTION file('reviews.parquet')
SELECT
    number AS id,
    concat('product-', toString(number % 1000)) AS product,
    repeat('r', 200) AS review
FROM numbers(1000000);

SELECT * FROM file('reviews.parquet');
```

| Settings | Rows per batch |
| - | - |
| defaults | 65409 |
| `max_block_size = 500000` | 65409 |
| `input_format_parquet_max_block_size = 500000` | 76498 (limited by `input_format_parquet_prefer_block_bytes`) |
| `input_format_parquet_max_block_size = 500000`, `input_format_parquet_prefer_block_bytes = 1000000000` | 500000 |
| `output_format_arrow_record_batch_size = 500000` | 523272 and 476728 |

## Things to keep in mind

* Pass the settings in the `settings` argument of the query method or of `get_client`, see [Settings argument](https://clickhouse.com/docs/integrations/language-clients/python/driver-api.md#settings-argument-1). The user must be allowed to change them: for a setting that the server marks as read-only for the user, for example with `readonly = 1`, ClickHouse Connect raises `ProgrammingError` unless its `invalid_setting_action` option is set to `drop` or `send`, see [Additional options](https://clickhouse.com/docs/integrations/language-clients/python/additional-options.md).
* `query_df_arrow_stream` converts the same Arrow batches to DataFrames, so the same settings control the size of its chunks.
* The size of a block isn't a guarantee. Treat the settings as limits and targets, and write the application so that it accepts batches of any size.
* Larger batches need more memory on the server and in the client for every batch.

## Related pages

* [Streaming queries in ClickHouse Connect](https://clickhouse.com/docs/integrations/language-clients/python/advanced-querying.md#streaming-queries)
* [`ArrowStream` format](https://clickhouse.com/docs/reference/formats/Arrow/ArrowStream)
