感谢 MrCleanX 将我指向 EXPLAIN 函数。(由于此处的措辞更好,我删除了另一个问题)......当我将 ORDER BY 子句添加到我的查询时,我基本上在“检查的行”中得到了巨大的跳跃。
表:
packages 和 package_to_tag 都运行 MyISAM
结构如下:
包裹 日期被索引。
+----------------+------------------+----------------+
| aid(primary) | source | date(index) |
+----------------+------------------+----------------+
| 1 | CA | 2013-04-05 |
+----------------+------------------+----------------+
| 2 | FL | 2013-05-05 |
+----------------+------------------+----------------+
| 3 | UT | 2012-06-13 |
+----------------+------------------+----------------+
| 4 | VT | 2011-04-29 |
+----------------+------------------+----------------+
| 5 | CT | 2013-04-10 |
+----------------+------------------+----------------+
package_to_tag package-tag 上的唯一索引,并且package_aid 和 tag 都有索引
+---------------+------------------+
| package_aid | tag |
+---------------+------------------+
| 2 | sports |
+---------------+------------------+
| 2 | nba |
+---------------+------------------+
| 1 | food |
+---------------+------------------+
| 1 | burrito |
+---------------+------------------+
| 4 | hockey |
+---------------+------------------+
| 4 | sports |
+---------------+------------------+
| 3 | news |
+---------------+------------------+
| 5 | sports |
+---------------+------------------+
| 5 | nba |
+---------------+------------------+
我的基本查询是:
SELECT package_aid, source
FROM package_to_tag
RIGHT JOIN packages ON packages.aid = package_to_tag.package_aid
WHERE tag IN("sports","nba")
GROUP BY package_aid
HAVING COUNT(*) = 2
ORDER BY date DESC
LIMIT 500;
如果我“解释”给出的结果表明我正在检查 package_to_tag 表中的每条记录(在 150 万左右)。
但是,如果我去掉 ORDER BY 子句,这将减少到更易于管理的 30k。
完整的结果屏幕大写。
包:
package_to_tag:
用 ORDER BY 解释:
不带 ORDER BY 的解释:
package_to_tag 上的索引:
如何在不涉及 package_to_tag 表中的每一行的情况下保持排序?