我想找出一个词在任何一种语言中的“排名”。具有1级别的单词意味着它是语言中最常用的单词。
有两个MySQL表。
word表中大约有100万行。下面是一个有效的查询,但它需要7-10秒。有更好的方法来写这个查询吗?
select count(*) + 1
from (
select word.body, count(*) as count
from word
join document on word.document_id = document.id
where document.language = 'english'
group by word.body
) t
where count > (select count
from (
select word.body, count(*) as count
from word
join document on word.document_id = document.id
where document.language = 'english'
group by word.body
) t
where body = 'the')发布于 2017-12-15 19:17:23
最初,我想建议使用HAVING子句,但在这种情况下,我不认为这会有帮助,因为查询选择聚合计数并使用它进行比较。
一种选择是使用通用表表达式(CTE),但很明显直到MySQL版本8才引入它们。另一种选择是制作视图:
CREATE VIEW countEnglishWord as select word.body, count(*) as count
from word
join document on word.document_id = document.id
where document.language = 'english'
group by word.body;像这样使用它:
select count(*) + 1, body
from countEnglishWord c
where count > (select count
from countEnglishWord t
where body = 'the'
)我还没试过一百万行.但在这个木琴中有3个。
https://codereview.stackexchange.com/questions/182873
复制相似问题