| Hi @hodgesrm. Yeah, good questions. We actually spent quite a bit of time on exactly these topics. What Mason is optimised for: - We've purposefully optimised Mason for BI use-cases, rather than dashboard use-cases. i.e. we wanted it to be fast for p95 of all adhoc queries thrown at it, rather than being fast for p99 of a fixed set of queries. Optimising to be fast for dashboards (where you know the shapes to expect) is very different to optimising for humans and agents throwing different queries at it every day. We wanted Mason to be ideal for the latter. Storing data for optimal performance: - i'm sure you know this, but just for context for anyone else reading this comment - OLAP (column-based) databases (like Duckdb and Clickhouse) store their data as columns, rather than rows, and each column is split into "chunks" of values (~8k rows per chunk in clickhouse, or ~122k rows per chunk in duckdb) and the primary way to make queries fast is to reduce the number of chunks that need to be processed by the engine to answer any given query. - we tested storing data in primary-key order, but what we found is that for most production postgres databases, the order in which the rows were inserted into the database aligns very closely with the time column that we usually want to filter on (usually created_at). And duckdb automatically stores min-max values for every column of every chunk, with nothing to declare. So, by storing data in duckdb in the order they're stored in postgres, most time-based filters instantly reduce the number of chunks that need to be processed down to just a few. Making it typically very fast for time-based queries. - where the above sort order falls down is for pk-based row lookups when the key is random (e.g. UUIDs), which we found are still remarkably common in BI-like query loads (usually you start by doing an aggregate, but then you want to drill down into particular rows). For these tables Mason stores a sorted pk-to-row lookup, and rewrites incoming queries to use it to find the row. This took a single-row lookup on 100M rows from ~13 seconds to ~100ms. - beyond sort-order, the other way to get more performance out of duckdb is to reduce how much data each chunk takes, so that less is read from disk and more fits in memory. This is where compression comes in, and mason will automatically sample each column, during the first sync, and choose the appropriate compression method for that column (e.g. short or repeated values get dictionary compression, long unique values get ZSTD) - a 3rd type of query that is remarkably common is aggregating by time (day / week / month / etc.) and doing so for different timezones. We found that a significant amount of the query processing time was spent converting timestamps to the appropriate granularity (especially when the query isn't for UTC). Mason now automatically stores, beside every timestamptz column, a 15-min bucketed version of that column. And Mason rewrites incoming GROUP BY time queries to use that column instead. On a sample of 50M rows, this sped up non-UTC queries by 6-7x (less once joins are involved). The column is written at insert (or update) time, so the read queries never need to pay the cost. Handling updates: - most of the production databases we work with have a mix of inserts and updates, so it was important for us that Mason handles update workloads correctly, and keeps up with them. - our decision (described above) to store data in the order they were inserted into postgres was also driven by this requirement. By storing in that order, it means that inserts can just be appended to the end of our duckdb tables, so staying in-sync with postgres is not a processing-heavy operation. - for updates in duckdb, the expensive part is usually finding the row (and chunks) to update. For tables with random keys, Mason uses the pk-to-row lookup (described above) to find the rows to update, rather than scanning the whole key column, which keeps the steady trickle of updates fast. Large batches of changes are grouped together and applied every few seconds. - the one part that is harder to keep up-to-date is the pk-to-row lookup itself. We don't write to it on every insert. Instead, rows added since it was last built are read directly from the end of the table, and the lookup is rebuilt in the background once those new rows pass 1% of the table. So lookups and updates are never delayed waiting for it. - updates do leave dead rows behind in duckdb, so Mason also watches each table and compacts it in the background when enough space is wasted, without interrupting the main flow. Schema migration: - we don't currently do anything clever here. When a postgres table is changed, Mason will automatically resync the new table in full. The previous synced data keeps serving until the new data is swapped in. |