BaseX:插入节点性能问题

时间:2018-07-30 13:34:39

标签: java xml performance basex

insert将XML节点添加到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数据库)不是此用例的正确工具?

其他信息

我在Ubuntu 18.04上以独立模式使用BaseX 9.0.2。在运行上述代码之前,我已经完成了“全部优化”。

1 个答案:

答案 0 :(得分:0)

我认为我没有正确运行optimize。在我optimized之后,insert命令再次非常快地运行。现在,在一秒钟之内可以执行大约10000次插入。停用UPDINDEXAUTOOPTIMIZE也许也有帮助。