【问题标题】:Performance penalties from sqlite viewssqlite 视图的性能损失
【发布时间】:2018-04-17 10:31:17
【问题描述】:

我有一个选择查询,如下:

select distinct
  ...some fields

from
  table_a,
  table_b,
  table_c,
  ...more tables

where
  table_a.id = table_b.id and
  ... (rest of the tables) and
  some_field_that_i_care_about = 42

order by
  my_field

如果我按原样运行该查询,大约需要半秒才能得到我期望的结果。

不过,如果我将相同的查询转换为视图(不包括 some_field_that_i_care_about = 42 条件)然后运行:

select *
from the_view
where some_field_that_i_care_about = 42

查询需要大约 40 秒才能返回相同的数据。

为什么会这样?

【问题讨论】:

标签: sqlite views database-performance


【解决方案1】:

视图只是子查询的语法糖。此类子查询通常不会导致性能损失,因为它们可以flattened,即合并到外部查询中。

但是,在这种情况下,ORDER BY 阻止了展平。

只需从视图中删除 ORDER BY 即可;无论如何,子查询的顺序是ignored by the outer query。 (如果要对结果进行排序,则必须在最外层的查询中使用 ORDER BY。)

【讨论】:

  • 我尝试了您的建议,但查询仍然需要大约 40 秒。还有其他想法吗?
猜你喜欢
  • 2012-02-08
  • 2019-02-09
  • 2012-08-23
  • 2016-12-20
  • 1970-01-01
  • 2010-12-17
  • 2018-08-22
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多