Effectieve query-uitvoering

Data-pipelines schalen en optimaliseren met Polars

Liam Brannigan

Data Scientist & Polars Contributor

Maak kennis met je instructeur

$$

$$

  • Liam Brannigan, Lead Data Scientist
  • ML- en data-engineeringspecialist
  • Polars-bijdrager

Foto van Liam Brannigan - de instructeur van deze course

Data-pipelines schalen en optimaliseren met Polars

Van laptop naar cloud

Polars-pipeline schaalt van laptop naar cloud

Data-pipelines schalen en optimaliseren met Polars

Is deze course iets voor jou?

Polars-cursuspagina's

  • Een luie query maken met scan_csv
  • Expressies schrijven met filter, select en group_by
Data-pipelines schalen en optimaliseren met Polars

Hoofdstuk 1 - optimalisatie

Queryoptimalisatie

Data-pipelines schalen en optimaliseren met Polars

Hoofdstuk 1 - optimalisatie

Queryoptimalisatie

Data-pipelines schalen en optimaliseren met Polars

Hoofdstuk 2 - efficiënte I/O

Afbeelding van Polars die meerdere bestandstypen inleest

Data-pipelines schalen en optimaliseren met Polars

Hoofdstuk 3 - rijkere dtypes

Diagram van geneste data getransformeerd naar een tabel.

Data-pipelines schalen en optimaliseren met Polars

Hoofdstuk 4 - pipelines opschalen

Afbeelding van een grote tabel die wordt gestreamd.

Data-pipelines schalen en optimaliseren met Polars

Chicago-requests dataset

Het data-analyticateam van de burgemeester van Chicago

Data-pipelines schalen en optimaliseren met Polars

De dataset inspecteren

requests = pl.scan_csv("311_Service_Requests.csv",try_parse_dates=True)

Een enorme dataset met alle serviceverzoeken van Chicago-inwoners

Data-pipelines schalen en optimaliseren met Polars

De dataset inspecteren

requests.collect()
Data-pipelines schalen en optimaliseren met Polars

De dataset inspecteren

requests.collect().head(5)
shape: (5, 39)
| TYPE                          | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                           | ---       | ---            | ---                 | --- |
| str                           | str       | str            | str                 | ... |
|-------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint   | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request             | Cancelled | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance      | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint   | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up             | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
Data-pipelines schalen en optimaliseren met Polars

Verwerkte rijen beperken

requests.head(5)
Data-pipelines schalen en optimaliseren met Polars

Verwerkte rijen beperken

requests.head(5).collect()
shape: (5, 39)
| TYPE                            | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                             | ---       | ---            | ---                 | --- |
| str                             | str       | str            | str                 | ... |
|---------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint     | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request | Completed | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance        | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint     | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up               | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
  • Stel collect uit
Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department


Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department = requests


Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
)
Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect()
Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect().group_by("DEPARTMENT").len()
Data-pipelines schalen en optimaliseren met Polars

Query: samenvatting per afdeling

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len().collect()
shape: (10, 2)
| DEPARTMENT                     | len     |
| ---                            | ---     |
| str                            | u32     |
|-------------------------------|---------|
| 311 City Services              | 4859161 |
| Sanitation                     | 3406631 |
| Aviation                       | 2337842 |
| CDOT - Department of Transport | 1468240 |
Data-pipelines schalen en optimaliseren met Polars

Uitelopende querytakken

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len()
completed_by_month = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("MONTH").len()
Data-pipelines schalen en optimaliseren met Polars

Hoe het team dit nu draait

completed_by_department.collect()
completed_by_month.collect()
Data-pipelines schalen en optimaliseren met Polars

Uitelopende queries uitvoeren

results = pl.collect_all(


)
Data-pipelines schalen en optimaliseren met Polars

Uitelopende queries uitvoeren

results = pl.collect_all([
    completed_by_department,
    completed_by_month,
])
Data-pipelines schalen en optimaliseren met Polars

Uitelopende queries uitvoeren

results[0]  # completed_by_department
shape: (10, 2)
| DEPARTMENT           | len     |
| ---                  | ---     |
| str                  | u32     |
|----------------------|---------|
| 311 City Services    | 4859161 |
| Sanitation           | 3406631 |
| Aviation             | 2337842 |
| Transport            | 1468240 |
results[1]  # completed_by_month
shape: (12, 2)
| MONTH | len  |
| ---   | ---  |
| i64   | u32  |
|-------|------|
| 1     | 2506 |
| 2     | 4566 |
| 3     | 2739 |
| 4     | 2922 |
Data-pipelines schalen en optimaliseren met Polars

Laten we oefenen!

Data-pipelines schalen en optimaliseren met Polars

Preparing Video For Download...