Anda di halaman 1dari 5


stores have many advantages for analytical queries, such as highly optimized
compression and fast table scans. Relative to row stores, they tend to suffer on workloads that
require selective filters, as column stores cannot leverage index seeks. Column stores are
primarily optimized for queries that scan most or all of a table.

Compression and column pruning vastly reduce disk IO, and processing can be optimized with
vectorized execution, code generation, and by running operations directly on compressed data.

With the addition of a sort order, often called a sorted projection, several new patterns of
optimized queries are enabled.

Operations on small key ranges, such as SQL BETWEEN queries, can be executed with low
variance latencies. In many cases, after examination of memory-pinned file metadata, only a
single disk IO will be required to service a query. Heavier queries can also be significantly
improved - for example, two large tables could be joined with a merge join algorithm, or a
distributed grouping operation could be performed in a streaming manner and with constant
space overhead.

MemSQL's column store maintains groups of rows in an order that allows for fast seeks and can
be efficiently maintained during concurrent data ingestion. It leverages snapshot isolation level
to reorder rows in a background thread with constant disk and memory overhead, and without
interfering with concurrent read queries. It uses an optimistic reordering technique that does
not interfere with concurrent updates and deletes. And finally, it enables a wide range of
workloads which depend on data being visible to read queries immediately after ingest, and
which have traditionally been unattainable for column store engines.


In a column store, each column of the source table is stored separately, to allow independent
compression and query access. To allow flexibility in implementation, as well as amortization of
costs such as disk seeks, tables are also partitioned horizontally into subsets of rows which we
call segments. Each row belongs to exactly one segment. Each segment will typically contain
between tens of thousands and millions of rows, depending on the details of the workload.

For each segment, several items are maintained. There is a file on disk per column of the table,
storing the appropriate values. There is also a metadata representation of the segment, held in
a durable in-memory data structure (in fact, a MemSQL row-store table). The metadata
representation stores the file locations and a bit vector marking logically deleted rows. It also
stores optimization-related information, including encoding schemes and the maximum and
minimum projection key values represented in the segment.

With this representation, inserts and deletes are straightforward. Inserts always create a new
segment (or multiple new segments, for large inserts), while deletes update the metadata
representation to mark rows as logically deleted. Updates are implemented as deletes followed
by inserts (within a single transaction).


Projections have a purpose analogous to that of a row store table's indexes. They allow seeking
to a particular key value or key range in order to optimize query performance, though only to
the granularity of a single segment. They can also be leveraged to provide ordered iteration of
rows. Query optimization and execution techniques can leverage the projection sort order both
internally within a single segment, and externally across multiple segments. Internal sorting is
supported by majority of column store databases. easy to maintain as the data is inserted or
modified, but it also provides only a limited value to the optimizer and the query execution
engine. In many cases the cost of going to disk and decompressing overshadows any savings
from intra-segment seeking. External sorting, and, in particular, maintaining all the rows in a
perfectly sorted order, is a harder problem.

Segment Elimination

To enable segment elimination, we store the minimum and maximum key value in the per-
segment metadata. Note that it is not necessary to maintain these values in the presence of
deletes (and thus updates) - segment elimination is a safe heuristic.

A complementary approach, which we term full enclosure, is also available. If a query predicate
can be seen to be true for all values in a segment, certain aggregates can be computed directly
from metadata.

Sorted Runs
Sorted runs are the foundation upon which the MemSQL column store implements projections.
A sorted run is an ordered set of segments such that the minimum value of every segment in
the set is larger or equal to the maximum element of the preceding segment in the set. It is
desirable to limit the number of sorted runs that the segments form. The smaller the number of
sorted runs, the fewer segments that need to be opened to process a query that seeks for a
single value, or a query with a selective filter in general.
Certain kinds of data are naturally distributed in such a way that the number of sorted runs will
be very low. Most commonly, if the projection is sorted on a time-based column, and rows are
created and inserted in roughly chronological order, segments will partition into very few runs
even without reordering.

However, for most of the columns the distribution does not naturally align with the order in
which the data is inserted, and there will be almost as many sorted runs as there are individual
segments, making it impossible to effectively perform segment elimination. To address this, the
MemSQL column store engine maintains one large sorted run that has at least half the rows in
the table, another sorted run that has at least half of the remaining rows, and so forth. With
this structuring, the number of sorted runs does not exceed log s, where s is the total number
of segments. We also have the property that ingesting a new batch of rows will only require
amortized k log s time to update the order, where the new batch comprises k total segments.
This idea is similar to that of Log Structured Merger (LSM) trees.

There is a tradeoff between the constant factor in the data ingestion performance and constant
factor in performance of reads that can leverage segment elimination. A higher multiplier
makes data ingestion slower, but at the same time improves the performance of the reads.

The addition of new segments due to inserts and updates disturbs the invariant of
exponentially decaying sorted runs.

To restore the invariant, it becomes necessary to reorder and repartition rows across segments.
Because segments are sorted internally as well as externally, merge sort is a natural way to
redistribute rows across segments. In order to maintain the exponentially decaying sorted runs,
we perform several steps on insert. Each time a new batch of data is inserted it is fully sorted
internally before the transaction completes. Due to this, the newly introduced rows form at
most one additional sorted run, even if they correspond to multiple segments. It is possible to
closely estimate how many segments will result from merging sorted runs. Our implementation
inspects the segment metadata for present and deleted row counts to determine the length of
the resulting run. This information is used to collapse multiple iterations of merging into a
single round in cases where a small trailing run would be produced and then immediately enter
the merging process again - instead we merge with the additional run immediately.

Reads During Sorting

