σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Parquet skill›Module 3 · Keeping a lake healthy›Topic 3.3·~45 minAdvanced

The small-files problem and compaction

Skill positionModule 3 of 3 · Topic 3 of 4
3.3 The small-files problem and compaction
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Make the mess, then measure it
  3. 03Compact: many files in, one file out
  4. 04What the tasks ask

Nobody sets out to build a lake of ten thousand tiny files. It happens by itself: a job appends every five minutes, a backfill writes one file per day, and a year later every query spends longer opening files than reading them. The data did not change, the layout decayed. Knowing how to see it and fix it is routine lake maintenance.

01

The idea in plain words

Where we are. Everything so far, row groups, statistics, partitions, assumed files of sensible size. This topic is about what goes wrong over time and how to repair it with the metadata alone. The capstone puts the whole layout together, including how often to compact.

DefinitionCompaction

Rewriting many small files as a few large, well-organised ones without changing the data. It lowers the per-file costs of listing, opening and reading footers, and gives compression and statistics enough rows to work with.

02

Make the mess, then measure it

write_small_files cuts the table into slices of 200 rows, one file each, as an appending job would. file_rows then lists the files and their row counts from the footers alone, which is how you would audit a real lake without paying to read it. The totals show the cost: the same rows take more bytes in many files than in one.

main.pyPython
# Write the bars as files of 200 rows, then audit them from the footersos.makedirs("parts", exist_ok=True)print(write_small_files(table, "parts", 200), "files written")files = file_rows("parts")print(files[:2], "...", files[-1])print("bytes in all the small files:", sum(os.path.getsize("parts/" + name) for name, _ in files))
168 files written
[('part-0000.parquet', 200), ('part-0001.parquet', 200)] ... ('part-0167.parquet', 93)
bytes in all the small files: 1762336
main.pyPython
# The candidates for compaction: files below 500 rowssmall = small_files("parts", 500)print(len(small), "of", len(files), "files are below 500 rows")
168 of 168 files are below 500 rows
03

Compact: many files in, one file out

compact reads the files in name order, concatenates them and writes one file with row groups of a size you choose. Because the small files were written in order, the result is the original table, now in a handful of row groups whose statistics are tight and whose pages compress well. The small files are left in place: delete them only after the new file has been checked.

main.pyPython
# Merge all the small files into one file with row groups of 10,000 rows, and comparemerged = compact("parts", "compacted.parquet", 10000)print("files merged, row groups written:", merged)big = os.path.getsize("compacted.parquet")print("bytes in the one file:", big)print("same rows:", pq.read_table("compacted.parquet").equals(table))
files merged, row groups written: (168, 4)
bytes in the one file: 1268429
same rows: True
Compact on a schedule, not by hand

Compact the partitions that stopped receiving writes: yesterday's folder, not today's. Aim for files of 128 MB to 1 GB, check that the row counts match before deleting anything, and keep readers working throughout, which usually means writing the new file under a new name and then swapping. Table formats such as Iceberg and Delta automate exactly this.

04

What the tasks ask

  1. 1
    `write_small_files`Loop range(0, table.num_rows, rows_per_file), write table.slice(start, rows_per_file) as part-NNNN.parquet, count the files.
  2. 2
    `file_rows` and `small_files`pq.ParquetFile(path).metadata.num_rows for each .parquet file in sorted order; then keep the names below min_rows.
  3. 3
    `compact`pa.concat_tables of the files read in name order; pq.write_table(..., row_group_size=...); return the file count and the new file's row-group count; with no files, return (0, 0).

Key takeaways

  • Files have a fixed cost Listing, opening and a footer per file, whatever its size.
  • Audit from the footers Row counts and sizes cost almost nothing to read.
  • Write the new file before deleting the old Check the counts, then swap. Next: the capstone price lake.
Quick check
A lake of 20,000 files of 50 rows each is slow to query, though the total is only a million rows. What is the best fix?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Make the mess: write_small_files25 pts
  • Count rows from the footers: file_rows25 pts
  • Find the small ones: small_files25 pts
  • Merge them: compact25 pts
Explorer · lab-8-1
▾ lab-8-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-8-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-8-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes