联合与或

发布于 2024-09-07 02:52:22 字数 536 浏览 3 评论 0原文

假设我有两个查询:

select top 20 idField, field1, field2 from table1
where idField in
    (
    select idField from table1 where field3 like '...'
    union
    select idField from table2 where field4 like '...'
    )
order by sortfield1

select top 20 idField, field1, field2 from table1
where field3 like '...' 
or idfield in 
(select idField from table2 where field4 like '...')
order by sortfield1

假设两个表都相当大(表 2 大于表 1), 结果数的平均范围是 0-400,但我总是想要最多 20 个结果。

是否有可能以很大的概率说出哪种语句性能更高 和/或声明的选择取决于什么? 谢谢

let's say I've got two queries:

select top 20 idField, field1, field2 from table1
where idField in
    (
    select idField from table1 where field3 like '...'
    union
    select idField from table2 where field4 like '...'
    )
order by sortfield1

select top 20 idField, field1, field2 from table1
where field3 like '...' 
or idfield in 
(select idField from table2 where field4 like '...')
order by sortfield1

let's say that both tables are rather large (table2 larger than table1),
average range of results number is 0-400, but I always want max 20 results.

Is it possible to say with big probability which statement would be more performant
and/or what would the choice of statement depend on?
Thank you

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

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

发布评论

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

评论(4

路弥 2024-09-14 02:52:22

在完美的世界中,这应该不会产生影响,但我仍然会尝试这种方法。可以更快:

select top 20 idField, field1, field2 from table1
where idField in
    (
    select top 20 idField from table1 where field3 like '...'
    union
    select top 20 idField from table2 where field4 like '...'
    )

In the perfect world this should not make a difference, but I would still try out this approach too. Could be faster:

select top 20 idField, field1, field2 from table1
where idField in
    (
    select top 20 idField from table1 where field3 like '...'
    union
    select top 20 idField from table2 where field4 like '...'
    )
淤浪 2024-09-14 02:52:22

这很可能取决于您使用的数据库管理系统。我在使用 Oracle (8) 进行联合方面的经历很糟糕,它非常慢,可能是因为它必须将两个查询的结果放入内存中的列表中。

我不确定这两个查询是否有相同的结果。第二个查询仅返回 table1 中的值,而第一个查询也返回 table2 中的值。

It most probably depends on the dbms you are using. I had bad experiences with union using Oracle (8), it was very slow, probably because it had to put the results of two queries into a list in memory.

I'm not sure if these two queries have the same result. The second query only returns values from table1, while the first also returns values from table2.

黑色毁心梦 2024-09-14 02:52:22

检查解释计划,看看效率如何。它还可以让您深入了解索引(如果当前不存在)。

Check the explain plan to see what the efficiency is like. It would also give you insight to indexes, if they don't currently exist.

£烟消云散 2024-09-14 02:52:22

仅通过查看您的答案,我猜测第二个查询会更快,尽管不了解有关架构、索引和数据的更多信息,它只能是猜测。由于您使用的是 LIKE,因此搜索字符串也会产生很大的差异,尤其是在您使用前导通配符的情况下。

使用 LEFT OUTER JOIN 或 EXISTS 可能比问题中使用“IN”的解决方案更快。但在没有更多信息的情况下,我再次无法确定。

Just by looking at your answer, I would guess that the second query would be faster although without knowing more about the schema, indexes, and data it can only be a guess. Since you are using LIKE, the search string can also make a big difference, especially if you are using a leading wildcard.

Using a LEFT OUTER JOIN or EXISTS will probably be faster than either of the solutions in the question that use "IN". But again I couldn't say for certain without more information.

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