【问题标题】:sqlite union all optimizationsqlite union all 优化
【发布时间】:2013-03-21 03:27:19
【问题描述】:

我有一个如下所示的 sqlite DB 查询:

SELECT
    origin,
    destination,
    weight,
    rate,
    0 as group
    from groupAZones, groupARates
    where
    tms = groupZone

union all

SELECT
    origin,
    destination,
    weight,
    rate,
    1 as group
    from groupBZones, groupBRates
    where
    tms = groupZone

    union all

SELECT
    origin,
    destination,
    weight,
    rate,
    2 as group
    from groupCZones, groupCRates
    where
    tms = groupZone

    union all

SELECT
    origin,
    destination,
    weight,
    rate,
    3 as group
    from groupDZones, groupDRates
    where
    tms = groupZone

有没有好的方法来优化这样的查询?我正在尝试创建一个结合这 4 个表的简单视图。将此用作视图查询,对视图的查询大约需要 13 秒。

我尝试为 4 个表创建索引,但似乎没有帮助。

我在 SQL 方面是个新手,我知道足够做简单的事情,但我仍在学习高级技巧。

任何指针或信息都会有所帮助。

【问题讨论】:

  • 你要处理多少数据?
  • 如果我使用相同的查询构建一个表,它可以计算出大约 450 万条记录和大约 125 MB 的磁盘空间。
  • 为此查询显示EXPLAIN QUERY PLAN 的输出。
  • CL 这个命令实际上让我发现我的两个索引没有被使用!修复索引后,查询现在非常快。谢谢你的协助!我对你的评论投了赞成票。

标签: sql sqlite union-all


【解决方案1】:

CL 指出我应该运行EXPLAIN QUERY PLAN 命令。这导致发现 4 个索引中有两个没有被使用。

谢谢 CL!

我仍然想知道是否有更好的方法来执行该查询,但现在它正在做我需要它做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2021-08-10
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多