简体   繁体   English

是不是比MySQL中的不等要快?

[英]Is NOT IN faster than inequals in MySQL?

I'm trying to figure out if there's a more efficient way to write my stored procedure for MySQL 5.5. 我试图找出是否有一种更有效的方法来为MySQL 5.5编写存储过程。 Essentially the stored procedure will truncate data from all my created tables except a select few. 本质上,存储过程将截断我创建的所有表中的数据,除了少数几个。 I try to achieve this by executing the below … 我尝试通过执行以下操作来实现这一目标……

create procedure truncate_tables()
begin
 declare tab_name varchar(64);
 declare done tinyint unsigned default 0;

 declare table_cur cursor for select t.table_name
 from
  information_schema.schemata s
  inner join information_schema.tables t on s.schema_name = t.table_schema
 where
   s.schema_name = database() 
   and t.table_type = 'BASE TABLE'
   and t.table_name not like 'DATABASE%'
   and t.table_name != ‘my_table_1’
   and t.table_name != ‘my_table_2’
   and t.table_name != ‘my_table_3’
   …
   and t.table_name != ‘my_table_n-1’
   and t.table_name != ‘my_table_n’;

 declare continue handler for not found set done = 1;

 open table_cur;
 repeat
   fetch table_cur into tab_name;
   set @cmd = concat('truncate table ', tab_name);

   prepare stmt from @cmd;
   execute stmt;
 until done end repeat;

 close table_cur;
end

As I explore ways to make this faster, should I replace my numerous inequals clauses with a "NOT IN (list)" clause? 当我探索更快的方法时,是否应该将我的众多不等式子句替换为“ NOT IN(list)”子句? Not sure which will execute faster in MySQL or if there are other efficiencies to be gained. 不确定哪种方法在MySQL中执行速度更快,或者是否还有其他效率需要提高。

NOT IN is a clearer way to write the code and it makes the code more maintainable (easier to add new table names, easier to understand). NOT IN是一种更清晰的代码编写方式,它使代码更具可维护性(更易于添加新表名,更易于理解)。

You are working off of system tables, which are probably not that large. 您正在处理的系统表可能不那么大。 You are doing a join which probably has more overhead than the where clause. 您正在执行的联接可能比where子句的开销更大。 And, you are using a cursor to access the data. 并且,您正在使用游标访问数据。 The difference in performance is likely to be negligible for some micro-optimization on the where clause. 对于where子句的某些微优化,性能差异可能可以忽略不计。

Go with not in . not in

And, if you are really concerned, test both methods and choose the one that is faster. 并且,如果您真的很担心,请测试这两种方法并选择速度更快的一种。

To answer the question in the title of your question, no, there's no difference in performance, in terms of the generated execution plan. 要在您的问题的标题中回答该问题,不,就生成的执行计划而言,性能没有差异。 The two forms are equivalent. 这两种形式是等效的。 (It's possible that there might be tiny bit of difference in performance in actually parsing the statement, but that's not going to be measurable. (在实际解析该语句时,性能可能会略有不同,但这将无法测量。

I expect the bulk of the time will be in actually performing the individual TRUNCATE statements. 我希望大部分时间都在实际执行单个TRUNCATE语句中。


You don't need a join to the schemata table, you can reference the table_schema column in the tables table in the predicate: 你不需要加入到schemata的表,你可以参考table_schematables中的谓语表:

SELECT t.table_name
  FROM information_schema.tables t 
 WHERE t.table_schema = DATABASE()
   AND t.table_type = 'BASE TABLE'
   AND ...

The predicate 谓词

foo NOT IN ('a','b','c')

is equivalent to 相当于

(foo != 'a' AND foo != 'b' AND foo != 'c')

There's no difference in the execution plan; 执行计划没有什么区别; in this case, the NOT IN just avoids us having to repeat the reference to foo and makes the statement easier to read, understand and maintain. 在这种情况下, NOT IN只是避免我们不得不重复对foo的引用,并使该语句更易于阅读,理解和维护。

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

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