请考虑以下“推文”表:
tweet_id user_id text
----------------------------
1 1 lorem ipsum
2 1 lorem ipsum
3 2 pear
4 1 dolor
5 3 foo
6 1 dolor
7 1 dolor
8 3 bar
9 3 baz
10 4 happy
11 4 happy
12 2 apple
13 3 foo
14 4 happy实际上,该表包含来自大约80,000个用户的数百万条推文。其中许多用户都是垃圾帐户,但他们很难手动识别。根据经验,垃圾邮件帐户至少会发布3次相同的消息。这就是为什么我要填写下面的表格,"duplicates“在左边,"duplicates_tweets”在右边:
duplicate_id user_id text cnt duplicate_id tweet_id
-------------------------------------- ----------------------
1 1 lorem ipsum 2 1 1
2 1 dolor 3 1 2
3 2 pear 1 2 4
4 2 apple 1 2 6
5 3 foo 2 2 7
6 3 bar 1 3 3
7 3 baz 1 4 12
8 4 happy 3 5 5
5 13
6 8
7 9
8 10
8 11
8 14例如,我现在可以很容易地对cnt进行排序,并查看哪些用户发布的重复消息最多。然而,我的问题是,如何才能最有效地做到这一点。换句话说:什么查询可以最有效地填充这些表?是否可以只使用SQL,或者是否应该使用PHP作为中介,例如,从"tweets“数据库中提取一条tweet,扫描重复项,填充表,然后转到下一条tweet?我担心这需要很长时间才能完成,所以任何帮助都是非常感谢的!
发布于 2012-04-01 03:00:11
您可以使用MySQL中的REPLACE函数根据键更新或插入新行:
REPLACE duplicates
SELECT user_id, text
FROM (SELECT user_id, text, count(1) as count
FROM tweets
GROUP BY user_id, text
HAVING count(1) > 2))发布于 2012-04-01 03:04:54
也许,您可以先按user_id排序表"tweets“,然后再按文本排序:
SELECT * FROM tweets ORDER BY user_id DESC, text DESC然后,您可以在PHP中迭代结果:
<?php
// ...
$lastuser = -1;
$lasttext = "";
$ids = array();
while ($row = mysql_fetch_assoc($result)) {
if($row['user_id'] != $lastuser || $row['text'] != $lasttext) {
$ids = array();
}
$ids[] = $row['id'];
if(count($ids) >= 3) {
// flag items as spam
}
$lastuser = $row['user_id'];
$lasttext = $row['text'];
}
?>如果在MySQL数据库中使用索引,应该能够在大约N*log(N)内处理N条tweet。
发布于 2012-04-01 02:56:44
您是否只想列出可能的垃圾推文?试试这个:
SELECT
user_id,
text,
COUNT(DISTINCT tweet_id)
FROM
tweets
GROUP BY
user_id,
text
HAVING
COUNT(DISTINCT tweet_id) >= 3然后,您可以使用PHP遍历结果并INSERT/UPDATE一个duplicate_tweets表(尽管正如Chris K所提到的,当您可以使用这个查询时,您真的需要一个duplicate_tweets表吗?)。
https://stackoverflow.com/questions/9958713
复制相似问题