Luna and DuckDB
Starting with Luna 1.9, DuckDB is the default format for result
databases in the command-line tools, Python (lunapi), and R
(lunaR). In our preliminary benchmark, DuckDB exported all result
tables about 56 times as fast as SQLite and used about one-third
of the initial disk space.
DuckDB is a modern embedded SQL database designed for analytical queries such as filtering, grouping, and summarizing large tables. The engine runs inside Luna or the tool reading the results, so you can work with the database file directly without starting a server. See Why DuckDB? for how the engine works.
Benchmark dataset
We ran power spectrum,
coherence, and hypnogram
analyses with epoch-level output on 301 recordings. Each recording
contributed two EEG channels (FZ and PZ), with coherence
computed for the FZ–PZ pair.
PSD epoch-spectrum dB max=60
COH epoch-spectrum max=60
HYPNO epoch
Generating results
Although the analysis used only two channels, its epoch-by-epoch outputs produced a substantial database: 467,870,151 values across 15 tables.
To compare DuckDB with the original SQLite implementation, we measured file sizes and the time to generate and extract results. We used an Apple M5 MacBook Pro with 32 GB RAM and macOS 27.2.1.
| SQLite | DuckDB | |
|---|---|---|
| Run the analysis and write results | 7 min 2 s | 6 min 31 s |
| Database size after writing | 13 GiB | 4.1 GiB |
| Database size after the legacy SQLite reader indexed it | 18 GiB | 4.1 GiB |
The Luna run was marginally faster when using DuckDB -- for typical runs you won't notice much difference -- but the resulting file size was much smaller at less than one-quarter of the indexed SQLite file.
Extracting results
The largest speed gains came when working with saved results: tasks that took minutes with SQLite took seconds with DuckDB.
We used the legacy SQLite reader (destrat-sqlite) and the DuckDB
reader (destrat) to list the result tables and export them. The
first SQLite inspection also built an index and so was one minute
slower than the second run. DuckDB could list the tables from its
catalog.
| Task | SQLite | DuckDB |
|---|---|---|
| List result tables, first run | 6 min 43 s | 1.1 s |
| List result tables, second run | 5 min 49 s | 0.9 s |
| Export epoch-level PSD as TSV | 3 min 45 s | 4.7 s |
| Export all 15 tables as TSV | 10 min 53 s | 11.6 s |
DuckDB exported the epoch-level PSD table about 48 times as fast and all 15 tables about 56 times as fast. The 15 TSV files from each database matched byte for byte.
Parquet for large extracts
The epoch-level PSD table alone contained 144,023,790 rows and five columns: participant ID, channel, epoch, frequency, and power. We exported that table from DuckDB as plain TSV, gzipped TSV, and Parquet. Parquet stores typed columns in a compressed file and supports reading selected columns and filtering rows.
| Format | Export time | File size | Read with pandas | Read with pandas (PyArrow CSV) | Read with Polars |
|---|---|---|---|---|---|
| TSV | 4.7 s | 5.3 GiB | 17.8 s | 6.0 s | 2.1 s |
| Gzipped TSV | 1 min 32 s | 1.6 GiB | 22.1 s | 7.9 s | 5.9 s |
| Parquet | 5.7 s | 1.2 GiB | 2.0 s | — | — |
The pandas column uses its default CSV reader for TSV and its Parquet
reader for Parquet. With a fast text reader, a full TSV read can be
competitive. Parquet's advantage here was a file 77% smaller than
plain TSV and the ability to read a subset directly. Selecting channel
FZ and frequencies up to 20 Hz took 0.5 s and returned 23.8
million rows, about one-sixth of the table. A separate Parquet export
of the same table via lunapi took 1.6 s.
Selective loading
In Python, opening the DuckDB results with proj.import_db() made the
15 tables available for inspection while leaving the data on disk.
The whole-night PSD table had 143,878 rows. By default, lunapi checks
the size of a table before loading it and blocks oversized requests
to help prevent accidental memory exhaustion. The epoch-level table
had 144 million rows and would have used an estimated 6.5 GB in
memory, so it exceeded the default limit. When we explicitly allowed
the full load with force=True, it took 12.9 s.
For a check on two participants, say P001 and P002, you can
restrict the import to those IDs. For an analysis of a particular
channel or frequency range, you can query the result database or
read a filtered Parquet extract. Both approaches reduce the amount
of data brought into memory. The 6.5 GB figure is lunapi's full-table
estimate. The benchmark did not measure peak process memory.
See the Reference section below for example commands.
Parallel processing
To illustrate how parallel processing can speed up analysis, we reran
the same commands on the same 301 recordings using eight
lunapi worker processes on the same MacBook Pro. procn() splits
the sample list across the workers, so each process runs PSD, COH,
and HYPNO on its own recordings. With out_db set, each worker
writes its results to a separate DuckDB file. Specifically:
import lunapi as lp
p = lp.proj()
p.sample_list("s.lst")
analysis = """
PSD epoch-spectrum dB max=60
COH epoch-spectrum max=60
HYPNO epoch
"""
result = p.procn( analysis,
workers=8,
out_db="out-" )
The analysis and database writes took 1 min 59 s, compared with
6 min 31 s for the single-process DuckDB run above. That is about
3.3 times as fast on one laptop. The eight files were named
out-1.db through out-8.db.
As with SQLite results, destrat can combine multiple DuckDB result
files on the fly. See working with multiple
databases. To export one
Parquet file from all eight databases, we can pass the worker
databases to destrat in one command. It combines their results on
the fly and leaves the input files separate.
destrat out-*.db +PSD -r E F CH --parquet epoch-psd.parquet
+PSD selects the PSD command and -r E F CH selects its
epoch-by-frequency-by-channel table. Participant ID is included
automatically, so the Parquet file has one row per participant, epoch,
frequency, and channel, with PSD power as a value column. The export
from all eight databases took 9.1 seconds.
Notes
Like SQLite, DuckDB allows multiple readers but only one writer at a time for a database file. DuckDB can coordinate concurrent writes from threads within one process, but separate processes cannot write to the same database concurrently.
Each Luna database also has a catalog of its tables, factors, variables,
and types. When you list the available tables, destrat can consult
that catalog without scanning the result values. When you request one
table, it can read and export that table directly.
For most analyses, we recommend keeping the results in a DuckDB
database and exporting individual tables as needed. This gives you
one file per run and avoids collecting a separate set of text files
for every participant. Use -t when a downstream workflow specifically
expects per-participant text files. See the
output options and
DuckDB support.
Luna continues to read and write SQLite results. In the CLI, -o writes
a DuckDB database and -o out.db --sqlite explicitly selects SQLite. The
SQLite output path already uses prepared statements, groups writes into
transactions, and disables synchronous disk syncing while writing to reduce
write overhead. In our benchmark, the largest performance differences were
in reading and exporting results.
Reference
These were the specific commands used with a Luna sample list
(s.lst), the DuckDB-enabled luna and destrat, and the legacy
destrat-sqlite reader.
Running the analyses and writing the databases:
analysis='PSD epoch-spectrum dB max=60 & COH epoch-spectrum max=60 & HYPNO epoch'
# legacy SQLite outputs --> out0.db
luna s.lst -o out0.db --sqlite -s "$analysis"
# new DuckDB outputs --> out1.db
luna s.lst -d out1.db -s "$analysis"
Initial review of database contents:
destrat-sqlite out0.db
destrat out1.db
Extracting the channel-by-frequency-by-epoch-by-individual table:
destrat-sqlite out0.db +PSD -r F CH E > psd-sqlite.tsv
destrat out1.db +PSD -r F CH E > psd-duckdb.tsv
Extracting all outputs as TSV, and establishing they are identical (diff on macOS)
destrat-sqlite out0.db -t tout0
destrat out1.db -t tout1
diff -rq tout0/ tout1/
Exporting compressed formats (Parquet and gzipped TSV):
destrat out1.db +PSD -r F CH E --parquet psd.parquet
destrat out1.db +PSD -r F CH E | gzip > psd.tsv.gz
Using Python/lunapi to work with the databases:
import lunapi as lp
import pandas as pd
To extract the same table as a Parquet file from DuckDB:
lp.destrat("out1.db").export_table("psd-python.parquet", "PSD", "CH_E_F")
To read a subset of a Parquet file (here, PSD values up to 20 Hz for FZ):
fz = pd.read_parquet( "psd.parquet", filters=[ ("CH", "==", "FZ"), ("F", "<=", 20) ],
)
To import a DuckDB output in Python:
p = lp.proj()
p.import_db("out1.db")
To list the strata (available tables) in the attached database:
p.strata()
To extract a single table:
whole_night = p.table("PSD", "CH_F")