当将insert节点与BaseX数据库中的现有节点连接时,我遇到了一些性能问题。
脲酶
我有一个很大的XML文件(大约2GB),我用它创建了一个BaseX数据库。XML看起来如下(简化)。它有大约350.000 <record>s:
<collection>
<record>
<id>ABC007</id>
<title>The title of the record</title>
<author>Joe Lastname</author>
... [other information]
</record>
<record>
<id>ABC555</id>
<relation_id>ABC007</relation_id>
<title>Another title</title>
<author>Sue Lastname</author>
... [other information]
</record>
... [many other <record>s]
</collection><record>是相互关联的。一个记录中的<relation_id>指向另一个记录中的<id> (参见上面的示例)。
我在BaseX中所做的是将信息从一个相关记录插入到另一个相关记录,反之亦然。因此,结果如下:
<collection>
<record>
<id>ABC007</id>
<title>The title of the record</title>
<author>Joe Lastname</author>
... [other information]
<related_record> <!-- Insert this information -->
<title>Another title</title>
<author>Sue Lastname</author>
</related_record>
</record>
<record>
<id>ABC555</id>
<relation_id>ABC007</relation_id>
<title>Another title</title>
<author>Sue Lastname</author>
... [other information]
<related_record> <!-- Insert this information -->
<title>The title of the record</title>
<author>Joe Lastname</author>
</related_record>
</record>
... [many other <record>s that should be enriched with other records data]
</collection>我使用以下Java代码执行此操作:
// Setting some options and variables
Context context = new Context();
new Set(MainOptions.AUTOFLUSH, false).execute(context);
new Set(MainOptions.AUTOOPTIMIZE, false).execute(context);
new Set(MainOptions.UPDINDEX, true).execute(context);
// Opening the database
new Open('database_name').execute(context);
// Get all records with <relation_id> tags. These are the "child" records and they contain the "parent" record ID.
String queryParentIdsInChild = "for $childRecord in doc('xmlfile.xml')//record[relation_id]
return db:node-id($childRecord)"
// Iterate over the child records and get the parent record ID
QueryProcessor parentIdsInChildProc = new QueryProcessor(queryParentIdsInChild, context);
Iter iter = parentIdsInChildProc.iter();
parentIdsInChildProc.close();
for(Item childRecord; (childRecord = iter.next()) != null;) {
// Create a pointer to the child record in BaseX for convenience
String childNodeId = childRecord.toString();
String childNode = "db:open-id('database_name', " + childNodeId + ")";
// Get some details from the child record. They should be added to the parent record.
String queryChildDetails = "let $title := data("+childNode+"/title)"
+ " let $author := data("+childNode+"/author)"
+ " return "
+ "<related_record>"
+ " <title>{$title}</title>"
+ " <author>{$author}</author>"
+ "</related_record>";
String childDetails = new XQuery(queryChildDetails).execute(context);
// Create a pointer to the parent record in BaseX for convenience
parentNode = (... similar procedure like getting the child node, therefore skiping that code here)
// PERFORMANCE ISSUE HERE!!!
// Insert the child record details to the parent node
String parentUpdate = "insert node " + childDetails + " into " + parentNode;
new XQuery(parentUpdate).execute(context);
}
... flushing and optimizing code here问题
问题是,当将新节点插入到<record>时,我遇到了大量的性能问题。在一个具有大约10.000 <record>s的较小的测试数据库中,插入的执行速度相当快--大约在7秒内。当我用大约350.000 <record>s在我的生产数据库中运行相同的代码时,一个插入操作需要几秒钟,甚至几分钟!而且会有数千个这样的插入,所以肯定要花费太长时间。
问题
我对BaseX非常陌生,而且我肯定不是最有经验的Java程序员。也许我只是忽略了什么或者犯了个愚蠢的错误。所以我在问有没有人给我个提示。有什么问题吗?是Java代码吗?还是350.000 <record>s的BaseX数据库太大,无法进行插入操作?如果是,有解决办法吗?或者BaseX (或者一般的XML数据库)不是这个使用过程的正确工具吗?
进一步信息
我在Ubuntu18.04上使用BaseX 9.0.2作为独立模式.在运行上述代码之前,我已经做了一个“优化所有”。
发布于 2018-09-25 08:57:54
我想我没有正确地运行optimize。在我再次optimized之后,insert命令运行得非常快。现在,大约有10000个插入在一秒钟内执行。也许这也对我使UPDINDEX和AUTOOPTIMIZE失效有所帮助。
https://stackoverflow.com/questions/51595210
复制相似问题