Repository navigation
Done
Labels
Description
Problem
- Export. With "Compress with gzip" on, the SQL export writes the whole dump uncompressed to a temp file, then reads it back and compresses it. Compression starts only after the last row is read, so it cannot overlap the database read. Peak temp disk use is the uncompressed size plus the compressed size. A compressed export also cannot be split into parts. Estimate, not measured: zlib at the default level on SQL text runs at roughly 80-150 MB/s on an M4, so a 2 GB dump adds about 13-25 s after the data phase.
- Import. Picking a
.sql.gzinflates the whole file to a temp.sqlbefore the dialog can show its preview. The dialog then parses the whole file to count statements, and the import parses it a second time. Every byte is inflated and written once, then read and lexed twice. Estimate, not measured: a 1 GB.sql.gzholding about 8 GB of SQL costs about 15-30 s of inflate and 8 GB of disk writes before Import means anything.
Cause
Export:
- When
compressWithGzipis on, the writer targets a temp.sqlin the temporary directory (apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift:103-111), and split is turned off with a warning (:113-118). - After the data phase,
SQLExportCompressor.compressruns (SQLExportPlugin.swift:221-226,apple/Plugins/SQLExportPlugin/SQLExportCompressor.swift:16-52). It callsGzipFile.compress, which reads the temp file back and deflates it (apple/Packages/TableProCore/Sources/TableProGzip/GzipFile.swift:61-84). - A streaming
GzipDeflaterwithdeflateandfinishalready exists (apple/Packages/TableProCore/Sources/TableProGzip/GzipDeflater.swift:25,:54,:70). The writer writes each statement straight to aFileHandle(apple/Plugins/SQLExportPlugin/SQLExportFileWriter.swift:91-102) and rotates parts in one place (:163).
Import:
ImportDialogawaitsFileDecompressor.decompressIfNeededon file pick (apple/TablePro/Views/Import/ImportDialog.swift:408). That inflates the whole file to a temp file (apple/TablePro/Core/Utilities/File/FileDecompressor.swift:20-27) before the 5 MB preview is read (ImportDialog.swift:437-438).- It then starts
ImportStatementCount.count(ImportDialog.swift:465-468,:566-583), which runsSQLFileParser.countStatementsover the whole file (apple/Packages/TableProCore/Sources/TableProSQLFile/SQLFileParser.swift:676). The count only feeds a label and the progress total (knownStatementCount,ImportDialog.swift:523). - The import reuses the temp file (
ImportDialog.swift:505-523,apple/TablePro/Core/Plugins/SqlFileImportSource.swift:132-150) and parses it again. - The parser can only read a file through a
FileHandle(SQLFileParser.swift:1033-1037). A streamingGzipInflateralready exists (apple/Packages/TableProCore/Sources/TableProGzip/GzipInflater.swift:30,:62). The import source already reports bytes read against file size (SqlFileImportSource.swift:63).
Change
Export:
- Give
SQLExportFileWriteran output sink that deflates as it writes, usingGzipDeflater(container: .gzip), behind the existing atomic temp-file pair. The temp.sqlandSQLExportCompressor's second pass go. - Each split part is its own gzip file, finished at rotation. Split and gzip then work together, and the "split size was not applied" warning goes.
- Deflate on its own serial queue fed by a bounded buffer, so compression overlaps the database read instead of following it.
Import:
- Let the parser read from a byte source: the file itself, or a
GzipInflaterover the file. Nothing is inflated to disk. - Build the preview from the first 5 MB of inflated text.
- Drop the up-front statement count. Drive progress by compressed bytes read for
.gzand by bytes read for.sql, throughreportsReading. Show the statement count as a running number. - The disk-space budget for inflating to a temp file (
GzipFile.swift:12-24) is no longer needed on this path. The parser's statement size limit still bounds memory.
The batch-separator pre-scan for SQL Server grammar is #4555's.
Measure
- Export a 1M-row and a 10M-row PostgreSQL table as
.sql.gz. Record wall time and bytes written withfs_usageor Instruments File Activity. Before: two uncompressed-size writes and one uncompressed-size read. After: only compressed bytes written, and wall time close to the uncompressed export. - Export with gzip and a split size: every part decompresses on its own, and the parts restore in order.
- Import a 1 GB
.sql.gz: time from file pick to the preview, time from Import to the first statement, total wall time, and peak temp directory size (duon the temporary directory). After: no temp.sql, and nocountStatementssamples in Time Profiler. - The round trip test
apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift:32("A gzipped SQL dump decompresses to the whole dump and restores every row") keeps passing, with a split-and-gzip case added.
Part of the performance plan.
Activity