Skip to content

PERF: improve performance of read_dataframe if a filter is used - #577

Merged
theroggy merged 15 commits into
geopandas:mainfrom
theroggy:ENH-improve-performance-of-read_dataframe
Jun 16, 2026
Merged

PERF: improve performance of read_dataframe if a filter is used#577
theroggy merged 15 commits into
geopandas:mainfrom
theroggy:ENH-improve-performance-of-read_dataframe

Conversation

@theroggy

@theroggy theroggy commented Sep 13, 2025

Copy link
Copy Markdown
Member

In read_dataframe without arrow, the number of rows of the result was counted first, and then the full data was read.

Especially when using a filter, counting the rows can take significant time.

This PR avoids doing the rowcount before reading to improve performance by reading the results in chunks till no more rows are returned from the dataset. For the new zealand building outlines geopackage (3.3 million rows) this gives these results:

  • If the filter limits the rows a lot counting the rows can even take the same time as the subsequent reading of all data... so in this case the time taken ~halves.
    • e.g. reading the test file file with where="ST_NPOINTS(st_buffer(geom, 10)) > 2000" (returning 9 rows) took 82 s, now 45 s.
  • When reading the entire file without filter, both implementations take 55-60 seconds on my windows laptop (plugged in), with the chunked implementation giving the same average timings. To be sure, if no filter is used and counting rows is fast for the file type involved, no chunking is used (=old way).

@theroggy
theroggy marked this pull request as ready for review September 13, 2025 15:49
@theroggy
theroggy marked this pull request as draft September 13, 2025 15:50
@theroggy
theroggy marked this pull request as ready for review September 13, 2025 20:30
@theroggy theroggy modified the milestones: 0.11.0, 0.12.0 Sep 14, 2025
@theroggy theroggy modified the milestones: 0.12.0, 0.12.1, 0.13.0 Nov 21, 2025

@jorisvandenbossche jorisvandenbossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't know if it is worth the added complexity, but in theory the following optimization might be possible: check if the driver supports fast count, and in the case there are no filters, that count should be a cheap lookup (like GPKG without any filtering), and at that point actually populate num_features. That would avoid using the growing chunk size in those cases (and we could directly read the entire data or directly use the max chunk size)

Comment thread pyogrio/_io.pyx
Comment thread pyogrio/_io.pyx Outdated
Comment thread pyogrio/_io.pyx
@theroggy

theroggy commented Jun 13, 2026

Copy link
Copy Markdown
Member Author

I don't know if it is worth the added complexity, but in theory the following optimization might be possible: check if the driver supports fast count, and in the case there are no filters, that count should be a cheap lookup (like GPKG without any filtering), and at that point actually populate num_features. That would avoid using the growing chunk size in those cases (and we could directly read the entire data or directly use the max chunk size)

According to my tests when I wrote the PR, I didn't measure a significant difference between chunking or not for the case where no filtering was done, but it is not difficult to shortcut this case, so I added it.

@jorisvandenbossche jorisvandenbossche changed the title ENH: improve performance of read_dataframe if a filter is used PERF: improve performance of read_dataframe if a filter is used Jun 16, 2026

@jorisvandenbossche jorisvandenbossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good! Just two suggestions for clarifying comments for the future reader

Comment thread pyogrio/_io.pyx
Comment thread pyogrio/_io.pyx Outdated
@jorisvandenbossche

Copy link
Copy Markdown
Member

(and needs to fix the merge conflicts)

@theroggy
theroggy merged commit 467d338 into geopandas:main Jun 16, 2026
29 checks passed
@theroggy
theroggy deleted the ENH-improve-performance-of-read_dataframe branch June 16, 2026 14:41
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