gpt4 book ai didi

mysql - 如何获取大表中匹配字符串的数量

转载 作者:行者123 更新时间:2023-11-29 08:28:49 24 4
gpt4 key购买 nike

我有一个具有以下结构的表:

+-----+-------------------+
| ID | Name |
+-----+-------------------+
| 1 | abc |
+-----+-------------------+
| 2 | abc (duplicate) |
+-----+-------------------+
| 3 | bcd |
+-----+-------------------+
| 4 | bcd (duplicate) |
+-----+-------------------+
| 5 | bcd (duplicate) |
+-----+-------------------+
| 6 | efg |
+-----+-------------------+
| 7 | hij |
+-----+-------------------+

我需要计算每个 Name 的出现次数(包括 (duplicate)),即:

+-------------------+--------+
| Name | Count |
+-------------------+--------+
| abc | 2 |
+-------------------+--------+
| bcd | 3 |
+-------------------+--------+
| efg | 1 |
+-------------------+--------+
| hij | 1 |
+-------------------+--------+

我想提一下,Name列实际上是TINYTEXT类型。其中将会有很多行:5396 已处于测试模式。我尝试通过 TRIM(REPLACE(Name, '(duplicate)', '')) 进行表的自连接和分组:

SELECT
DISTINCT TRIM(REPLACE(`t`.`Name`, '(duplicate)', '')) as `name`,
COUNT(`s`.`ID`) as `count`
FROM
`Table` as `t` INNER JOIN `Table` as `s` ON
TRIM(REPLACE(`t`.`Name`, '(duplicate)', '')) LIKE TRIM(REPLACE(`s`.`Name`, '(duplicate)', ''))
GROUP BY 1;

而且......好吧,在我的开发机器上花了122.62秒(?!),结果为 4846 行。

<小时/>

问题1:这是正确的方法吗?

问题2:有什么办法可以让它更快吗?

如有任何帮助,我们将不胜感激。

最佳答案

只需删除“重复”文本:

select replace(name, ' (duplicate)', ''), count(*)
from mytable
group by 1

关于mysql - 如何获取大表中匹配字符串的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17229011/

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