首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户特有的单词-在

用户特有的单词-在
EN

Stack Overflow用户
提问于 2011-02-22 14:05:47
回答 1查看 94关注 0票数 1

我这里的桌子结构很简单。只是与一个简单的user_id相关的单词列表。

代码语言:javascript
复制
Word Table: 
    word - varchar(50)
    user_id - integer

我需要找到一个用户使用的词,而其他用户没有使用。目前,我正在执行此操作,它在Postgresql (9.0.3)上的工作时间为200 k字(~3.5-5秒),并且在MySQL上完全崩溃(5.1.54),它具有相同的数据(5+ mins并且仍在运行)。所有已使用的列都被编入索引。

代码语言:javascript
复制
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 mins+到10-20 to谢谢Borealid

代码语言:javascript
复制
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

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-22 14:27:09

尝试不存在():

代码语言:javascript
复制
SELECT  
    w1.word, 
    COUNT(w1.word) as count
FROM 
    words w1
WHERE         
    NOT EXISTS (
        SELECT 1 
        FROM 
            words w2 
        WHERE 
            w2.user_id <> 99 
        AND 
            w1.word = w2.word 
        )
AND 
    w1.user_id = 99
GROUP BY 
    w1.word
ORDER BY 
    count DESC 
LIMIT 20;

确保您在user_id和word (或组合)上有一个索引,使用explain查看查询计划,以及什么对您最合适。

======编辑:还可以使用IS尝试左联接解决方案:

代码语言:javascript
复制
SELECT
    w1.word,
    COUNT(w1.word) AS count
FROM
    words w1
        LEFT JOIN words w2 ON (w1.word = w2.word AND w1.user_id <> w2.user_id)
WHERE
    w1.user_id = 99
AND
    w2.word IS NULL
GROUP BY
    w1.word
ORDER BY 
    count DESC 
LIMIT 20;

在这两列上尝试索引:

代码语言:javascript
复制
CREATE INDEX idx_word_user ON words ( word, user_id);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5079145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档