116 points · by hankchinaski · 3y ago · 73 comments
The problem with row based database structures is that the data between any two rows can vary in length, so if scanning across rows on disk for a column's value, you need metadata that points at the start of each column. That means the process of scanning across the data on disk involves a _lot_ of jumping around; less of a problem on SSDs than it was on spinning platters, but still work.
The alternate approach is to store columns together on disk. That means that when performing a query that filters based on values in a given column, you can scan sequential areas on disk, which is much much faster. Of course, if you now want to assemble rows based on your filter, you need to use the metadata about where related values are in each other column in order to assemble those rows, but hopefully you have a much smaller set of records that you need to do this over. (It's cheaper to assemble 10k matching rows across a set of columns than it is to jump across a billion unaligned columns to filter them in the first place.)
This is especially useful for reporting/analytical purposes where you want to aggregate over data from many records.
And do you run them in conjunction with your existing Postgres, install it as a plugin, or switch your entire db to something like Hydra?
Do they also have a benefit similar to the compression that comes from record re-ordering in column databases?
It's not just that you need to read a smaller section of disk to scan the same amount of interesting data - it's that that data also takes up considerably less space than it otherwise would.
In columnar databases a batch of column data is stored sequentially followed by the next column and so on. So if you need to sum a column you can read it sequentially very quickly and sum it up without needing to touch the data of any other column.
Columnar is also better at filtering and grouping for similar reasons.
As a general rule if your workload looks like “select sum(x) where y group by X” a columnar db will be a great fit.
The trade off is that columnar databases don’t like being updated and work best when you only insert data. This is because when you update existing data you now need to rewrite several pages on disk because the columns are all over the place.
Because of this there’s very few areas where a pure columnar DB makes sense. They’re great for timeseries metrics like sensor data , click event logs etc. places where you are just recording a fact and don’t need to update past events.
This is how many aggregate functions can run real fast.
To take advantage of that, data has to be structured a certain way: In a columnar way (which is then be vectorized and pushed to SIMD).
1. No indexes or primary keys
2. Insert and delete only, no updates
3. Automatically normalized behind the scenes leading to data compression in the 10x-100x range
4. Very fast analytical queries
BigQuery, RedShift, etc are columnar databases (for example).
You can convert a table in SQL Server to "Clustered ColumnStore" storage, it'll become compressed, but everything will mostly work the same.
The implementation in SQL Server uses a row "delta store" to keep recent changes. Once a certain threshold is reached, the delta store is converted to columnar format and merged with the underlying table.
It's like a database engine that uses grep under the hood.
Data is in flat files. Queries are executed by scanning those files (as quickly as possible).
For aggregate queries (which is what these things are used for), substitute awk for grep.
https://benchmark.clickhouse.com/#eyJzeXN0ZW0iOnsiQXRoZW5hIC...
shared_buffers = 128MB
max_parallel_workers = 8
max_parallel_workers_per_gather = 2
work_mem = 4MB
"Postgres (tuned)" uses: shared_buffers = 8GB (64x the default)
max_parallel_workers = 16
max_parallel_workers_per_gather = 8
work_mem = 4MB
"Hydra" uses: shared_buffers = 8GB
max_parallel_workers = 16
max_worker_processes = 32 (default 8)
work_mem = 64MB
The default settings are not realistic when running on a c6a.4xlarge instance, and the "Postgres (tuned)" settings more closely match what "Hydra" is configured to use.I consider it dishonest because I do not believe a team working this close to Postgres internals wouldn't know this.
basically larger buffers and more concurrent workers.
* Citus (cstore_fwd) extension
* Azure CosmosDB for PostgreSQL
* GCP AlloyDB for PostgreSQL
* AWS Redshift (but very old Postgres base)
Fwiw, the main use case we had in mind when developing the Citus columnar table access method was compression of old data in a time-partitioned table. Citus is commonly used for real-time analytics on time series data. That involves more materialization than typical OLAP reporting use cases. However, you might still want to keep the source data in the database for ad-hoc queries or future materialization and it's preferable to keep the source data in compressed form.
Redshift is specifically architected for ad-hoc OLAP queries. AlloyDB I'm not sure.
I am wondering why they are saying it is not for OLAP workload..
Another example is cassandra is not column oriented.