Mysql 优化以避免 table 扫描
Mysql optimize to avoid table scan
请考虑以下内容table:
_____________________
| sentence_word |
|---------|---------|
| sent_id | word_id |
|---------|---------|
| 1 | 1 |
| 1 | 2 |
| ... | ... |
| 2 | 4 |
| 2 | 1 |
| ... | ... |
我想用这个table结构来存储句子中的单词。现在我想找出句子中哪些单词与特定单词在一起。结果应如下所示:
_____________________
| word_id | counted |
|---------|---------|
| 5 | 1000 |
| 7 | 800 |
| 3 | 600 |
| 1 | 400 |
| 2 | 100 |
| ... | ... |
查询如下所示:
SELECT
word_id,
COUNT(*) AS counted
FROM sentence_word
WHERE sentence_word.sent_id IN (SELECT
sent_id
FROM sentence_word
WHERE word_id = [desired word]
)
AND word_id != [desired word]
GROUP BY word_id
ORDER BY counted DESC;
查询正常运行,但它始终扫描完整 table。我为 sent_id 和 word_id 创建了一个索引。你有什么优化它的想法,它不需要一直扫描完整的 table 吗?
您可以尝试这样的自连接:
SELECT COUNT(DISTINCT sw1.word_id)
FROM sentence_word sw1
JOIN sentence_word sw2 ON (
sw1.sent_id = sw2.sent_id
AND sw2.word_id = [your word id]
)
WHERE sw1.word_id != [your word id]
甚至更好
SELECT COUNT(DISTINCT sw1.word_id)
FROM sentence_word sw1
JOIN sentence_word sw2 ON (
sw1.sent_id = sw2.sent_id
AND sw2.word_id = [your word id]
AND sw2.word_id != sw1.word_id
)
请考虑以下内容table:
_____________________
| sentence_word |
|---------|---------|
| sent_id | word_id |
|---------|---------|
| 1 | 1 |
| 1 | 2 |
| ... | ... |
| 2 | 4 |
| 2 | 1 |
| ... | ... |
我想用这个table结构来存储句子中的单词。现在我想找出句子中哪些单词与特定单词在一起。结果应如下所示:
_____________________
| word_id | counted |
|---------|---------|
| 5 | 1000 |
| 7 | 800 |
| 3 | 600 |
| 1 | 400 |
| 2 | 100 |
| ... | ... |
查询如下所示:
SELECT
word_id,
COUNT(*) AS counted
FROM sentence_word
WHERE sentence_word.sent_id IN (SELECT
sent_id
FROM sentence_word
WHERE word_id = [desired word]
)
AND word_id != [desired word]
GROUP BY word_id
ORDER BY counted DESC;
查询正常运行,但它始终扫描完整 table。我为 sent_id 和 word_id 创建了一个索引。你有什么优化它的想法,它不需要一直扫描完整的 table 吗?
您可以尝试这样的自连接:
SELECT COUNT(DISTINCT sw1.word_id)
FROM sentence_word sw1
JOIN sentence_word sw2 ON (
sw1.sent_id = sw2.sent_id
AND sw2.word_id = [your word id]
)
WHERE sw1.word_id != [your word id]
甚至更好
SELECT COUNT(DISTINCT sw1.word_id)
FROM sentence_word sw1
JOIN sentence_word sw2 ON (
sw1.sent_id = sw2.sent_id
AND sw2.word_id = [your word id]
AND sw2.word_id != sw1.word_id
)