简体   繁体   English

SQL 数据库 - Hash 多个主键

[英]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.我想问你在什么情况下,hash SQL 表的主键(如果你有多个)是一个不错的选择。 (Add the primary key columns as normal columns and add a single primary key column, containing a hash above all primary keys)? (将主键列添加为普通列并添加单个主键列,在所有主键之上包含一个 hash)? Is there any improvement in performance or can a good SQL implementation do it by itself?性能是否有任何改进,或者一个好的 SQL 实现可以自己做到吗?

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.首先,并非所有数据库都支持 hash 索引,因此您的问题是高度特定于数据库的。 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.其次,即使在支持的数据库中,主键也可能不支持 hash 索引。

The standard B-Tree index used in databases is quite sufficient for primary keys.数据库中使用的标准 B-Tree 索引对于主键来说已经足够了。 A hash index differs from a B-Tree in some important ways. hash 索引在某些重要方面与 B 树不同。 But notably, hash indexes only support equality operations, and not inequalities.但值得注意的是,hash 索引只支持相等运算,不支持不等式。

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). hash 索引更接近于 O(1) 查找时间(但不要忘记 hash 表可能会发生冲突并且可能需要溢出到磁盘上)。 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.这比 B-Tree 的 O(log n) 时间稍微好一点——但对数非常小,即使在数百万或数十亿行数据上也是如此。

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.如果我要创建一个合理的用例,我将从一个大规模并行分布式数据库开始,其中主键也将用于分发数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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