mysql - 使用 like vs group by 聚合函数的 SQL 查询 - 性能

标签 mysql performance

我正在尝试获取每个应用程序 (app_id) 的第一个 SRC 条目。

图像表有 2 列(app_idsrc)。

两种解决方案: - 使用 MIN。 - 使用 LIKE

我有以下两个查询,我想知道哪个执行得更快或者它们是等价的?

我应该使用不同的指标或函数吗?

查询

MIN 与 GROUP BY

EXPLAIN
SELECT app_id,
       min(src)
FROM image
GROUP BY app_id ;

喜欢

EXPLAIN
SELECT app_id,
       i.src
FROM image i
WHERE i.src LIKE '%.0.jpg';

结果:

mysql> EXPLAIN select app_id, min(src) from image group by app_id limit   100000;
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+-------------+
| id | select_type | table | type  | possible_keys | key                         | key_len | ref  | rows  | Extra       |
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+-------------+
|  1 | SIMPLE      | image | index | NULL          | FK_x2mlprm4ootu8u253f7sd9rs | 768     | NULL | 52532 | Using index |
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+-------------+
1 row in set (0.00 sec)

mysql> EXPLAIN select app_id, i.src from image i where i.src like '%.0.jpg' limit 100000;
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+--------------------------+
| id | select_type | table | type  | possible_keys | key                         | key_len | ref  | rows  | Extra                    |
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+--------------------------+
|  1 | SIMPLE      | i     | index | NULL          | FK_x2mlprm4ootu8u253f7sd9rs | 768     | NULL | 52532 | Using where; Using index |
+----+-------------+-------+-------+---------------+-----------------------------+---------+------+-------+--------------------------+
1 row in set (0.00 sec)

说明

  • 存储的数据和结构使得两个查询产生相同的结果。
  • 数据的存储方式(来自应用程序逻辑)min(src) 将始终返回与 LIKE '%.0.jpg' 相同的方式
  • .0.jpg 是每个应用程序可能存在的最小 src,因为 .0.jpg 之前的所有内容对于属于同一 app< 的条目都是相同的
  • 请假设(并说明何时)列将/可以以最佳方式编入索引。可以对表格进行更改。

最佳答案

GROUP BY 查询应该是最快的查询,因为它主要基于索引,而 LIKE 查询仅部分使用索引(如EXPLAIN,它说的是 Using where; Using index),当然不是 i.src like '%.0.jpg' 部分.

MySQL Reference Manual据报道如下:

The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character. For example, the following SELECT statements use indexes:

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

由于在您的情况下参数以通配符开头,它不会使用索引,因此查询应该更慢。

关于mysql - 使用 like vs group by 聚合函数的 SQL 查询 - 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24286431/

相关文章:

mysql - Wordpress,大量帖子插入

php - Codeigniter 调用非对象上的成员函数 num_rows()

java - 性能问题 onDraw 耗时太长

algorithm - 大O,您如何计算/近似?

mysql - .NET + MySQL(或替代的基于 SQL 的数据库)是一个不错的选择吗?

php - MySQL 更新命令出错。 (在 PHP 中)

mysql - ruby on rails 的循环模型依赖

performance - 使用存储过程是否有重大的性能提升?

java - 快速休息调用执行

performance - 如何在 Go 中通过清晰的结构值优化性能?