gpt4 book ai didi

sql - 使用子查询选择在 sqlite 中大约需要 1 分钟,在 SQL Server 中需要 < 1s

转载 作者:行者123 更新时间:2023-12-03 17:46:18 25 4
gpt4 key购买 nike

我已经查阅了很多关于这个问题的帖子(sqlite 中的子查询非常慢)。但我不太擅长 sql,我不知道我能做什么。

我有这个查询:

SELECT * FROM data d WHERE d.category = 3 AND 
(SELECT COUNT(id) FROM data_tag WHERE data = d.id AND (tag = 2136 OR tag = 8)) >= 2
ORDER BY id ASC

我有 3 张 table
data,tag 和 data_tag (n-m 的数据和标签)
每个数据都有 n 个标签,在这个查询中我按标签搜索数据(2 个标签,都必须在数据中)

我将我的数据库从 SQL Server 切换到 sqlite,除了这个查询之外一切正常。
在 SQL Server 中,这个执行时间不到 1 秒,在 sqlite 中大约需要 1 分钟。
请给我一些提示。

最佳答案

SELECT d.* FROM data d
INNER JOIN data_tag ON data_tag.data = d.id AND (tag = 2136 OR tag = 8)
WHERE d.category = 3
GROUP BY d.id
HAVING COUNT(data_tag.id) >= 2
ORDER BY id ASC

关于sql - 使用子查询选择在 sqlite 中大约需要 1 分钟,在 SQL Server 中需要 < 1s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com