search

Home  >  Q&A  >  body text

node.js - leveldb排序问题

有没有人使用过leveldb?我是在nodejs环境下使用的https://www.npmjs.com/package...

网上搜了资料,感觉还是挺少。更不用说用的人了。
不过没有关系,即使没用过,我下面的问题也可以看看。

我现在面临的问题就是。测试数据一百万左右。如果单独根据key查找,那是非常快速的。

但是我现在需要完成类似于这样的功能


    where name='a' order by dateline desc limit 100,50

无论怎样,你都必须得从数据库里面读取完所有的记录,然后再进行排序,然后再截取对应的数据段。

然而,光遍历读取所有的记录,100万条数据,花了4分钟,这个速度明显不能用于实际生产了。

我不明白mysql这种100万条数据,实现上面的sql应该是不到1秒的时间吧?他们到底怎么弄的。
有什么思路吗?

ringa_leeringa_lee2780 days ago609

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:01:18

    I think the questioner misunderstood how the database works. Databases often use a balanced tree data structure to organize data, instead of simply storing an array and sorting the array when using it, and finding the data order of dateline from desc 100 to 150 (I may have misunderstood this limit), it does not read all the data, but searches the balanced tree to find the leftmost subtree with a size of 150, and then from this subtree Find the node name = 'a' in the right subtree, and then take out the data of this node. This is the simple principle of the database. In fact, the database will store part of the data in the external storage device, and then establish the 一条 index of the external storage area in the memory. If the search is looking for this index, it will The data on the external storage device is read into the memory, a balanced tree is built, and the search is performed on this balanced tree. This is an implementation of a database that organizes data.

    The execution time of this query statement in mysql is very short, but if it is to obtain all the data, it will also take a long time.

    Finally, I still don’t understand the function implemented by the questioner. What I don’t understand is: 1. Is the data read from the database or just stored in the file? 2. If it is necessary to traverse all the data For example: all data must be displayed or all data must be stored in some other file. In fact, the bottleneck that limits the speed is the programming language, operating system, etc.

    reply
    0
  • Cancelreply