如何获得最后N个连接边

时间:2016-08-29 16:09:55

标签: orientdb orientdb2.2

我正在编写一个大型聊天应用程序,这是我的数据库架构:

CREATE CLASS User EXTENDS V;
CREATE PROPERTY User.name STRING;

CREATE CLASS Message EXTENDS V;
CREATE PROPERTY Message.text STRING;
CREATE PROPERTY Message.createdAt DATETIME;
CREATE INDEX Message.createdAt ON Message(createdAt) NOTUNIQUE;

CREATE CLASS Send EXTENDS E;

我正在使用轻量级边缘,我有200,000个边连接到#12:0,如下所示:

CREATE VERTEX User SET name = 'john';
/* #12:0 */

CREATE VERTEX Message SET content = 'Lorem ipsum dolor sit amet', createdAt = SYSDATE();
/* #20:0 */

CREATE EDGE Send FROM #12:0 TO #20:0

我想获取连接到#12:0的最后5条消息。我试过这些问题:

查询1:

SELECT FROM (
  SELECT EXPAND(OUT('Send')) FROM #12:0
) ORDER BY createdAt DESC LIMIT 5

耗时~2s

“解释”结果:

{
    "result": [
        {
            "@type": "d",
            "@version": 0,
            "documentReads": 200000,
            "current": "#19:66661",
            "recordReads": 200000,
            "fetchingFromTargetElapsed": 377,
            "expandElapsed": 0,
            "orderByElapsed": 5,
            "evaluated": 200000,
            "elapsed": 2416.7283,
            "resultType": "collection",
            "resultSize": 5,
            "@fieldTypes": "documentReads=l,current=x,recordReads=l,fetchingFromTargetElapsed=l,expandElapsed=l,orderByElapsed=l,evaluated=l,elapsed=f"
        }
    ],
    "notification": "Query executed in 2.427 sec. Returned 1 record(s)"
}

查询2:

SELECT OUT('Send')[199994-199999] FROM #12:0

需要~6s

“解释”结果:

{
    "result": [
        {
            "@type": "d",
            "@version": 0,
            "documentReads": 1,
            "current": "#12:0",
            "recordReads": 1,
            "optimizationElapsed": 0,
            "fetchingFromTargetElapsed": 8749,
            "evaluated": 1,
            "elapsed": 8749.445,
            "resultType": "collection",
            "resultSize": 1,
            "@fieldTypes": "documentReads=l,current=x,recordReads=l,optimizationElapsed=l,fetchingFromTargetElapsed=l,evaluated=l,elapsed=f"
        }
    ],
    "notification": "Query executed in 8.759 sec. Returned 1 record(s)"
}

有没有更快的方法呢?

请不要参考chat use case ...

我正在使用orientdb 2.2.7

1 个答案:

答案 0 :(得分:0)

最后!我找到了一种快速的方法。

我已将我的数据库架构更改为:

CREATE CLASS User EXTENDS V;
CREATE PROPERTY User.name STRING;

CREATE CLASS Message;
CREATE PROPERTY Message.text STRING;
CREATE PROPERTY Message.writer LINK User;
CREATE PROPERTY Message.createdAt DATETIME;
CREATE INDEX Message.writer ON Message(writer) NOTUNIQUE_HASH_INDEX;
CREATE INDEX Message.createdAt ON Message(createdAt) NOTUNIQUE;

我正在使用Message.writer来连接User而不是使用边缘。

<强>查询:

SELECT FROM Message
WHERE createdAt < sysdate() AND writer = #12:0
ORDER BY createdAt DESC
SKIP 10 LIMIT 5

这需要150万条记录大约30毫秒!

重要提示:请注意createdAt < sysdate()子句中的WHERE, 你必须在ORDER BY字段上写一个虚拟条件,这会使ORDER BY使用createdAt索引(快〜700毫秒)

“解释”结果(使用createdAt < sysdate()):

{
    "result": [
        {
            "@type": "d",
            "@version": 0,
            "documentReads": 252,
            "fullySortedByIndex": true,
            "documentAnalyzedCompatibleClass": 252,
            "recordReads": 252,
            "fetchingFromTargetElapsed": 6,
            "indexIsUsedInOrderBy": true,
            "compositeIndexUsed": 1,
            "current": "#49:1493348",
            "involvedIndexes": [
                "Message.createdAt"
            ],
            "limit": 5,
            "evaluated": 252,
            "elapsed": 6.447579,
            "resultType": "collection",
            "resultSize": 5,
            "@fieldTypes": "documentReads=l,documentAnalyzedCompatibleClass=l,recordReads=l,fetchingFromTargetElapsed=l,compositeIndexUsed=l,current=x,involvedIndexes=e,evaluated=l,user=x,elapsed=f"
        }
    ],
    "notification": "Query executed in 0.032 sec. Returned 1 record(s)"
}

“解释”结果(没有createdAt < sysdate()):

{
    "result": [
        {
            "@type": "d",
            "@version": 0,
            "documentReads": 48512,
            "fullySortedByIndex": false,
            "documentAnalyzedCompatibleClass": 48512,
            "recordReads": 48512,
            "fetchingFromTargetElapsed": 801,
            "indexIsUsedInOrderBy": false,
            "compositeIndexUsed": 1,
            "current": "#49:1499971",
            "involvedIndexes": [
                "Message.writer"
            ],
            "limit": 5,
            "orderByElapsed": 49,
            "evaluated": 48512,
            "elapsed": 853.48004,
            "resultType": "collection",
            "resultSize": 5,
            "@fieldTypes": "documentReads=l,documentAnalyzedCompatibleClass=l,recordReads=l,fetchingFromTargetElapsed=l,compositeIndexUsed=l,current=x,involvedIndexes=e,orderByElapsed=l,evaluated=l,user=x,elapsed=f"
        }
    ],
    "notification": "Query executed in 0.864 sec. Returned 1 record(s)"
}