简体   繁体   English

MySQL更新操作的IN Criteria性能

[英]IN Criteria performance for update operation in MySQL

I have read that creating a temporary table is best if the number of parameters passed in the IN criteria is large. 我已经读到,如果在IN标准中传递的参数数量很大,则最好创建一个临时表。 This is for select queries. 这用于选择查询。 Does this hold true for update queries as well ?? 这对更新查询也适用吗? I have an update query which uses 3 table joins (Inner Joins) and passes 1000 parameters in the IN criteria and this query runs in a loop for 200 or more times. 我有一个更新查询,该查询使用3个表联接(内部联接)并在IN条件中传递1000个参数,并且该查询在循环中运行200次或更多次。 Which is the best approach to execute this query ? 哪个是执行此查询的最佳方法?

IN operations are usually slow. IN操作通常很慢。 Passing 1000 parameters to any query sounds awful. 将1000个参数传递给任何查询听起来很糟糕。 If you can avoid that, do it. 如果可以避免,请执行此操作。 Now, I'd really have a go with the temp table. 现在,我真的可以使用临时表了。 You can even play with the indexing of the table. 您甚至可以使用表的索引。 I mean, instead of just putting values in it, play with the indexes that would help you optimize your searches. 我的意思是,不要只是在其中添加值,而是要使用有助于您优化搜索的索引。

On the other hand, adding with indexes is slower that adding without indexes. 另一方面,带索引的添加要比不带索引的添加要慢。 Go for an empiric test there. 去那里进行经验测试。 Now, what I think is a must, bear in mind that when using the other table you don't need to use the IN clause because you can use the EXISTS clause which results usually in better performance. 现在,我认为必须记住,使用其他表时不需要使用IN子句,因为可以使用EXISTS子句,通常可以提高性能。 IE: IE浏览器:

select * from yourTable yt
where exists (
    select * from yourTempTable ytt
    where yt.id = ytt.id
)

I don't know your query, nor data, but that would give you an idea about how to do it. 我既不知道您的查询,也不知道数据,但这将使您了解如何执行此操作。 Note the inner select * is as fast as select aSingleField , as the database engine optimizes it. 请注意,内部select *select aSingleField一样快,因为数据库引擎对其进行了优化。

Those are all my thoughts. 这些都是我的想法。 But remember, to be 100% sure of what is best for your problem, there is nothing like performing both tests and timing them :) Hope this help. 但是请记住,要100%确定最适合您的问题,没有什么比执行测试和安排测试时间更合适了:)希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM