简体   繁体   中英

SQL database - Hash multiple primary keys

I wanted to ask you in which cases it is a good choice to hash the primary keys (if you have multiple) of an SQL table. (Add the primary key columns as normal columns and add a single primary key column, containing a hash above all primary keys)? Is there any improvement in performance or can a good SQL implementation do it by itself?

If this causes a performance improvement, which instructions will be improved?

T

First, not all databases support hash indexes, so your question is highly database specific. I note this because your question has not specified a database.

Second, even in databases that do, hash indexes may not be supported for primary keys.

The standard B-Tree index used in databases is quite sufficient for primary keys. A hash index differs from a B-Tree in some important ways. But notably, hash indexes only support equality operations, and not inequalities.

A hash index comes closer to having O(1) lookup time (but don't forget that a hash table can have collisions and might need to spill on disk). This is marginally better than O(log n) time for a B-Tree -- but logarithms are pretty small, even on millions or billions of rows of data.

In practice, I don't see any strong reason to use a hash-index as a primary key, even if the database supports it. If I were to create a use-case where it might be reasonable, I would start with a massively parallel distributed database, where the primary key would be used for distributing the data as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM