<李> SELECT * FROM test WHERE text LIKE '%something%' LIMIT 1mysql_num_rows(): 0.039061069488525s。李(快) < / >
<李> SELECT count(*) as count FROM test WHERE text LIKE '%something%:
李16.028197050095 s。< / >
<李> SELECT EXISTS(SELECT 1 FROM test WHERE text LIKE '%something%'):
李0.87045907974243 s。< / >
SELECT EXISTS(SELECT 1 FROM test WHERE text LIKE '%something%' LIMIT 1): 0.044898986816406s。
但是现在,在BIGINT PK字段中,只有一个条目等于'321321':
<李> SELECT * FROM test2 WHERE id ='321321' LIMIT 1mysql_num_rows(): 0.0089840888977051s.
SELECT count(*) as count FROM test2 WHERE id ='321321': 0.00033879280090332s。
SELECT EXISTS(SELECT 1 FROM test2 WHERE id ='321321'): 0.00023889541625977s。
SELECT EXISTS(SELECT 1 FROM test2 WHERE id ='321321' LIMIT 1): 0.0002031326293943s。(快)
mysql> SELECT * FROM table_1;
+----+--------+
| id | col1 |
+----+--------+
| 1 | foo |
| 2 | bar |
| 3 | foobar |
+----+--------+
3 rows in set (0.00 sec)
mysql> SELECT EXISTS(SELECT 1 FROM table_1 WHERE id = 1);
+--------------------------------------------+
| EXISTS(SELECT 1 FROM table_1 WHERE id = 1) |
+--------------------------------------------+
| 1 |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT EXISTS(SELECT 1 FROM table_1 WHERE id = 9);
+--------------------------------------------+
| EXISTS(SELECT 1 FROM table_1 WHERE id = 9) |
+--------------------------------------------+
| 0 |
+--------------------------------------------+
1 row in set (0.00 sec)
使用别名:
mysql> SELECT EXISTS(SELECT 1 FROM table_1 WHERE id = 1) AS mycheck;
+---------+
| mycheck |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
select * from table where condition=value
(1 total, Query took 0.0052 sec)
select exists(select * from table where condition=value)
(1 total, Query took 0.0008 sec)
select count(*) from table where condition=value limit 1)
(1 total, Query took 0.0007 sec)
select exists(select * from table where condition=value limit 1)
(1 total, Query took 0.0006 sec)