OrientDB:查询速度慢,需要帮助创建索引以加快速度
OrientDB: slow query, need help creating index to speed it up
我正在使用 SQL 查询从我的 OrientDB 数据库 (v2.1.16) 中检索货币交易
查询 运行 很慢,我想知道如何创建索引来加快查询速度。
查询是:
SELECT timestamp, txId
FROM MoneyTransaction
WHERE (
out("MoneyTransactionAccount").in("AccountMoneyProfile")[accountId] = :accountId
AND moneyType = :moneyType
AND :registerType IN registerQuantities.keys()
)
ORDER BY timestamp DESC, @rid DESC
我还有另一个从特定时间点恢复列表的变体:
SELECT timestamp, txId
FROM MoneyTransaction
WHERE (
out("MoneyTransactionAccount").in("AccountMoneyProfile")[accountId] = :accountId
AND moneyType = :moneyType
AND :registerType IN registerQuantities.keys()
)
AND timestamp <= :cutoffTimestamp
AND txId NOT IN :cutoffTxIds
ORDER BY timestamp DESC, @rid DESC
我遇到的困难是试图弄清楚如何使用更复杂的字段创建索引,即不在同一顶点内的 accountId 字段,以及要在一个顶点内找到的 registerType 字段EMBEDDEDMAP 字段。
你会创建哪个索引来加速这个查询?或者您将如何重写此查询?
我的结构如下:
[Account] --> (1 to 1) AccountMoneyProfile --> [MoneyProfile]
[MoneyTransaction] --> (n to 1) MoneyTransactionAccount --> [MoneyProfile]
重要字段:
Account.accountId STRING
MoneyTransaction.registerQuantities EMBEDDEDMAP
MoneyTransaction.timestamp DATETIME
我现在获取的帐户有大约 500 个 MoneyTransaction 顶点附加到它。
关于索引的选择,这取决于你的数据集的数量:
- 如果数据集不是很大,您可以使用
SB-TREE
索引,因为它们保持排序并允许范围操作;
- 如果数据集非常大,您可以使用
HASH INDEX
,它在处理大量数据时功能更强大,并且比其他索引消耗更少的资源,但它不会'不支持范围操作。
在您的情况下,您可以在 accountId
上创建一个 SB-TREE UNIQUE INDEX
(例如Account.accountId
) 并以目标查询直接匹配索引的方式重写您的查询,以便它读取尽可能少的记录。示例:
SELECT timestamp, txId
FROM (
SELECT expand(out("AccountMoneyProfile").in("MoneyTransactionAccount"))
FROM Account
WHERE accountId = :accountId
)
WHERE moneyType = :moneyType AND :registerType IN registerQuantities.keys()
ORDER BY timestamp DESC, @rid DESC
这样你直接select你要找的Account
条记录(通过使用之前创建的索引)然后你就可以检索只有连接的 MoneyTransaction
条记录。
您可以在 OrientDB official documentation 中找到有关索引的更多详细信息。
另一种方式,基于您指定 MoneyProfile
class 不包含重要数据的事实(如果我理解得很好的话) , 可能是更改结构以使搜索更直接。例如:
之前:
After(我之前创建了一个新的AccountMoneyTransaction
边class):
希望对您有所帮助
我正在使用 SQL 查询从我的 OrientDB 数据库 (v2.1.16) 中检索货币交易
查询 运行 很慢,我想知道如何创建索引来加快查询速度。
查询是:
SELECT timestamp, txId
FROM MoneyTransaction
WHERE (
out("MoneyTransactionAccount").in("AccountMoneyProfile")[accountId] = :accountId
AND moneyType = :moneyType
AND :registerType IN registerQuantities.keys()
)
ORDER BY timestamp DESC, @rid DESC
我还有另一个从特定时间点恢复列表的变体:
SELECT timestamp, txId
FROM MoneyTransaction
WHERE (
out("MoneyTransactionAccount").in("AccountMoneyProfile")[accountId] = :accountId
AND moneyType = :moneyType
AND :registerType IN registerQuantities.keys()
)
AND timestamp <= :cutoffTimestamp
AND txId NOT IN :cutoffTxIds
ORDER BY timestamp DESC, @rid DESC
我遇到的困难是试图弄清楚如何使用更复杂的字段创建索引,即不在同一顶点内的 accountId 字段,以及要在一个顶点内找到的 registerType 字段EMBEDDEDMAP 字段。
你会创建哪个索引来加速这个查询?或者您将如何重写此查询?
我的结构如下:
[Account] --> (1 to 1) AccountMoneyProfile --> [MoneyProfile]
[MoneyTransaction] --> (n to 1) MoneyTransactionAccount --> [MoneyProfile]
重要字段:
Account.accountId STRING
MoneyTransaction.registerQuantities EMBEDDEDMAP
MoneyTransaction.timestamp DATETIME
我现在获取的帐户有大约 500 个 MoneyTransaction 顶点附加到它。
关于索引的选择,这取决于你的数据集的数量:
- 如果数据集不是很大,您可以使用
SB-TREE
索引,因为它们保持排序并允许范围操作; - 如果数据集非常大,您可以使用
HASH INDEX
,它在处理大量数据时功能更强大,并且比其他索引消耗更少的资源,但它不会'不支持范围操作。
在您的情况下,您可以在 accountId
上创建一个 SB-TREE UNIQUE INDEX
(例如Account.accountId
) 并以目标查询直接匹配索引的方式重写您的查询,以便它读取尽可能少的记录。示例:
SELECT timestamp, txId
FROM (
SELECT expand(out("AccountMoneyProfile").in("MoneyTransactionAccount"))
FROM Account
WHERE accountId = :accountId
)
WHERE moneyType = :moneyType AND :registerType IN registerQuantities.keys()
ORDER BY timestamp DESC, @rid DESC
这样你直接select你要找的Account
条记录(通过使用之前创建的索引)然后你就可以检索只有连接的 MoneyTransaction
条记录。
您可以在 OrientDB official documentation 中找到有关索引的更多详细信息。
另一种方式,基于您指定 MoneyProfile
class 不包含重要数据的事实(如果我理解得很好的话) , 可能是更改结构以使搜索更直接。例如:
之前:
After(我之前创建了一个新的AccountMoneyTransaction
边class):
希望对您有所帮助