In OLTP (transactional processing) systems, storage engines generally fall into two main architectures:
Log-structured approach: This approach only allows appending data; old data is deleted, but files that have already been written are never modified. Common storage engines such as SSTable, LSM trees, RocksDB, Cassandra, HBase, Scylla, and Lucene use this architecture. Log-structured storage engines are very efficient in write throughput.
In-place update approach: This approach treats disk as an updatable storage area, typically updating data in fixed-size pages. B-trees are the representative of this approach; they are widely used in relational OLTP databases and in some nonrelational databases. B-trees generally perform better on reads, with short response times and high read throughput.
OLAP
Columnar Storage
For OLAP (analytical processing) systems, one of the most important concepts is columnar storage. In star or snowflake schemas, fact tables can be enormous (trillions of rows, thousands of columns), and analytical queries do not touch every column each time, so the efficiency of columnar storage becomes especially important. Columnar storage not only improves query efficiency but also reduces storage costs. Column compression techniques (such as bitmap encoding) save even more space and speed up queries.
Bitmap compression: Techniques such as RLE (run-length encoding) or Roaring Bitmaps efficiently compress repeated data, further saving space and improving query speed.
One advantage of columnar storage is that the order in which rows are stored does not matter. For example, sorting by date can make sense because it keeps related data from the same day together in the same storage block. This not only improves query efficiency but also helps compression, since identical values appear consecutively in the data and are easier to compress.
Writing to Columnar Storage
Writing to columnar storage works better with object storage. Columnar data is usually written once and does not need to be modified. The distributed nature of object storage makes large batch writes more efficient, and queries do not need to worry about whether storage blocks are physically adjacent.
Query Optimization
Query Compilation & Vectorized Processing
Query optimization can also be sped up through query compilation and vectorized processing:
Sequential memory access beats random access and reduces cache misses.
Most computation happens in a tight inner loop, avoiding the overhead of function calls.
Parallel processing and SIMD (single instruction, multiple data) instructions speed things up.
Operations run directly on compressed data, avoiding decoding, which saves memory and improves efficiency.
Materialized Views & Data Cubes
In addition, materialized views and data cubes are two common query optimization approaches:
Materialized views: Query results are stored persistently (for example, on disk) to improve query performance, but the drawback is that they can become stale.
Data cubes: Aggregate functions are precomputed to speed up queries. For example, you might precompute aggregate information such as sales for a given day at a given store.
Full-Text Search
Inverted Index
For full-text search, the commonly used indexing technique is the inverted index. Traditional queries associate data rows by ID, while an inverted index associates document IDs by term (for example, a word in the text). Inverted indexes can also be stored as sparse bitmaps and queried faster with operations such as bitwise AND or OR.
Inverted index: This is a structure where terms are keys and lists of document IDs are values, allowing fast lookup of all documents containing a particular term. If a term appears in multiple documents, we can use a bitmap to represent which documents contain it, then use bitwise operations to quickly find matching documents.
Trigrams (N-grams): For queries that contain a specific character substring, trigram techniques split a string into multiple substrings (for example, "hello" can be split into "hel", "ell", and "llo"). Building an inverted index on these substrings speeds up searches for arbitrary substrings of at least three characters.
When handling spelling mistakes, Lucene can find similar words within a certain edit distance (such as adding, deleting, or substituting one letter). Lucene uses finite state automata and Levenshtein automata to handle this kind of fuzzy matching efficiently.
Vector Search
In addition, vector search is a key technique in modern semantic search, especially in RAG (retrieval-augmented generation). With vector embeddings, documents are converted into points in a multidimensional space, and queries find similar documents by comparing the similarity between vectors.
Several common vector indexing methods include:
Flat indexes: Vectors are stored as-is, and queries must calculate the distance to the query vector one by one; this is accurate but slow.
Inverted file (IVF) index: The vector space is divided into multiple clusters (centroids), reducing the number of vectors that need to be compared. Queries are faster, but results are approximate.
Hierarchical navigable small world (HNSW): This maintains the vector space through a multilayer graph structure. A query first locates an approximate region, then refines the search layer by layer to find the closest vectors.
Most of these indexing methods are approximate. Increasing the number of probes or layers can improve query accuracy, but it also increases computational complexity.
Multi-Dimensional Indexes
Multi-dimensional indexes: These allow querying multiple columns at once, and are especially useful for queries that need to filter on multiple dimensions simultaneously, such as geospatial data. For example, when searching for restaurants, you may need to filter by both latitude and longitude. A composite index cannot handle this efficiently, but a multi-dimensional index can handle multiple dimensions at once.
Space-filling curves: A way to convert two-dimensional space (such as latitude and longitude) into a one-dimensional number that can be indexed in a conventional B-tree.
R-trees and Bkd trees: Index structures designed specifically for multi-dimensional spatial data (such as geospatial data). They divide space into regions and ensure that nearby points are assigned to the same subtree, improving query efficiency. They are often used for geospatial data; for example, PostGIS uses R-tree indexes to handle spatial data.
Grid tessellation: You can also use regularly spaced triangular, square, or hexagonal grids to partition space, which helps optimize multi-dimensional data queries.
Application examples:
Geographic queries: A restaurant search system can use multi-dimensional indexes to efficiently find restaurants within a given range, avoiding the efficiency problems that can arise with composite indexes.
E-commerce sites: A three-dimensional index on product color (red, green, blue) can quickly query products in a certain color range.
Weather observation data: A two-dimensional index enables efficient queries across date and temperature dimensions, avoiding a full row-by-row scan of all records.