简体   繁体   中英

MySQL column search for comma separated items - FIND_IN_SET or LIKE

I have a table which contains a comma separated list of URLs. Ignoring the fact that the schema should be updated. Both of the following statements work:

SELECT id FROM website WHERE url LIKE '%example.com%';

SELECT id FROM website WHERE FIND_IN_SET('example.com', url);

Is there a good way to measure performance of the queries? Is there a better way to do it (without updating the schema)?

Using EXPLAIN I get the following results:

+-------------+--------+---------+------+----------+
| select_type | type   | key     | rows | filtered |
+-------------+--------+---------+------+----------+
| SIMPLE      | ALL    | NULL    |  5   |   20.00  | (LIKE)
| SIMPLE      | ALL    | NULL    |  5   |   100.00 | (FIND_IN_SET)
+-------------+-------+----------+------+----------+   

In MariaDB you can see it with: SET profiling=ON; and SHOW PROFILE; after the query.

Sample

MariaDB [test]>  SET profiling=ON;
Query OK, 0 rows affected (0.000 sec)

MariaDB [test]>  SELECT FIND_IN_SET('2', '1,2,3,4,5');
+-------------------------------+
| FIND_IN_SET('2', '1,2,3,4,5') |
+-------------------------------+
|                             2 |
+-------------------------------+
1 row in set (0.000 sec)

MariaDB [test]>  SHOW PROFILE;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| Starting                       | 0.000025 |
| Waiting for query cache lock   | 0.000005 |
| Init                           | 0.000004 |
| Checking query cache for query | 0.000047 |
| Checking permissions           | 0.000007 |
| Opening tables                 | 0.000011 |
| After opening tables           | 0.000007 |
| Init                           | 0.000014 |
| Optimizing                     | 0.000012 |
| Executing                      | 0.000010 |
| End of update loop             | 0.000005 |
| Query end                      | 0.000003 |
| Commit                         | 0.000004 |
| Closing tables                 | 0.000003 |
| Starting cleanup               | 0.000004 |
| Freeing items                  | 0.000006 |
| Updating status                | 0.000014 |
| Reset for next command         | 0.000004 |
+--------------------------------+----------+
18 rows in set (0.000 sec)

MariaDB [test]>

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