Sobes.tech
Junior

What types of indexes exist in databases?

sobes.tech AI

Answer from AI

Main types of indexes in databases:

  1. 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).
  2. 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).
  3. 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...AGAINST in MySQL/MariaDB, tsvector/tsquery in PostgreSQL.
  4. Spatial indexes (R-tree):

    • Used for indexing geographic or spatial data.
    • Enable efficient finding of objects within a certain area or bounds.
  5. 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 AND and OR operations.
  6. Clustered index:

    • Affects the physical order of data storage in table rows.
    • Only one clustered index per table.
    • Often used for primary keys.
  7. 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.
  8. Covering index:

    • An index that contains all columns needed to execute a query, including columns in the SELECT list, WHERE conditions, and ORDER BY.
    • Allows the DBMS to avoid accessing table data, speeding up query execution.
  9. 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.

What types of indexes exist in databases? — PHP - sobes.tech