SQL 性能:哪个更快? IN() 与 JOIN

发布于 2024-12-13 09:33:59 字数 584 浏览 6 评论 0原文

这是一个我从未得到明确答案的问题。我在这个例子中使用 MySQL。

给定一组相当大的值(比方说 500)。使用这些值和 IN() 子句搜索表是否更快:

SELECT * FROM table WHERE field IN(values)

或者通过在内存中创建临时表,用值填充它并将其连接到正在搜索的表:

CREATE TEMPORARY TABLE `temp_table` (`field` varchar(255) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO temp_table VALUES (values)

SELECT * FROM table t1 JOIN temp_table t2 ON t1.field = t2.field

两种方法都会生成相同的结果集。

我自己做了一些基本的基准测试,发现当处理超过 500 个值时,使用临时表比使用 IN() 子句更快。

有人可以向我解释 MySQL 的内部工作原理以及这个问题的正确答案是什么吗?

谢谢, 狮子座

This is a question to which I've never gotten a definitive answer. I am using MySQL in this example.

Given a reasonably large set of values (lets say 500). Is it quicker to search a table using these values with an IN() clause:

SELECT * FROM table WHERE field IN(values)

Or by creating a temporary table in memory, populating it with the values and joining it to the table being searched:

CREATE TEMPORARY TABLE `temp_table` (`field` varchar(255) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO temp_table VALUES (values)

SELECT * FROM table t1 JOIN temp_table t2 ON t1.field = t2.field

Both methods will produce same result set.

I have done some of my own basic benchmarking tests and found that when dealing with more than 500 values it becomes quicker to use a temporary table than an IN() clause.

Can someone explain to me the inner workings of MySQL and what a proper answer to this question is?

Thanks,
Leo

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

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

发布评论

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

评论(2

随梦而飞# 2024-12-20 09:33:59

来自MySql在线文档, IN()

IN(值,...)

如果所有值都是常量,则根据 expr 的类型对它们进行求值并排序。
然后使用二分搜索来完成对该项目的搜索。这意味着
如果 IN 值列表完全由常量组成,则 IN 非常快。
否则,按照规则进行类型转换
第 11.2 节“表达式求值中的类型转换”中描述,
但适用于所有参数。

考虑到我认为将 IN() 与一组常量一起使用是有意义的,否则您应该在另一个表上使用子查询。

您可以考虑使用 EXISTS() 当从其他表检索项目时,使用 JOIN 代替 JOIN,对于大型数据集,它会明显更快

SELECT * 
FROM table t1 
WHERE EXISTS 
      (
        SELECT * 
        FROM temp_table t2 
        WHERE t1.field = t2.field
      )

From MySql online documentation, IN() :

IN (value,...)

If all values are constants, they are evaluated according to the type of expr and sorted.
The search for the item then is done using a binary search. This means
IN is very quick if the IN value list consists entirely of constants.
Otherwise, type conversion takes place according to the rules
described in Section 11.2, “Type Conversion in Expression Evaluation”,
but applied to all the arguments.

Considering that I believe it makes sense to use IN() with a set of constants, otherwise you should use subquery on another table.

You can consider usign EXISTS() instead of JOIN when items are retrieved from an other table, it would significantly faster for large data set

SELECT * 
FROM table t1 
WHERE EXISTS 
      (
        SELECT * 
        FROM temp_table t2 
        WHERE t1.field = t2.field
      )
ぇ气 2024-12-20 09:33:59

正确答案取决于很多因素。

您已经完成了工作 - 如果您的基准测试告诉您使用临时表更快,那么就应该这样做。

如果您更改硬件或显着更改架构,请记住再次进行基准测试。

The correct answer depends on many things.

You've done the work already - if your benchmarking tells you that using a temporary table is faster, then that's the way to go.

Remember to benchmark again if you change the hardware, or dramatically alter the schema.

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