用户独有的单词 - 不在其中
我这里有一个非常简单的表结构。只是与简单 user_id 相关的单词列表。
Word Table:
word - varchar(50)
user_id - integer
我需要找到一个用户使用但其他用户未使用的单词。目前我正在这样做,它在 Postgresql (9.0.3) 上以 200k 字(~.3-.5 秒)工作正常,但在具有相同数据(5+ 分钟)的 MySQL (5.1.54) 上完全失败仍在运行)。所有使用的列都已建立索引。
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select word from words where user_id <> 99 group by word)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
1)有人知道更好的方法吗?
2)有人知道为什么它在MySql上完全失败吗?
编辑:这修复了 MySQL 上的问题,从 5 分钟以上缩短到 10-20 毫秒 - 感谢 Borealid
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select distinct word from words where user_id <> 99)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
谢谢。
I have a very simple table structure here. Just a list of words related to a simple user_id.
Word Table:
word - varchar(50)
user_id - integer
I need to find words used by one user that are not used by other users. Currently I'm doing this and it works alright on Postgresql (9.0.3) at 200k words (~.3-.5 seconds) and completely falls over on MySQL (5.1.54) with the same data (5+ mins and it is still running). All used columns are indexed.
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select word from words where user_id <> 99 group by word)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
1) Anyone know of a better way to do this?
2) Anyone know why it is completely failing on MySql?
EDIT: This fixes the issue on MySQL, from 5 mins+ to 10-20ms - Thanks Borealid
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select distinct word from words where user_id <> 99)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 NOT EXISTS():
确保您在 user_id 和单词(或组合)上有索引,使用解释来查看查询计划以及最适合您的内容。
======
编辑:还可以尝试使用 IS NULL 的 LEFT JOIN 解决方案:
尝试在两列上建立索引:
Try NOT EXISTS():
Make sure you have an index on the user_id and word (or a combination), use explain to see the queryplan and what works best for you.
======
Edit: Also try the LEFT JOIN solution using a IS NULL:
Try an index on both columns: