使用数据库

使用 Polars 扩展与优化数据流水线

Liam Brannigan

Data Scientist & Polars Contributor

在 Polars 中查询数据库

概念图:Polars 向数据库发送 SQL 查询和 uri。

使用 Polars 扩展与优化数据流水线

在 Polars 中查询数据库

概念图:Polars 向数据库发送 SQL 查询,并通过数据库引擎接收结果表。

使用 Polars 扩展与优化数据流水线

Polars 可查询的数据库

Polars 可使用的数据库

  • 内嵌型 → 本地运行,无服务器
  • DuckDB → 分析型数据库
使用 Polars 扩展与优化数据流水线

建筑许可表

SELECT *
FROM building_permits
LIMIT 5
shape: (5, 5)
| issue_date | permit_type | review_type | street_name  | work_description             |
| ---        | ---         | ---         | ---          | ---                          |
| date       | str         | str         | str          | str                          |
|------------|-------------|-------------|--------------|------------------------------|
| 2026-04-30 | Signs       | SIGN PERMIT | CLARK ST     | Wall sign install            |
| 2026-04-28 | Renovation  | SELF CERT   | OAKLEY AVE   | Add dormer to 8-unit roof    |
| 2026-03-21 | Easy Permit | EASY PERMIT | SEMINARY AVE | Parapet masonry repair       |
| 2026-03-11 | Easy Permit | EASY PERMIT | RHODES AVE   | Rebuild south chimney side   |
| 2026-03-03 | Easy Permit | EASY PERMIT | GIDDINGS ST  | Porch lintel + tuckpointing  |
使用 Polars 扩展与优化数据流水线

连接 URI

import polars as pl

uri = "postgresql://analyst:[email protected]:5432/buildings"
  • postgresql:// 协议
  • analyst:secret 用户名与密码
  • data.chicago.org:5432 主机与端口
  • buildings 数据库名
使用 Polars 扩展与优化数据流水线

查询数据库

query = """
    SELECT issue_date, permit_type
    FROM building_permits
    WHERE issue_date >= DATE '2026-01-01'
"""







使用 Polars 扩展与优化数据流水线

查询数据库

query = """
    SELECT issue_date, permit_type
    FROM building_permits
    WHERE issue_date >= DATE '2026-01-01'
"""

permit_counts = (
    pl.read_database_uri(query=query, uri=uri)



)
使用 Polars 扩展与优化数据流水线

查询数据库

query = """
    SELECT issue_date, permit_type
    FROM building_permits
    WHERE issue_date >= DATE '2026-01-01'
"""

permit_counts = (
    pl.read_database_uri(query=query, uri=uri)
    .group_by("permit_type")
    .len()
    .sort("len", descending=True)
)
使用 Polars 扩展与优化数据流水线

按类型汇总的许可证计数

permit_counts
shape: (3, 2)
| permit_type                     | len |
| ---                             | --- |
| str                             | u32 |
|---------------------------------|-----|
| Easy Permit                     | 58  |
| New Construction                | 14  |
| Renovation                      | 11  |
使用 Polars 扩展与优化数据流水线

选择引擎

pl.read_database_uri(
    query=query,
    uri=uri,
    engine="connectorx",
)
  • 支持更多数据库
pl.read_database_uri(
    query=query,
    uri=uri,
    engine="adbc",
)
  • 对受支持的数据库更快
1 https://arrow.apache.org/adbc/current/driver/status.html
使用 Polars 扩展与优化数据流水线

写回数据库

permit_counts.write_database(



)
使用 Polars 扩展与优化数据流水线

写回数据库

permit_counts.write_database(
    table_name="permit_type_summary",
    connection=uri,

)
使用 Polars 扩展与优化数据流水线

写回数据库

permit_counts.write_database(
    table_name="permit_type_summary",
    connection=uri,
    if_table_exists="replace",
)
  • append
  • fail
使用 Polars 扩展与优化数据流水线

DuckDB 集成

import duckdb

db = duckdb.connect("permits.duckdb")


$$

  • DuckDB → Polars 原生输出
使用 Polars 扩展与优化数据流水线

DuckDB 集成

import duckdb

db = duckdb.connect("permits.duckdb")

result = db.sql("SELECT * FROM permit_type_summary").pl()
shape: (3, 2)
| permit_type                     | len |
| ---                             | --- |
| str                             | u32 |
|---------------------------------|-----|
| Easy Permit                     | 58  |
| New Construction                | 14  |
| Renovation                      | 11  |
使用 Polars 扩展与优化数据流水线

Vamos praticar!

使用 Polars 扩展与优化数据流水线

Preparing Video For Download...