简体   繁体   English

数据库如何处理不适合内存的数据表?

[英]How do databases deal with data tables that cannot fit in memory?

Suppose you have a really large table, say a few billion unordered rows, and now you want to index it for fast lookups. 假设您有一个非常大的表,比如几十亿个无序行,现在您想要将其编入索引以便快速查找。 Or maybe you are going to bulk load it and order it on the disk with a clustered index. 或者您可能要批量加载它并在具有聚簇索引的磁盘上进行订购。 Obviously, when you get to a quantity of data this size you have to stop assuming that you can do things like sorting in memory (well, not without going to virtual memory and taking a massive performance hit). 显然,当你获得这么大的数据量时,你必须停止假设你可以做一些事情,比如在内存中进行排序(好吧,不是没有去虚拟内存并且会受到巨大的性能影响)。

Can anyone give me some clues about how databases handle large quantities of data like this under the hood? 任何人都可以给我一些关于数据库如何处理大量数据的线索吗? I'm guessing there are algorithms that use some form of smart disk caching to handle all the data but I don't know where to start. 我猜有些算法使用某种形式的智能磁盘缓存来处理所有数据,但我不知道从哪里开始。 References would be especially welcome. 参考文献特别受欢迎。 Maybe an advanced databases textbook? 也许是一本高级数据库教科书?

Multiway Merge Sort是一个用于排序大量内存的关键字

As far as I know most indexes use some form of B-trees , which do not need to have stuff in memory. 据我所知,大多数索引使用某种形式的B树 ,它们不需要在内存中有东西。 You can simply put nodes of the tree in a file, and then jump to varios position in the file. 您可以简单地将树的节点放在一个文件中,然后跳转到文件中的varios位置。 This can also be used for sorting. 这也可以用于排序。

Are you building a database engine? 你在构建数据库引擎吗?

Edit: I built a disc based database system back in the mid '90's. 编辑:我在90年代中期建立了一个基于光盘的数据库系统。

Fixed size records are the easiest to work with because your file offset for locating a record can be easily calculated as a multiple of the record size. 固定大小的记录是最容易使用的,因为用于查找记录的文件偏移量可以很容易地计算为记录大小的倍数。 I also had some with variable record sizes. 我也有一些可变记录大小。

My system needed to be optimized for reading. 我的系统需要针对阅读进行优化。 The data was actually stored on CD-ROM, so it was read-only. 数据实际存储在CD-ROM上,因此它是只读的。 I created binary search tree files for each column I wanted to search on. 我为我想要搜索的每个列创建了二叉搜索树文件。 I took an open source in-memory binary search tree implementation and converted it to do random access of a disc file. 我采用了开源内存二进制搜索树实现,并将其转换为对光盘文件进行随机访问。 Sorted reads from each index file were easy and then reading each data record from the main data file according to the indexed order was also easy. 从每个索引文件中分类读取很容易,然后根据索引顺序从主数据文件中读取每个数据记录也很容易。 I didn't need to do any in-memory sorting and the system was way faster than any of the available RDBMS systems that would run on a client machine at the time. 我不需要进行任何内存中排序,系统比当时在客户端计算机上运行的任何可用RDBMS系统都要快。

For fixed record size data, the index can just keep track of the record number. 对于固定记录大小的数据,索引可以只跟踪记录号。 For variable length data records, the index just needs to store the offset within the file where the record starts and each record needs to begin with a structure that specifies it's length. 对于可变长度数据记录,索引只需要在记录开始的文件中存储偏移量,并且每个记录需要以指定其长度的结构开始。

You would have to partition your data set in some way. 您必须以某种方式对数据集进行分区。 Spread out each partition on a separate server's RAM. 在单独的服务器RAM上展开每个分区。 If I had a billion 32-bit int's - thats 32 GB of RAM right there. 如果我有一个十亿32位的int - 那就是32 GB的RAM。 And thats only your index. 那只是你的索引。

For low cardinality data, such as Gender (has only 2 bits - Male, Female) - you can represent each index-entry in less than a byte. 对于低基数数据,例如Gender(只有2位 - 男性,女性) - 您可以用不到一个字节表示每个索引条目。 Oracle uses a bit-map index in such cases. 在这种情况下,Oracle使用位图索引。

Hmm... Interesting question. 嗯......有趣的问题。

I think that most used database management systems using operating system mechanism for memory management, and when physical memory ends up, memory tables goes to swap. 我认为最常用的数据库管理系统使用操作系统机制进行内存管理,当物理内存结束时,内存表会进行交换。

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

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