BDB,如何按指定顺序获取主键?
我在oracle论坛上问了同样的问题好几天了,但没有答案:( 链接是:http://forums.oracle.com/forums/thread.jspa?threadID=2162345&tstart=0
大家好,我搭建BBS使用BDB作为后端数据库,论坛数据库、主题数据库和帖子数据库共享一个环境。这个BBS Web应用程序是多线程程序。如果用户选择一个论坛,则其主题将按照最后回复时间的顺序列出;选择一个主题,帖子也会按照回复时间的顺序列出。
struct forum {
UInt16 forumID;
string forumName;
string _lastPoster; // who is the last one replied in this forum
};
struct topic {
UInt32 topicID;
UInt16 forumID; // topic comes from this forum
string title; // topic title
UInt64 dateOfLastReply; // when last reply to this topic happen
};
struct post {
UInt64 postID;
UInt32 topicID; // post comes from this topic
string title; // post title as of topic
UInt64 dateOfPost; // when this post is created
};
我为主题创建一个主数据库和两个辅助数据库,主键是topicID,辅助键分别是forumID和dateOfLastReply,我想在第一个浏览器页面上按最新回复时间顺序显示第1个25个主题,在第2个浏览器页面上显示第2个25个主题浏览器页面等,
如果使用 SQL,则为: SELECT topicID FROM topic WHERE forumID=xx ORDER BY dateOfLastReply DESC
从性能角度来看,我想获取同一个论坛的所有主题id,并且需要它们按照回复时间顺序出现,然后根据返回的topicID逐一检索主题,我该怎么做这?我想我必须使用连接。 另外,考虑到每次浏览器想要请求下一页(即该论坛的第 2 个 25 个主题)时都会进行主题检索,您对检索性能有什么建议吗? DB_DBT_MULTIPLE 对我有帮助吗?
谢谢。
I have asked the same question for some days on oracle forums, but no answer came:(
link is:http://forums.oracle.com/forums/thread.jspa?threadID=2162345&tstart=0
Hi, I build BBS using BDB as backend database, forum database, topic database and post database share one environment. This BBS web application is multi-thread program. If user selects one forum, its topics will be listed in order of last reply time;selecting one topic, posts are listed in order of reply time as well.
struct forum {
UInt16 forumID;
string forumName;
string _lastPoster; // who is the last one replied in this forum
};
struct topic {
UInt32 topicID;
UInt16 forumID; // topic comes from this forum
string title; // topic title
UInt64 dateOfLastReply; // when last reply to this topic happen
};
struct post {
UInt64 postID;
UInt32 topicID; // post comes from this topic
string title; // post title as of topic
UInt64 dateOfPost; // when this post is created
};
I create one primary database and two secondary databases for topic, primary key is topicID, secondary keys are forumID and dateOfLastReply respectively, and I want to show 1st 25 topics in latest reply time order on the 1st browser page, 2nd 25 topics on the 2nd browser page, and etc.
if using SQL, it will be: SELECT topicID FROM topic WHERE forumID=xx ORDER BY dateOfLastReply DESC
From performance perspective, I want get all topics id of one same forum, and need them come in reply time order, then retrieve topic one by one based on returned topicID, how can I do this? guess I have to use joins.
Plus, do you have any suggestion about retrieval performance given the fact that topics retrieval will happen each time browser want to request the next page, that is, 2nd 25 topics of this forum?
Is DB_DBT_MULTIPLE helpful to me?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要创建一个具有复合键的辅助数据库,该复合键由 (forum_id, dateoflastreply) 组成。然后,您想要的所有结果都将位于连续的行范围内,您可以通过执行范围扫描来查询它们(例如,以 (1, 2010-01-01) 开头的 20 行)。
You need to create a secondary database with a composite key, consisting of (forum_id, dateoflastreply). Then, all the results you want will be in a contiguous range of rows, and you can query on them by doing a range scan (eg, 20 rows starting with (1, 2010-01-01)).
您是否使用 BDB 的新 SQL 访问层?如果是这样,只需使用 SQL。
如果不是,BDB(大部分)只是一个键值数据存储。它有多种访问方法(哈希、B+树等),但其核心仅此而已。如果您想按非主键排序/搜索,则必须在这些值上创建索引,并在自己的代码中进行搜索/连接。
Are you using BDB's new SQL access layers? If so, just use SQL.
If not, BDB is (mostly) just a key-value datastore. It has several access methods (hash, B+tree, etc), but at its core thats all it is. If you want to sort/search by non-primary keys, you'll have to create indexes on those values, and do the searching/joining in your own code.
假设 topic_Secondary 数据库(包含辅助键)是通过 DB_ASSOCIATE 定义的,那么您将在 topic_Secondary 数据库上打开一个游标,并使用 DBC->get() 和 DB_SET_RANGE 将游标定位在所需的 forumID 上并移动光标从那里向前。
通过在辅助数据库上使用光标,您可以按所需的顺序获取从主数据库返回的数据。通过在初始 DBC->get() 中使用 DB_SET_RANGE 指定 forumID,您可以将光标定位在适当的位置以开始扫描。
请参阅有关光标和二级索引。
如果这能回答您的问题,请告诉我。
问候,
戴夫
Assuming that the topic_secondary database (which contains the secondary keys) is defined via DB_ASSOCIATE, then you would open a cursor on the topic_secondary database and using DBC->get() with DB_SET_RANGE you would position the cursor on the desired forumID and move the cursor forward from there.
By using a cursor over the secondary database, you get the data returned from the primary database in the desired order. And by specifying the forumID in the initial DBC->get() with DB_SET_RANGE, you can position the cursor at the proper place to begin your scan.
Please see the documentation on Cursors and Secondary indexes.
Please let me know if this answers your question.
Regards,
Dave