如何获得最后 N 个连接边
How to get last N connected edges
我正在编写一个大型聊天应用程序,这是我的数据库架构:
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
"explain" 结果:
{
"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
"explain" 结果:
{
"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
终于!我找到了一种快速的方法。
我已将数据库架构更改为:
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 毫秒!
重要提示: 注意 WHERE
子句中的 createdAt < sysdate()
,
你必须在 ORDER BY
字段上写一个虚拟条件,这使得 ORDER BY
使用 createdAt
索引(快 ~700ms)
"explain" 结果(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)"
}
"explain" 结果(没有 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)"
}
我正在编写一个大型聊天应用程序,这是我的数据库架构:
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
"explain" 结果:
{
"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
"explain" 结果:
{
"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
终于!我找到了一种快速的方法。
我已将数据库架构更改为:
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 毫秒!
重要提示: 注意 WHERE
子句中的 createdAt < sysdate()
,
你必须在 ORDER BY
字段上写一个虚拟条件,这使得 ORDER BY
使用 createdAt
索引(快 ~700ms)
"explain" 结果(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)"
}
"explain" 结果(没有 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)"
}