简体   繁体   中英

SQL query using like vs group by aggregation function - performance

I am trying to get the first SRC entry for each application ( app_id ).

Image table has 2 columns ( app_id , src ).

Two solutions: - Using MIN . - Using LIKE .

I have the following two queries and am wondering which performs faster or are they both equivalent?

Should I use different metrics or functions?

Queries

MIN with GROUP BY

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

LIKE

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

Results:

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)

Clarifications

  • The data stored and structure is such that the two queries produce equivalent results.
  • The data is stored (from the application logic) in such a way that min(src) will always return the same as LIKE '%.0.jpg'
  • .0.jpg is the smallest src per app there can be, as everything preceding .0.jpg is identical for entries belonging to same app .
  • Please assume (and state when) that columns will/can be indexed in the best possible way. Changes can be made to the table.

The GROUP BY query should be the fastest one, since it is based mainly on an index while the LIKE query is using the index only partially (as indicated by the EXPLAIN , where it says Using where; Using index ), and surely not for the i.src like '%.0.jpg' part.

In the MySQL Reference Manual it is reported the following:

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%'; 

Since in your case the argument starts with a wildcard, it won't use the index, therefore the query should be slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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