【发布时间】: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 这个命令实际上让我发现我的两个索引没有被使用!修复索引后,查询现在非常快。谢谢你的协助!我对你的评论投了赞成票。