I want to find out the 'rank' of a word in any given language. A word with a rank of 1
means it's the most commonly used word in the language.
There are two MySQL tables.
document:
- id: 1
- language: "english"
word:
- id: 2
- body: "the"
- document_id: 1
There are about 1 million rows in the word
table. Here is a query that works, but it takes 7-10 seconds. Is there a better way to write this query?
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')
word
table? And how often is the query above run? Perhaps some caching/memoization technique might help... \$\endgroup\$