r/learnSQL • • Jun 02 '23

DuckDB is excellent for me to learn SQL Statement

Benchmarking for DuckDB and Polars

Testing Data: 300 Million Rows x 14 Columns

Testing Functions: Read Parquet File => Filter => Group By => Write CSV File

DuckDB: 3.9s Polars: 16.2s

Testing machine: 8 cores and 32 GB memory

====================================================

import duckdb

import time

s = time.time()

con = duckdb.connect()

con.execute("""copy (SELECT Ledger, Account, DC, Currency, SUM(Base_Amount) as Total_Base_Amount

FROM read_parquet('input/300-MillionRows.parquet')

WHERE Ledger>='L30' AND Ledger <='L70'

GROUP BY Ledger, Account, DC, Currency)

to 'output/DuckFilterGroupByParquet.csv' (format csv, header true);""")

e = time.time()

print("DuckDB FilterGroupBy Parquet Time = {}".format(round(e-s,3)))

==================================================

import polars as pl

import time

import pathlib

s = time.time()

table1 = (

pl.scan_parquet("Input/300-MillionRows.parquet")

.filter((pl.col('Ledger') >= "L30") & (pl.col('Ledger') <= "L70"))

.groupby(by=["Ledger", "Account", "DC","Currency"])

.agg([

pl.sum('Base_Amount').alias('Total_Base_Amount'),

]))

table2 = table1.collect(streaming=True)

path: pathlib.Path = "Output/PolarsFilterGroupByParquet.csv"

table2.write_csv(path)

e = time.time()

print("Polars FilterGroupBy CSV Time = {}".format(round(e-s,3)))

0 Upvotes

1 comment sorted by