MySQL列搜索逗号分隔的项目-FIND_IN_SET或LIKE

时间:2018-12-11 11:20:02

标签: mysql sql

我有一个表,其中包含用逗号分隔的URL列表。忽略应该更新架构的事实。以下两个语句均有效:

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

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

是否有衡量查询性能的好方法?有没有更好的方法(无需更新架构)?

使用“说明”,我得到以下结果:

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

1 个答案:

答案 0 :(得分:0)

在MariaDB中,您可以在查询后看到SET profiling=ON;SHOW PROFILE;

样品

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]>