Tối ưu hóa và kế hoạch truy vấn

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Liam Brannigan

Data Scientist & Polars Contributor

Giới thiệu tối ưu truy vấn

Hình bảng với các hàng và cột

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Giới thiệu tối ưu truy vấn

Hình bảng với các hàng và cột

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Giới thiệu tối ưu truy vấn

Hình các thao tác chạy tuần tự

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Giới thiệu tối ưu truy vấn

Hình các thao tác chạy song song

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Giới thiệu tối ưu truy vấn

Pipeline với thao tác trùng lặp được thực hiện hai lần

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Giới thiệu tối ưu truy vấn

Pipeline với thao tác trùng lặp được thực hiện hai lần

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Loại yêu cầu nhiều nhất theo phòng ban

department_request_types = (
    requests





)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Loại yêu cầu nhiều nhất theo phòng ban

department_request_types = (
    requests
    .filter(pl.col("STATUS") == "Completed")




)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Loại yêu cầu nhiều nhất theo phòng ban

department_request_types = (
    requests
    .filter(pl.col("STATUS") == "Completed")
    .group_by("DEPARTMENT")



)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Loại yêu cầu nhiều nhất theo phòng ban

department_request_types = (
    requests    
    .filter(pl.col("STATUS") == "Completed")
    .group_by("DEPARTMENT")
    .agg(pl.col("TYPE").n_unique().alias("n_request_types"))


)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Loại yêu cầu nhiều nhất theo phòng ban

department_request_types = (
    requests
    .filter(pl.col("STATUS") == "Completed")
    .group_by("DEPARTMENT")
    .agg(pl.col("TYPE").n_unique().alias("n_request_types"))
    .sort("n_request_types", descending=True)
    .head(5)
)
  • Kế hoạch "ngây thơ"
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch chưa tối ưu

print(department_request_types)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch chưa tối ưu

print(department_request_types)







        Csv SCAN [311_Service_Requests.csv]
        PROJECT */39 COLUMNS
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch chưa tối ưu

print(department_request_types)





      FILTER [(col("STATUS")) == ("Completed")]
      FROM
        Csv SCAN [311_Service_Requests.csv]
        PROJECT */39 COLUMNS
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch chưa tối ưu

print(department_request_types)


    AGGREGATE[maintain_order: false]
      [col("TYPE").n_unique().alias("n_request_types")] BY [col("DEPARTMENT")]
      FROM
      FILTER [(col("STATUS")) == ("Completed")]
      FROM
        Csv SCAN [311_Service_Requests.csv]
        PROJECT */39 COLUMNS
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch chưa tối ưu

print(department_request_types)
SLICE[offset: 0, len: 5]
  SORT BY [descending: [true]] [col("n_request_types")]
    AGGREGATE[maintain_order: false]
      [col("TYPE").n_unique().alias("n_request_types")] BY [col("DEPARTMENT")]
      FROM
      FILTER [(col("STATUS")) == ("Completed")]
      FROM
        Csv SCAN [311_Service_Requests.csv]
        PROJECT */39 COLUMNS
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch đã tối ưu

print(department_request_types.explain())
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch đã tối ưu







        Csv SCAN [311_Service_Requests.csv]
        PROJECT 3/39 COLUMNS
        SELECTION: [(col("STATUS")) == ("Completed")]
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch đã tối ưu






      simple pi 2/2 ["TYPE", "DEPARTMENT"]
        Csv SCAN [311_Service_Requests.csv]
        PROJECT 3/39 COLUMNS
        SELECTION: [(col("STATUS")) == ("Completed")]
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch đã tối ưu



    AGGREGATE[maintain_order: false]
      [col("TYPE").n_unique().alias("n_request_types")] BY [col("DEPARTMENT")]
      FROM
      simple pi 2/2 ["TYPE", "DEPARTMENT"]
        Csv SCAN [311_Service_Requests.csv]
        PROJECT 3/39 COLUMNS
        SELECTION: [(col("STATUS")) == ("Completed")]
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch đã tối ưu

SORT BY [slice: (0, 10, ...), descending: [true]] [col("n_request_types")]
  FILTER col("n_request_types").dynamic_predicate() FROM
    AGGREGATE[maintain_order: false]
      [col("TYPE").n_unique().alias("n_request_types")] BY [col("DEPARTMENT")]
      FROM
      simple pi 2/2 ["TYPE", "DEPARTMENT"]
        Csv SCAN [311_Service_Requests.csv]
        PROJECT 3/39 COLUMNS
        SELECTION: [(col("STATUS")) == ("Completed")]
  • Lấy top 10 hàng theo n_request_types
  • Loại bỏ phần còn lại
  • Sắp xếp 10 hàng
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kế hoạch tối ưu dạng đồ thị

print(department_request_types.show_graph())

Dạng đồ thị của kế hoạch tối ưu hiển thị quét CSV với tối ưu hóa.

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tối ưu hóa thêm

(
    requests








)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tối ưu hóa thêm

(
    requests
    .filter(pl.col("STATUS") == "Completed")
    .filter(pl.col("DEPARTMENT") == "Sanitation")






)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tối ưu hóa thêm

(
    requests
    .filter(pl.col("STATUS") == "Completed")
    .filter(pl.col("DEPARTMENT") == "Sanitation")
    .with_columns(
        pl.col("TYPE").str.to_lowercase().alias("type_lower")
    )
    .with_columns(
        pl.col("STATUS").str.to_lowercase().alias("status_lower")
    )
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tối ưu hóa thêm



  Csv SCAN [311_Service_Requests.csv]
  PROJECT */39 COLUMNS
  SELECTION: [([(col("DEPARTMENT")) == ("Sanitation")]) & ([(col("STATUS")) == ("Completed")])]
  • Gộp điều kiện AND
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tối ưu hóa thêm

 WITH_COLUMNS:
 [col("TYPE").str.to_lowercase().alias("type_lower"), col("STATUS").str.to_lowercase().alias("status_lower")]
  Csv SCAN [311_Service_Requests.csv]
  PROJECT */39 COLUMNS
  SELECTION: [([(col("DEPARTMENT")) == ("Sanitation")]) & ([(col("STATUS")) == ("Completed")])]
  • Gộp điều kiện AND
  • Gom cụm các biểu thức WITH_COLUMNS
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

¡Vamos a practicar!

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Preparing Video For Download...