优化 BaseX 中缓慢的 XQuery 查询

2024-04-02

我有一个 BaseX XML 数据库,只有一个小的 XML 文件。这些文件基本上由两种结构组成。一是PlatformCategory有 46 个实例,另一个PlatformGenericType有 213 个实例。

PlatformGenericType有参考PlatformCategory in the href属性。

<PlatformGeneralType id="/plib/platformgeneraltypes/pgt1">
  <name>No statement</name>
  <enum>NO_STATEMENT</enum>
  <isOfPlatformCategory href="/plib/platformcategories/pc1"/>
  <readOnly>true</readOnly>
</PlatformGeneralType>

<PlatformCategory id="/plib/platformcategories/pc1">
  <name>No statement</name>
  <enum>NO_STATEMENT</enum>
  <environment>AIR</environment>
  <readOnly>true</readOnly>
</PlatformCategory>

当我执行以下查询时,大约需要六秒钟才能获得结果:

//PlatformGeneralType[isOfPlatformCategory/@href=//PlatformCategory[environment="AIR"]/@id]

我可以做什么来优化这个查询?

请注意,我运行了“优化全部”。

Update: 上一个查询的问题似乎已经解决了。但是当我使用以下扩展查询时,查询需要 44,28 秒:

/PLib/PlatformSpecificTypes/PlatformSpecificType
[isOfPlatformGeneralType/@href=/PLib/PlatformGeneralTypes/PlatformGeneralType
    [isOfPlatformCategory/@href=/PLib/PlatformCategories/PlatformCategory
        [environment='AIR']/@id]/@id]

共有 8939 个实例PlatformSpecificType及其结构:

<PlatformSpecificTypes>
    <PlatformSpecificType id="/plib/platformspecifictypes/DataShip.3">
        <name>Meko 360H2</name>
        <lethalityLevel>LOW</lethalityLevel>
        <isOfPlatformGeneralType href="/plib/platformgeneraltypes/pgt62"/>
        <ownedByCountry href="/plib/countries/10"/>
    </PlatformSpecificType>
</PlatformSpecificTypes>

其查询信息:

询问: /PLib/PlatformSpecificTypes/PlatformSpecificType[isOfPlatformGeneralType/@href=/PLib/PlatformGeneralTypes/PlatformGeneralType[isOfPlatformCategory/@href=/PLib/PlatformCategories/PlatformCategory[environment='AIR']/@id]/@id] 结果: - 点击数:3642 件 - 更新:0 项 - 印刷版:2048 KB - 读锁定:本地[command_plib] - 写锁定:无 定时: - 解析:1.25 毫秒 - 编译:0.71 毫秒 - 评估:44248.94 毫秒 - 打印:37.11 毫秒 - 总时间:44288.02 毫秒 查询计划:

数据库属性:

Database Properties
 Name: command_plib
 Size: 20247 KB
 Nodes: 781606
 Documents: 1
 Binaries: 0
 Timestamp: 2015-06-12-10-12-14

Resource Properties
 Input Path: /home/sceran/Documents/PLIB/command_plib.xml
 Input Size: 21354 KB
 Timestamp: 2015-06-11-15-34-07
 Encoding: UTF-8
 CHOP: true

Indexes
 Up-to-date: true
 TEXTINDEX: true
 ATTRINDEX: true
 FTINDEX: false
 LANGUAGE: English
 STEMMING: true
 CASESENS: true
 DIACRITICS: false
 STOPWORDS: 
 UPDINDEX: false
 AUTOOPTIMIZE: false
 MAXCATS: 100
 MAXLEN: 96

查询信息:

