I have a serious problem with MySQL (innoDB) 5.0.
A very simple SQL query is executed with a very unexpected query plan.
Inquiry:
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;
MBCategory - contains 216583 lines
ResourcePermission - contains 3098354 lines.
In MBCategory, I have several indexes (columns are ordered as in an index):
Primary (categoryId) A (groupId,parentCategoryId,categoryId) B (groupId,parentCategoryId)
In ResourcePermission, I have several indexes (columns are ordered as in the index):
Primary - on some column A (primKey).
When I look at the query plan, Mysql changes the sequence of the tables and first selects the rows from the ResourcePermission, then joins the MBCategory table (crazy idea) and it takes age. So I added STRAIGHT_JOIN to force the innodb engine to use the correct table sequence:
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;
But here is the second materialzie problem: In my opinion, mysql should use index A (primKey) in the join operation, instead it performs a Range on each record (index card: 0x400), and it takes a long time again! The force index does not help; mysql still performs the range checked for each record.
In MBCategory there are only 23 lines that fulfill the criteria, and after combining only 75 lines. How can I get mysql to select the correct index for this operation?