Junior
What types of indexes exist in databases?
sobes.tech AI
Answer from AI
Main types of indexes in databases:
-
B-tree (B+tree):
- The most common type.
- Suitable for a wide range of operations: range searches, exact matches, sorting.
- Used by default in many DBMSs (MySQL, PostgreSQL, Oracle).
-
Hash indexes:
- Based on hash tables.
- Very fast for exact match searches (
=). - Not suitable for range searches (
BETWEEN,<,>), sorting (ORDER BY), or pattern matching (LIKE).
-
Full-text indexes:
- Designed for text searches in text fields (BLOB, TEXT).
- Allow searching for words or phrases, considering synonyms, and performing morphological analysis.
- Examples:
MATCH...AGAINSTin MySQL/MariaDB,tsvector/tsqueryin PostgreSQL.
-
Spatial indexes (R-tree):
- Used for indexing geographic or spatial data.
- Enable efficient finding of objects within a certain area or bounds.
-
Bitmap indexes:
- Used in some DBMSs (e.g., Oracle, PostgreSQL).
- Suitable for fields with low cardinality (few unique values, e.g., gender, status: active/inactive).
- Can be very efficient for complex queries with
ANDandORoperations.
-
Clustered index:
- Affects the physical order of data storage in table rows.
- Only one clustered index per table.
- Often used for primary keys.
-
Non-clustered index:
- Does not determine the physical order of rows.
- Contains pointers to the physical location of data rows.
- A table can have multiple non-clustered indexes.
-
Covering index:
- An index that contains all columns needed to execute a query, including columns in the
SELECTlist,WHEREconditions, andORDER BY. - Allows the DBMS to avoid accessing table data, speeding up query execution.
- An index that contains all columns needed to execute a query, including columns in the
-
Functional (expression-based) index:
- An index built on the result of a function or expression over one or more columns.
- Allows indexing of computed values.
When choosing an index type, consider data type, query frequency and nature (exact match, range, sorting, full-text search, etc.), and column cardinality.