gpt4 book ai didi

mysql - GROUP_CONCAT 与 FIND_IN_SET,多个连接

转载 作者:行者123 更新时间:2023-12-04 02:15:59 57 4
gpt4 key购买 nike

我想检索设置了特定过滤器的项目。例如,红色或蓝色和小 的列表项应该只返回项目 apple。((red(2) or blue(4)) and small(5)) => apple

我找到了 2 个解决方案,但在我看来都过于复杂。第一个解决方案在我看来更优雅,因为当我想通过 AND 添加另一个过滤器时,它非常简单。而第二种解决方案将需要另一个 JOIN。我希望我忽略了一些东西并且有比这更好的解决方案。

问题,

  1. 有更好的解决方案吗?
  2. 如果没有更好的解决方案 - 哪个更快/推荐哪个?

项目表

| id | itemname |
├────┼──────────┤
| 1 | apple |
| 2 | orange |
| 3 | banana |
| 4 | melon |

过滤表

│ id │ filtername │
├────┼────────────┤
│ 1 │ orange │
│ 2 │ red │
│ 3 │ green │
│ 4 │ blue │
│ 5 │ small │
│ 6 │ medium │
│ 7 │ big │
│ 8 │ yellow │

item_filter

│ item_id │ filter_id │
├─────────┼───────────┤
│ 1 │ 2 │
│ 1 │ 3 │
│ 1 │ 5 │
│ 2 │ 1 │
│ 2 │ 5 │
│ 3 │ 6 │
│ 3 │ 8 │
│ 4 │ 3 │
│ 4 │ 7 │

基于 GROUP_CONCAT 和 FIND_IN_SET 的第一个解决方案

sqlfiddle:http://sqlfiddle.com/#!9/26f99/1/0

SELECT * FROM item
JOIN (
SELECT item_id, GROUP_CONCAT(filter_id) AS filters
FROM item_filter
GROUP BY item_id
) AS grp ON grp.item_id = item.id
WHERE (FIND_IN_SET(2,filters) OR FIND_IN_SET(4,filters)) AND FIND_IN_SET(5, filters)

第二种解决方案仅基于 JOIN 和 where 子句

sqlfiddle:http://sqlfiddle.com/#!9/f0b95/1/0

SELECT itemname FROM item
JOIN item_filter as filter1 on item.id=filter1.item_id
JOIN item_filter as filter2 on item.id=filter2.item_id
WHERE (filter1.filter_id=2 or filter1.filter_id=4) and filter2.filter_id=5

最佳答案

我不是 MySQL 专家,但这是我的两分钱。

您应该使用 MySQL EXPLAIN 函数来获取有关如何执行查询的详细信息: http://dev.mysql.com/doc/refman/5.7/en/explain-output.html

但在此之前,您应该为您的关系表添加一个组合键索引,即:item_filter 表;如果不这样做,EXPLAIN 结果将不相关,因为将为每个查询完全扫描后一个表。

现在,对您的两个查询运行解释,您会注意到从性能的角度您的第二个解决方案显然是最好的(并假设您将索引添加到 EXPLAIN 表):

mysql> EXPLAIN SELECT * FROM item
-> JOIN (
-> SELECT item_id, GROUP_CONCAT(filter_id) AS filters
-> FROM item_filter
-> GROUP BY item_id
-> ) AS grp ON grp.item_id = item.id
-> WHERE (FIND_IN_SET(2,filters) OR FIND_IN_SET(4,filters)) AND FIND_IN_SET(5, filters);
+----+-------------+-------------+-------+---------------+---------+---------+------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+--------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
| 1 | PRIMARY | item | ALL | PRIMARY | NULL | NULL | NULL | 4 | Using where; Using join buffer |
| 2 | DERIVED | item_filter | index | NULL | PRIMARY | 8 | NULL | 9 | Using index |
+----+-------------+-------------+-------+---------------+---------+---------+------+------+--------------------------------+
3 rows in set (0.00 sec)

mysql> EXPLAIN SELECT itemname FROM item
-> JOIN item_filter as filter1 on item.id=filter1.item_id
-> JOIN item_filter as filter2 on item.id=filter2.item_id
-> WHERE (filter1.filter_id=2 or filter1.filter_id=4) and filter2.filter_id=5;
+----+-------------+---------+--------+---------------+---------+---------+--------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+--------+---------------+---------+---------+--------------------+------+--------------------------+
| 1 | SIMPLE | item | ALL | PRIMARY | NULL | NULL | NULL | 4 | |
| 1 | SIMPLE | filter1 | ref | PRIMARY | PRIMARY | 4 | test.item.id | 1 | Using where; Using index |
| 1 | SIMPLE | filter2 | eq_ref | PRIMARY | PRIMARY | 8 | test.item.id,const | 1 | Using index |
+----+-------------+---------+--------+---------------+---------+---------+--------------------+------+--------------------------+
3 rows in set (0.01 sec)

mysql>

无需详细说明:

  • 解决方案一执行两次全表扫描,一次索引查找并读取 17 行(另外我不相信 GROUP_CONCATFIND_IN_SET 性能影响)。

  • 方案二进行单表全扫描,总共只读取6行。

查看EXPLAIN Join Types 文档以获取更多信息: http://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-join-types

关于mysql - GROUP_CONCAT 与 FIND_IN_SET,多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33610205/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com