我遇到了MySQL(innoDB)5.0的严重问题.
使用非常意外的查询计划执行非常简单的SQL查询.
查询:
SELECT
SQL_NO_CACHE
mbCategory.*
FROM
MBCategory mbCategory
INNER JOIN ResourcePermission as rp
ON rp.primKey = mbCategory.categoryId
where mbCategory.groupId = 12345 AND mbCategory.parentCategoryId = 0
limit 20;
Run Code Online (Sandbox Code Playgroud)
MBCategory - 包含216583行
ResourcePermission - 包含3098354行.
在MBCategory中我有多个索引(列在索引中排序):
Primary (categoryId)
A (groupId,parentCategoryId,categoryId)
B (groupId,parentCategoryId)
Run Code Online (Sandbox Code Playgroud)
在ResourcePermission中,我有多个索引(列在索引中排序):
Primary - on some column
A (primKey).
Run Code Online (Sandbox Code Playgroud)
当我查看查询计划时,Mysql更改表序列并首先从ResourcePermission中选择行,然后它加入MBCategory表(疯狂的想法)并且需要很长时间.所以我添加STRAIGHT_JOIN
了强制innodb引擎使用正确的表序列:
SELECT
STRAIGHT_JOIN SQL_NO_CACHE
mbCategory.*
FROM
MBCategory
mbCategory
INNER JOIN ResourcePermission as rp
ON rp.primKey = mbCategory.categoryId
where mbCategory.groupId = 12345 AND mbCategory.parentCategoryId = 0
limit 20;
Run Code Online (Sandbox Code Playgroud)
但是这里的第二个问题是材料:在我看来,mysql应该index …