简体   繁体   English

OrientDB:查询速度慢,需要帮助创建索引以加快速度

[英]OrientDB: slow query, need help creating index to speed it up

I'm using an SQL query to retrieve money transactions from my OrientDB database (v2.1.16)我正在使用 SQL 查询从我的 OrientDB 数据库 (v2.1.16) 中检索金钱交易

The query is running slowly and I'd like to know how to create the index that will speed it up.查询运行缓慢,我想知道如何创建可以加快查询速度的索引。

The query is:查询是:

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

I also have another variant that resumes the list from a specific point in time:我还有另一种从特定时间点恢复列表的变体:

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

The difficulty I have is trying to figure out how to create an index with the more complex fields, namely the accountId field which doesn't reside within the same vertex, and the registerType field which is to be found within an EMBEDDEDMAP field.我遇到的困难是试图弄清楚如何使用更复杂的字段创建索引,即不在同一顶点内的 accountId 字段,以及将在 EMBEDDEDMAP 字段中找到的 registerType 字段。

Which index would you create to speed up this query?你会创建哪个索引来加速这个查询? Or how would you rewrite this query?或者你会如何重写这个查询?

My structure is as follows:我的结构如下:

[Account] --> (1 to 1) AccountMoneyProfile --> [MoneyProfile]
[MoneyTransaction] --> (n to 1) MoneyTransactionAccount --> [MoneyProfile]

Important fields:重要领域:

Account.accountId STRING
MoneyTransaction.registerQuantities EMBEDDEDMAP
MoneyTransaction.timestamp DATETIME

The account I'm fetching right now has about 500 MoneyTransaction vertices attached to it.我现在正在获取的帐户附加了大约 500 个 MoneyTransaction 顶点。

about the index choice, it depends by the amounts of your dataset:关于索引选择,这取决于数据集的数量:

  • If the dataset isn't very large, you could use an SB-TREE index because they maintain sorting and allow range operations;如果数据集不是很大,您可以使用SB-TREE索引,因为它们保持排序并允许范围操作;
  • If the dataset instead is very large, you could use an HASH INDEX which is more functional on large numbers and consumes less resources than other indexes, but it doesn't support range operations.如果数据集非常大,您可以使用HASH INDEX ,它在处理大量数据时功能更强大,并且比其他索引消耗更少的资源,但它不支持范围操作。

In your case you could create, for example, an SB-TREE UNIQUE INDEX on the accountId (eg Account.accountId ) and rewrite your query in a way that the target query directly matches the index and so that it reads fewer records as possible.例如,在您的情况下,您可以在accountId (例如Account.accountId )上创建一个SB-TREE UNIQUE INDEX并以目标查询直接匹配索引的方式重写您的查询,以便它读取尽可能少的记录。 Example:例子:

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

In this way you directly select the Account records you're looking for (by using the index previously created) and then you can retrieve only the connected MoneyTransaction records.通过这种方式,您可以直接选择您要查找的Account记录(通过使用先前创建的索引),然后您可以仅检索连接的MoneyTransaction记录。

You can find more detailed information about indexes in the OrientDB official documentation .您可以在OrientDB 官方文档中找到有关索引的更多详细信息。

Another way, based on the fact that you specified that MoneyProfile class doesn't contains important data (if I've understood well), could be to change the structure to make the search more direct.另一种方法,基于您指定MoneyProfile类不包含重要数据(如果我理解得很好)的事实,可以更改结构以使搜索更直接。 Eg:例如:

Before:前:

在此处输入图片说明

After (I've previously created a new AccountMoneyTransaction edge class):之后(我之前创建了一个新的AccountMoneyTransaction边缘类):

在此处输入图片说明

Hope to have been helpful希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM