【问题标题】:Optimize SQL query in OrientDB with two sub-queries使用两个子查询优化 OrientDB 中的 SQL 查询
【发布时间】:2019-07-19 21:11:45
【问题描述】:

假设有以下 SQL 查询:

select count(name)
from asset
where bucket in (
  select @RID
  from bucket
  where repository_name = 'some-release'
) and blob_updated < sysdate() - 17280000000


+----+-----+
|#   |count|
+----+-----+
|0   |90717|
+----+-----+

我发现存储库some-release 中有多少文件超过 200 天。我花了 17.588 秒。但是,我想利用以下查询将 200 天转换为毫秒,它给了我相同的输出,但需要 83.93 秒:

select count(name)
from asset
let $days = (
  select eval ( "200 * 24 * 60 * 60 * 1000" )
)
where bucket in (
  select @RID
  from bucket
  where repository_name = 'some-release'
) and blob_updated < sysdate() - first($days.eval)

为什么需要这么长时间以及如何优化它?

存储库 some-release 包含 255196 个文件。

【问题讨论】:

    标签: sql optimization subquery orientdb nexus


    【解决方案1】:

    您是否尝试过通过EXPLAIN 运行查询?更多信息请参见OrientDB documentation

    LET 块根据每条记录进行评估,因此,如果您的 some-release 存储库中有大量资产(或过去有),这将大大缩短您的查询评估时间。为避免这种情况,您可以直接在 WHERE 子句中对其进行评估,即:

    select count(name)
    from asset
    where bucket in (
      select @RID
      from bucket
      where repository_name = 'some-release'
    ) and blob_updated < sysdate() - eval('200 * 24 * 60 * 60 * 1000')
    

    请问你想达到什么目的?无论如何,您是否正在尝试摆脱一些旧资产?您可以为此设置Cleanup Policy

    您可能希望压缩 blob 存储,以减少您可能不再需要的资产数量。在运行Admin - Compact blob store 任务之前,请确保您了解会发生什么。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多