mysql列搜索逗号分隔的项-在\集中查找\或类似项

beq87vna  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(305)

我有一个表,其中包含一个逗号分隔的URL列表。忽略应该更新架构的事实。以下两种说法都适用:

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

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

有没有一个好的方法来衡量查询的性能?有没有更好的方法(不更新模式)?
使用explain得到以下结果:

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

v6ylcynt1#

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

相关问题