Compiling:
- rewriting descendant-or-self step(s)
- rewriting descendant-or-self step(s)
- converting descendant::*:PlatformGeneralType[(*:isOfPlatformCategory/@*:href = root()/descendant::*:PlatformCategory[(*:environment = "AIR")]/@*:id)] to child steps
Query:
//PlatformGeneralType[isOfPlatformCategory/@href=//PlatformCategory[environment="AIR"]/@id]
Optimized Query:
db:open-pre("command_plib",0)/*:PLib/*:PlatformGeneralTypes/*:PlatformGeneralType[(*:isOfPlatformCategory/@*:href = root()/descendant::*:PlatformCategory[(*:environment = "AIR")]/@*:id)]
Result:
- Hit(s): 55 Items
- Updated: 0 Items
- Printed: 12776 Bytes
- Read Locking: local [command_plib]
- Write Locking: none
Timing:
- Parsing: 0.55 ms
- Compiling: 0.3 ms
- Evaluating: 5786.29 ms
- Printing: 1.0 ms
- Total Time: 5788.15 ms
Query plan:
<QueryPlan compiled="true">
  <IterPath>
    <DBNode name="command_plib" pre="0"/>
    <IterStep axis="child" test="*:PLib"/>
    <IterStep axis="child" test="*:PlatformGeneralTypes"/>
    <IterStep axis="child" test="*:PlatformGeneralType">
      <CmpG op="=">
        <CachedPath>
          <IterStep axis="child" test="*:isOfPlatformCategory"/>
          <IterStep axis="attribute" test="*:href"/>
        </CachedPath>
        <IterPath>
          <Root/>
          <IterStep axis="descendant" test="*:PlatformCategory">
            <CmpG op="=">
              <CachedPath>
                <IterStep axis="child" test="*:environment"/>
              </CachedPath>
              <Str value="AIR" type="xs:string"/>
            </CmpG>
          </IterStep>
          <IterStep axis="attribute" test="*:id"/>
        </IterPath>
      </CmpG>
    </IterStep>
  </IterPath>
</QueryPlan>

更新二:我怀疑 PlatformSpecificTypes 的结构会阻止索引。我想知道如果我按如下方式更改它,会提高查询性能吗?

<PlatformSpecificTypes>
    <PlatformSpecificType id="/plib/platformspecifictypes/DataShip.3">
        <name>Meko 360H2</name>
        <lethalityLevel>LOW</lethalityLevel>
        **<isOfPlatformGeneralType>/plib/platformgeneraltypes/pgt62 </isOfPlatformGeneralType>**
        <ownedByCountry href="/plib/countries/10"/>
    </PlatformSpecificType>
</PlatformSpecificTypes>

更新三:我已经上传了XML 文件要点 https://gist.github.com/anonymous/821b74d26e631a07f212,以便您可以检查它。

现在,当我执行以下查询时,我需要大约 28 秒才能获得结果。

/root/PlSpTys/PlSpTy[isOfPlGeTy/@href=/root/PlGeTys/PlGeTy[isOfPlCt/@href=/root/PlCts/PlCt[environment='AIR']/@id]/@id]

以下是查询信息:

 Query:
/root/PlSpTys/PlSpTy[isOfPlGeTy/@href=/root/PlGeTys/PlGeTy[isOfPlCt/@href=/root/PlCts/PlCt[environment='AIR']/@id]/@id]
Result:
- Hit(s): 3642 Items
- Updated: 0 Items
- Printed: 257 KB
- Read Locking: local [Output6]
- Write Locking: none
Timing:
- Parsing: 0.66 ms
- Compiling: 0.34 ms
- Evaluating: 28398.32 ms
- Printing: 4.63 ms
- Total Time: 28403.97 ms
Query plan:
<QueryPlan compiled="true">
  <IterPath>
    <DBNode name="Output6" pre="0"/>
    <IterStep axis="child" test="*:root"/>
    <IterStep axis="child" test="*:PlSpTys"/>
    <IterStep axis="child" test="*:PlSpTy">
      <CmpG op="=">
        <CachedPath>
          <IterStep axis="child" test="*:isOfPlGeTy"/>
          <IterStep axis="attribute" test="*:href"/>
        </CachedPath>
        <IterPath>
          <Root/>
          <IterStep axis="child" test="*:root"/>
          <IterStep axis="child" test="*:PlGeTys"/>
          <IterStep axis="child" test="*:PlGeTy">
            <CmpG op="=">
              <CachedPath>
                <IterStep axis="child" test="*:isOfPlCt"/>
                <IterStep axis="attribute" test="*:href"/>
              </CachedPath>
              <IterPath>
                <Root/>
                <IterStep axis="child" test="*:root"/>
                <IterStep axis="child" test="*:PlCts"/>
                <IterStep axis="child" test="*:PlCt">
                  <CmpG op="=">
                    <CachedPath>
                      <IterStep axis="child" test="*:environment"/>
                    </CachedPath>
                    <Str value="AIR" type="xs:string"/>
                  </CmpG>
                </IterStep>
                <IterStep axis="attribute" test="*:id"/>
              </IterPath>
            </CmpG>
          </IterStep>
          <IterStep axis="attribute" test="*:id"/>
        </IterPath>
      </CmpG>
    </IterStep>
  </IterPath>
</QueryPlan>

您能帮我优化查询持续时间吗?


BaseX 似乎没有意识到它应该用静态结果预处理“内部”部分,因此评估成本约为O(n^2)代替O(n).

重新格式化您的查询(在我的机器上大约需要 30 秒)以更好地理解它,显示第一个谓词内比较的整个右侧是静态的,而不依赖于PlSpTy目前分析的元素:

/root/PlSpTys/PlSpTy[
  isOfPlGeTy/@href=/root/PlGeTys/PlGeTy[
    isOfPlCt/@href=/root/PlCts/PlCt[
      environment='AIR'
    ]/@id
  ]/@id
]

在我的机器上对此进行评估大约需要 9 毫秒,这不是很多,但如果重复运行可能会变得昂贵。计算数量PlSpTy元素(count(/root/PlSpTys/PlSpTy))揭示了接近 8939 个这样的元素,因此内部零件成本的评估约为8939*9ms ~= 80s -- 某物肯定已经优化掉了,但不是一切。

如果我们简单地取出查询的这一部分并预先计算它,会发生什么?

let $compare :=
  /root/PlGeTys/PlGeTy[
    isOfPlCt/@href=/root/PlCts/PlCt[
      environment='AIR'
    ]/@id
  ]/@id

return
  /root/PlSpTys/PlSpTy[
    isOfPlGeTy/@href=$compare
  ]

计算时间降至 16 毫秒,其中四分之一用于实际打印结果。我开了一个错误报告要求更好的优化 https://github.com/BaseXdb/basex/issues/1160。 (更新:已应用一些优化 https://github.com/BaseXdb/basex/issues/1160#issuecomment-113525690).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

优化 BaseX 中缓慢的 XQuery 查询 的相关文章

  • 是否可以用 CSS 来设计 C# 应用程序

    我更擅长用 C 创建基于计算机的应用程序 而不是用 ASP net 创建网页 但是 CSS 在设计按钮 表单等方面的强大功能确实让我喜欢它 我在谷歌上搜索是否可以在软件中只使用CSS 但还没有结果 是否可以 我知道这个问题太国外了而且有点愚
  • 如何在 Asp.net MVC 中编写集成和系统测试

    我的应用程序 我的应用程序设计如下所示 Web 应用程序层 带有使用 POCO 和调用服务的控制器和视图的 asp net MVC 应用程序 服务层 使用 POCO 和调用存储库的业务流程 数据层 使用 POCO 并以 EF 模型形式与模型
  • 将 Excel 工作簿的一部分复制/粘贴到当前未打开的工作簿

    我需要将 Excel 工作簿的一部分复制到大约 3500 个其他工作簿 由于工作簿数量众多 在每个工作簿上运行宏是不可行的 有没有办法复制该部分并将其粘贴到其他工作簿而不打开它们 番茄 不幸的是 正如 Ken White 和 L42 都指出
  • Windows 上“没有名为 scipy 的模块”

    在 sklearn base py 中 我有这样的声明from scipy import sparse 我收到这个错误 no module named scipy 我尝试安装使用pip install scipy 但我收到了很多错误 lib
  • 在 Xna/MonoGame 中按程序生成 Texture2D

    如何使用代码按程序生成Texture2D 例如 我希望在 32x32 图像上交替出现黑白像素 您可以使用 GraphicsDevice 创建一个新的纹理 public static Texture2D CreateTexture Graph
  • 将 UIViewController 添加到 UIScrollView

    我有一个UIViewCOntroller我已经添加了很多 UI 组件 标签 文本字段等 现在 我需要向其中添加更多字段 我发现我的视图控制器中没有任何空间可以添加任何空间 所以我想添加一个scrollView来解决这个问题 我可以在哪里添加
  • .net中RSA公钥模数和指数的格式是什么?

    一个简短的问题 如果我有来自第三方的以下输入 我该如何填写 RSAParameters 模数 123456 指数 111 长话短说 我使用以下代码 RSACryptoServiceProvider RSAalg 新 RSACryptoSer
  • 在选项卡式 UIPopover 中使用 UIImagePicker

    我正在开发一个 iPad 应用程序 需要在设备 相册 远程和应用程序中包含多个图像源 现在理想的情况是拥有一个 UIPopover 控制器 每个源有 3 个选项卡 唯一的问题是我似乎无法弄清楚如何让 UIImagePicker 位于其自己的

随机推荐