Path: blob/main/docs/source/user-guide/lazy/execution.md
6940 views
Query execution
Our example query on the Reddit dataset is:
{{code_block('user-guide/lazy/execution','df',['scan_csv'])}}
If we were to run the code above on the Reddit CSV the query would not be evaluated. Instead Polars takes each line of code, adds it to the internal query graph and optimizes the query graph.
When we execute the code Polars executes the optimized query graph by default.
Execution on the full dataset
We can execute our query on the full dataset by calling the .collect
method on the query.
{{code_block('user-guide/lazy/execution','collect',['scan_csv','collect'])}}
Above we see that from the 10 million rows there are 14,029 rows that match our predicate.
With the default collect
method Polars processes all of your data as one batch. This means that all the data has to fit into your available memory at the point of peak memory usage in your query.
!!! warning "Reusing LazyFrame
objects"
Execution on larger-than-memory data
If your data requires more memory than you have available Polars may be able to process the data in batches using streaming mode. To use streaming mode you simply pass the engine="streaming"
argument to collect
{{code_block('user-guide/lazy/execution','stream',['scan_csv','collect'])}}
Execution on a partial dataset
While you're writing, optimizing or checking your query on a large dataset, querying all available data may lead to a slow development process.
Instead, you can scan a subset of your partitions or use .head
/.collect
at the beginning and end of your query, respectively. Keep in mind that the results of aggregations and filters on subsets of your data may not be representative of the result you would get on the full data.
{{code_block('user-guide/lazy/execution','partial',['scan_csv','collect','head'])}}
Diverging queries
It is very common that a query diverges at one point. In these cases it is recommended to use collect_all
as they will ensure that diverging queries execute only once.