简体   繁体   English

检查mySQL中是否存在记录时的性能问题?

[英]Performance issues when checking if record exists in mySQL?

I am wondering if there is a performance difference between these two queries that check if a record exists? 我想知道检查记录是否存在的这两个查询之间是否存在性能差异?

select count(1) from table where id = 1;

or 要么

select id from table where id = 1;

I don't think there will be much of a difference between those two queries : the difference being selecting a field (which is part of an index) in the second case, and counting one line in the first... Not that much of a difference. 我认为这两个查询之间不会有太大的区别:第二种情况是选择一个字段(属于索引的一部分),第一种情况是计数一行...有所不同。

Still, out of curiosity, I did a very quick benchmark of those kind of queries on a database I have on my computer -- note there are only like 7 lines in the post table, so it might not be that close to a real situation, but as there is a PK on id , which means an index... . 尽管如此,出于好奇,我还是对计算机上的数据库中的此类查询进行了非常快速的基准测试-请注意,post表中只有7行,因此可能与实际情况不太接近,但id上有一个PK,这意味着一个索引...。

Here's what I got : 这是我得到的:

mysql> select benchmark(10000000000, 'select sql_no_cache id from post where id = 1');
+-------------------------------------------------------------------------+
| benchmark(10000000000, 'select sql_no_cache id from post where id = 1') |
+-------------------------------------------------------------------------+
|                                                                       0 |
+-------------------------------------------------------------------------+
1 row in set (1 min 0,25 sec)

mysql> select benchmark(10000000000, 'select sql_no_cache count(1) from post where id = 1');
+-------------------------------------------------------------------------------+
| benchmark(10000000000, 'select sql_no_cache count(1) from post where id = 1') |
+-------------------------------------------------------------------------------+
|                                                                             0 |
+-------------------------------------------------------------------------------+
1 row in set (1 min 0,23 sec)

So, really not that much of a difference, it seems ^^ 所以,实际上并没有太大的区别,看来^^

Although Pascal is correct in his resume, his example is erroneous. 尽管Pascal的简历是正确的,但他的榜样是错误的。

benchmark(10000000000, 'select sql_no_cache id from post where id = 1')

The above statement evaluates a string with a statement , not the SELECT statement itself. 上面的语句使用语句而不是SELECT语句本身对字符串求值。 The correct syntax for the BENCHMARK function would be: BENCHMARK函数的正确语法为:

benchmark(10000000000, (select sql_no_cache id from post where id = 1))

Also, at least in MySQL 5.5.22, using SQL_NO_CACHE in the statement provided to BENCHMARK results in an error: 另外,至少在MySQL 5.5.22中,在提供给BENCHMARK的语句中使用SQL_NO_CACHE会导致错误:

mysql> SELECT BENCHMARK(1000000000,(SELECT SQL_NO_CACHE COUNT(1) FROM players WHERE id=1));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COUNT(1) FROM players WHERE id=1))' at line 1
mysql> SELECT BENCHMARK(1000000000,(SELECT SQL_NO_CACHE id FROM players WHERE id=1));
ERROR 1054 (42S22): Unknown column 'SQL_NO_CACHE' in 'field list'

However, an experiment with actual statements, 40000 record table and query cache turned off confirm that COUNT would work just a bit faster than retrieving the ID, even though the difference might be considered negligible. 但是,通过对实际语句,40000条记录表和查询高速缓存进行关闭的实验,可以确认COUNT的工作速度比检索ID快一点,即使该差异可能被忽略不计。

mysql> set query_cache_type=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT SQL_NO_CACHE benchmark(1000000000,(SELECT id FROM test WHERE id=1));
+-----------------------------------------------------------+
| benchmark(1000000000,(SELECT id FROM players WHERE id=1)) |
+-----------------------------------------------------------+
|                                                         0 |
+-----------------------------------------------------------+
1 row in set (23.17 sec)

mysql> SELECT SQL_NO_CACHE benchmark(1000000000,(SELECT COUNT(1) FROM test WHERE id=1));
+-----------------------------------------------------------------+
| benchmark(1000000000,(SELECT COUNT(1) FROM players WHERE id=1)) |
+-----------------------------------------------------------------+
|                                                               0 |
+-----------------------------------------------------------------+
1 row in set (22.50 sec)

The second statement is probably faster. 第二条语句可能更快。 But for practical purposes, the difference will be negligible 但出于实际目的,差异可以忽略不计

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

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