In order to allow reads while merging is underway, it is important that the reads have a
consistent view of the data. In particular, new segments produced by the merge cannot be
immediately made visible to concurrent reads. If a concurrent read sees the new segment, it
may either encounter duplicate rows (from another segment that was partially consumed by
the merge to produce the new segment), or may miss rows ifthe partially consumed segment is
relocated in the metadata index and thus missed entirely. These issues are similar to those that
could be observed due to a concurrent update in a READ COMMITTED isolation level
transaction - and indeed, one way to look at merges is as an UPDATE that happens to not
change any column values. However because these update-like operations are generated
internally by the engine's background process, instead of by explicit queries in the user
workload, it is important that they cause no anomalies.

The approach that we use in MemSQL column store engine is to commit immediately as a new
segment is written, and mark all the rows in the input segments that were already moved to
the new segments as deleted as part of the same transaction. If some segment was completely
saturated as part of a merge step, it is deleted from the metadata entirely, and all its files are
erased from disk, as part of the transaction that writes the new segment onto disk. Because
reads are served with SNAPSHOT isolation, each query will see the correct set of rows.

Writing during sorting

MemSQL uses optimistic concurrency control. Merging does not take lock on segments as it
processes them. Locks are taken only when a transaction is ready to be committed. Every time
the merger is ready to write a new segment to disk it verifies that all the input segments that
contributed to that segment still exist and that all the rows that were merged in have not been
deleted by concurrent writes. If this is not the case, it rolls back all iterators to the positions
they were before the last output segment was populated, and discards the new segment. If any
input segment that the iterators are currently pointing at was completely deleted, the
corresponding iterator simply advances to the next segment in its sorted run. If a subset of the
rows in a segment were deleted then the new output segment can be regenerated, skipping the
deleted rows on the second pass.

We assume that writes proceed at approximately the same speed as the merge. New data
being ingested does not interfere with the merger because the merger operates on a snapshot
of the metadata. Updates which affect many consecutive rows have a low probability of
interfering with the merger because the time it takes to update one segment's worth of rows is
comparable to time it takes for merger to produce a new segment.

Deletes and updates of individual or well- distributed rows need to be handled separately. In
the presence of queries that update few rows across many segments, the chance of modifying
one of the segments that is being merged is very high and this can result in starvation of the
merger if it retries each time. To address the problem we optimize this case to track exactly
which rows were merged into the new output segment. As we commit we check if any of them
were deleted. If any were concurrently deleted then in place of discarding the segment and
starting over, we mark those rows as deleted in the output segment before writing it to disk.
Newly created segments can therefore already contain tombstoned rows. We only trigger this
optimization if the fraction of concurrently deleted rows falls below a certain threshold because
introducing segments with a significant fraction of deleted rows negatively impacts both disk
usage and query performance. The value of the threshold is a tradeoff. Higher values will result
in higher disk usage and some penalty on select but will provide faster merges since fewer
restarts will be necessary. Lower values will result in lower disk usage but will slow down

Concurrent Merges

It is desirable for multiple merges to occur simultaneously on the same set of segments. For
example, while two very large sorted runs are being merged, it might be desirable to merge
several smaller sorted runs to get immediate performance benefits. If two merges operate on
two disjoint sets of sorted runs, then they will not interfere with each other in any way, which
allows the introduction of concurrent merges.

A critical observations is that merging two large sorted runs and merging two small sorted runs
result in the same expected improvement in performance of the read queries that can leverage
sorted order. We have two background mergers - the Fast Merger and the Slow Merger, which
are distinguished by which size runs they process. The Fast Merger processes only runs below a
certain small cutoff (in MemSQL, this bound is set to the length of ten segments), while the
Slow Merger exclusively processes runs at or above that threshold. Since the fast merger only
merges small sorted runs it never gets stuck on an expensive operation. This way when a new
batch of data is inserted, if a very small sorted run is created that can be quickly merged into
another existing sorted run, the fast merger will be available to do that with very low latency.
The slow merger, on the other hand, can work on merging large sorted runs for a long time
without any regression in performance of concurrent queries.

When a new batch of data is inserted, unless the new rows are tightly clustered with respect to
the sort key, then it is unlikely that any of the segments this batch introduces will naturally fit
into any of the existing sorted runs. It is also unlikely that any two of the new segments will
form a sorted run on their own. A new batch consisting of N segments is likely to introduce N
new sorted runs, which will immediately be picked up by the fast merger. Until the fast merger
processes them, the performance of read queries with high selectivity will be degraded
significantly. For example, if the new batch introduced 10 new segments, and the number of
sorted runs is 8 (log8 N for N of around 100 millions), then the performance of the selects will
degrade by a factor of two until the fast merger finishes with them. Because of these two
considerations, we sort the entire new batch of data before the transaction commits and the
data is made visible.

Column stores were originally designed with bulk loading of data in mind. Our experience
indicates that there are many practical use cases for column store where it is to insert small
chunks of data at a time, with batch sizes as low as a single row. In the context of a distributed
database system, this challenge can be significantly amplified. If rows are hashed and
distributed among many partitions on different machines, then each individual
partition may receive input batch sizes two or three orders of magnitude smaller than the
application initially provided, which are negligibly small compared to the full segment size.

In our implementation we use our row store data structure, namely a lock-free skip list, in front
of the column store. Each time a write query attempts to insert less than a full segment of rows,
those rows are inserted into that skip list instead. As soon as the skip list has enough rows for a
full segment, that segment is written to disk and the rows are deleted from the skip list in a
single transaction. From the perspective of the read queries such a skip list is just an extra
segment, indistinguishable from a segment that is actually stored on disk. The skip list naturally
stores data sorted, so this extra segment is also always sorted.