水平联合全部

发布于 2024-11-06 19:06:30 字数 912 浏览 0 评论 0原文

我有两个表,我需要从每个表中选择一列。 这必须在单个查询中完成。 好消息是这两列的排序方式正确,并且它们都包含相同的行数。 现在,我知道我可以通过 rowid JOIN 两个表,但它很慢,因为它必须进行比较。就我而言,这是没有必要的......我需要更像水平 UNION ALL 的东西来连接两列长度相等的列。

SQLite 3 中可能有类似的事情吗?

谢谢。

TABLE1:

| timestamp | FIELD1 | FIELD2 | ...
| 12345678  | 000000 | 000000 | ...
| 00154789  | 000000 | 000000 | ...

TABLE2:

| temperature |
| 1000000000  |
| 2000000000  |

需要选择输出

| timestamp | temperature |
| 12345678  | 1000000000  |
| 00154789  | 2000000000  |

查询

SELECT timestamp, temperature
FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.rowid = TABLE2.rowid;

在我的测试应用程序中,这大约需要 0.75 秒。当我执行两个单独的 SELECT 操作并稍后在程序中加入输出时,大约需要 0.4 秒,但这不是很方便。最快的方法(~0.23s)是将两列都放在一个表中,但这很浪费,因为我有多个版本的 TABLE2 共享相同的时间戳。

I have two tables and I need to select one column from each of them.
This must be done in a single query.
The good news is that the two columns are ordered the right way and they both contain the same number of rows.
Now, I know I could JOIN the two tables by rowid, but it is slow as it has to do that comparison. In my case it is not necessary... I need something more like horizontal UNION ALL to concatenate two columns of equal length.

Is anything like that possible in SQLite 3?

Thanks.

TABLE1:

| timestamp | FIELD1 | FIELD2 | ...
| 12345678  | 000000 | 000000 | ...
| 00154789  | 000000 | 000000 | ...

TABLE2:

| temperature |
| 1000000000  |
| 2000000000  |

REQUIRED SELECT OUTPUT

| timestamp | temperature |
| 12345678  | 1000000000  |
| 00154789  | 2000000000  |

QUERY:

SELECT timestamp, temperature
FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.rowid = TABLE2.rowid;

This takes ~0.75s in my testing app. When I do two separate SELECTs and join the outputs later in my program it takes ~0.4s, but it is not very convenient. The fastest way (~0.23s) is to have both columns in one table, but it is wasteful as I have multiple versions of TABLE2 that share the same timestamps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2024-11-13 19:06:30

SQLite 支持UNION ALL

两个或多个简单的 SELECT 语句
可以连接在一起形成
使用 UNION 的复合 SELECT, UNION
ALL、INTERSECT 或 EXCEPT 运算符。在
复合 SELECT,所有组成部分
SELECT 必须返回相同数量的
结果列。作为一个组件
复合 SELECT 必须是简单 SELECT
语句,它们可能不包含 ORDER
BY 或 LIMIT 子句。订购依据和
LIMIT 子句只能出现在
整个复合 SELECT 的结尾。

使用 UNION 创建的复合 SELECT
ALL 运算符返回所有行
UNION 左侧的 SELECT
ALL 运算符,以及来自的所有行
其右侧的 SELECT。这
UNION 运算符的工作方式与
UNION ALL,除了重复的行
从最终结果集中删除。
INTERSECT 运算符返回
结果的交集
左右选择。除了
运算符返回行的子集
由左侧 SELECT 返回的是
右手也没有返回
选择。重复的行被删除
根据 INTERSECT 和的结果
结果集之前的 EXCEPT 运算符
已返回。

SQLite supports UNION ALL.

Two or more simple SELECT statements
may be connected together to form a
compound SELECT using the UNION, UNION
ALL, INTERSECT or EXCEPT operator. In
a compound SELECT, all the constituent
SELECTs must return the same number of
result columns. As the components of a
compound SELECT must be simple SELECT
statements, they may not contain ORDER
BY or LIMIT clauses. ORDER BY and
LIMIT clauses may only occur at the
end of the entire compound SELECT.

A compound SELECT created using UNION
ALL operator returns all the rows from
the SELECT to the left of the UNION
ALL operator, and all the rows from
the SELECT to the right of it. The
UNION operator works the same way as
UNION ALL, except that duplicate rows
are removed from the final result set.
The INTERSECT operator returns the
intersection of the results of the
left and right SELECTs. The EXCEPT
operator returns the subset of rows
returned by the left SELECT that are
not also returned by the right-hand
SELECT. Duplicate rows are removed
from the results of INTERSECT and
EXCEPT operators before the result set
is returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文