Câu hỏi phỏng vấn SQL
Câu hỏi

How does a Hash index work?

Câu trả lời

A hash index is a type of database index that uses a hash function to map keys to specific locations, or buckets, in a hash table. This method allows for very fast data retrieval, especially for equality comparisons. Here’s a detailed explanation of how a hash index works:

How a Hash Index Works

  1. Hash Function:

    • The core component of a hash index is the hash function. This function takes an input key (e.g., a column value) and converts it into a hash value, which is typically an integer. The hash function is designed to distribute keys uniformly across the hash table to minimize collisions (situations where different keys map to the same bucket) [3][8].
  2. Buckets:

    • The hash table is divided into buckets, each of which can store one or more entries. Each bucket is identified by an index, which is the result of the hash function applied to a key. The bucket contains pointers to the actual data rows in the table [1][12].
  3. Insertion:

    • When a new record is inserted into the table, the hash function is applied to the key of the record. The resulting hash value determines the bucket where the record will be stored. If the bucket already contains entries, the new entry is added to the bucket, potentially leading to a collision [1][3].
  4. Lookup:

    • For data retrieval, the hash function is applied to the search key to find the corresponding bucket. The database system then searches within this bucket to find the exact record. This process is very efficient for equality searches because it directly maps the key to a specific bucket, typically resulting in O(1) time complexity for lookups [1][3][8].
  5. Collision Handling:

    • Collisions occur when multiple keys hash to the same bucket. Different strategies can be used to handle collisions, such as chaining (storing all entries that hash to the same bucket in a linked list) or open addressing (finding another open bucket within the table) [3][12].

Advantages and Disadvantages

Advantages:

  • Speed: Hash indexes provide very fast lookups for equality comparisons due to the direct mapping of keys to buckets [1][3][8].
  • Efficiency: They are particularly efficient for queries that involve exact matches, such as looking up a record by its primary key [1][3][8].

Disadvantages:

  • Range Queries: Hash indexes are not suitable for range queries (e.g., finding all records with values between X and Y) because the hash function does not preserve any order among the ...
middle

middle

Gợi ý câu hỏi phỏng vấn

entry

Define a Temp Table

middle

What is the difference between UNION and UNION ALL ?

middle

Discuss INNER JOIN ON vs WHERE clause (with multiple FROM tables)

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào