简体   繁体   English

关系型数据库如何组织数据?

[英]How does a relational database organize data?

I was thinking that a relational database will store every possible query and the values to return for that query in a hash table.我在想关系数据库将存储每个可能的查询以及要在哈希表中为该查询返回的值。

So like, if each entry in your table had 5 attributes, then you would make a copy of that element for each subset of the 5 attributes that appear in any given query that should return that specific entry.因此,如果表中的每个条目都有 5 个属性,那么您将为出现在应返回该特定条目的任何给定查询中的 5 个属性的每个子集制作该元素的副本。 So every individual entry would appear 2^5 = 32 times in the table.因此,每个单独的条目将在表中出现 2^5 = 32 次。 This seems like it would be very memory inefficient for large data sets with many entries, but it also allows for the fastest possible query time.对于具有许多条目的大型数据集,这似乎是非常低内存效率的,但它也允许最快的查询时间。

Do real world relational-databases have a mixed version of this where some response time for queries/lookups is traded off for more memory efficiency?现实世界的关系数据库是否有一个混合版本,其中一些查询/查找的响应时间被折衷以获得更高的内存效率? If so, how would this be implemented?如果是这样,这将如何实施?

That's not how relational databases store data.这不是关系数据库存储数据的方式。 Keep in mind it's a lot more than 2^32, because you can make queries that have expressions, not simply references to attribute columns.请记住,它远不止 2^32,因为您可以使用表达式进行查询,而不仅仅是对属性列的引用。 Also queries that are joins, which expands the possibilities immensely.还有作为连接的查询,这极大地扩展了可能性。

Even if you could store all possible combinations, it would be a waste because most of them will never be needed.即使您可以存储所有可能的组合,也将是一种浪费,因为它们中的大多数永远都不需要。

Instead, databases typically store records, where a record includes all columns of one table.相反,数据库通常存储记录,其中记录包括一个表的所有列。 If you run a query that only needs some columns, the DBMS still fetches the whole record, and simply ignores columns that you didn't ask for.如果您运行一个只需要一些列的查询,DBMS 仍会获取整个记录,而只是忽略您没有要求的列。 Then it evaluates any expressions in your query.然后它评估查询中的任何表达式。 And finally returns the result set.最后返回结果集。

MySQL does not use hash tables to store these records, it uses a B+Tree data structure, so looking up a record by its primary key takes O(log n) time. MySQL 不使用哈希表来存储这些记录,它使用 B+Tree 数据结构,因此通过其主键查找记录需要 O(log n) 时间。

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

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