Index Design
Index Overview
Consider a table structured as follows:
The application frequently executes queries in this format:
Without an index, the system must scan the entire test1 table row by row to find matching entries. This method is inefficient if test1 contains many rows but only returns a few (or none). By creating an index on the id column, the system can use a more efficient strategy to locate matching rows, potentially requiring only a few IO operations.
To create an index on the id column, use the following command:
Index Type
Three index types are available: B-Tree, GIN, and SPLIT_BITMAP. Index names must be unique within the schema. Each index type uses a distinct algorithm and is optimized for different queries. By default, the CREATE INDEX
command generates a B-Tree index. To create a different index type, specify the desired type after the keyword USING
. For example, to create a SPLIT_BITMAP index:
B-Tree
B-trees handle equality and range queries on sortable data. The following two statements for index creation are equivalent.
The Tacnode query optimizer considers B-tree indexes when the indexed columns are compared using any of the following operators:
Bitmap
Columnar storage tables use bitmap indexes, a database structure designed to enhance query performance, especially for columns with low cardinality and few distinct values.
Starting with version V1.0, the engine automatically generates adaptive Bitmap indexes for text types (TEXT/CHAR/VARCHAR/BPCHAR), date-time types (DATE), and enumeration types (ENUM). Manually creating bitmap indexes is no longer necessary. The engine determines how to produce the index file based on data characteristics, and users can adjust the adaptive behavior by altering parameters.
GIN
The Generalized Inverted Index (GIN) is an "inverted index" suitable for data with multiple combined values.
Tacnode enables index queries through these operators:
Locate products labeled as 'furniture':
Multi-column Index
An index, which can be either a B-Tree or a bitmap index, may be created on several table columns. For instance, consider a table structured as follows:
Examples include queries like:
In this case, it would be advisable to create an index on both the major and minor columns together, as shown:
It's essential to recognize that the column order in a B-Tree index greatly influences index lookup efficiency. However, this is only valid if a constraint exists on the leading (leftmost) column. Conversely, the column order in a bitmap index does not impact performance. Note that with bitmap indexes, the effect of defining a combined index is the same as defining each column separately; the difference lies in the method of writing.
Concurrent Index Building
Creating indexes can disrupt the database's regular functioning. Typically, Tacnode locks the table intended for indexing, preventing both reads and writes and completes the entire index build in one table scan. This can have significant consequences if the system is a live production database.
Tacnode allows index creation without blocking writes. To activate this feature, use the CONCURRENTLY option in the CREATE INDEX command.
Best Practices
- Choose the appropriate column:
- Generate indexes for columns commonly found in WHERE clauses, JOIN conditions, and ORDER BY or GROUP BY clauses.
- Consider implementing B-Tree indexes on high cardinality columns (those with a wide variety of values) to assist in filtering and sorting.
- For filtering purposes, consider bitmap indexes for low cardinality columns (those with many repeated values).
- Avoid over-indexing:
- Do not create indexes on columns that are seldom or never queried.
- Create indexes only when query performance requires enhancement.
- Avoid duplicate indexes like this:
- Consider the size of indexes:
- Indexes consume storage space, and large ones can affect disk I/O and writing performance.
- Weigh the advantages of indexes against your database's storage constraints.
Example
Creating a B-tree index
Creating a split_bitmap index
Note: split_bitmap indexes do not apply to row tables.