Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:56:11+00:00 2026-05-25T16:56:11+00:00

I have matching Btree indices on 2 tables, but the explain plan says the

  • 0

I have matching Btree indices on 2 tables, but the explain plan says the engine is doing a full scan on one of the tables and it is very slow. When deployed both will start with ~750,000 rows, the relationship is em_localitems:em_submenuitemassoc => 1:n. My understanding is that the indices below with ** should work well in the query below as they are used left to right.

CREATE TABLE IF NOT EXISTS em_localitems (
  localitemid int(11) NOT NULL AUTO_INCREMENT,
  profitcenterid int(11) DEFAULT NULL,
  productid int(11) DEFAULT NULL,
  PRIMARY KEY (localitemid),
  UNIQUE KEY locationid (profitcenterid,productid),
  **KEY productid_2 (productid,profitcenterid)**
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;


CREATE TABLE IF NOT EXISTS em_submenuitemassoc (
  submenuitemassocid int(11) NOT NULL AUTO_INCREMENT,
  productid int(11) NOT NULL,
  profitcenterid int(11) NOT NULL DEFAULT '0',
  submenuid int(11) NOT NULL,
  enddate datetime DEFAULT NULL,
  PRIMARY KEY (submenuitemassocid),
  UNIQUE KEY productid (productid,profitcenterid,submenuid),
  **KEY productid_3 (productid,profitcenterid,submenuid)**
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

This is the query:

SELECT * from
em_submenuitemassoc sm
LEFT outer JOIN em_localitems li2 on li2.productid=sm.productid
  and li2.profitcenterid=sm.profitcenterid
  and sm.profitcenterid is not null

I also tried index hinting:

SELECT * from
em_submenuitemassoc sm **use index(productid_3)**
LEFT outer JOIN em_localitems li2 on li2.productid=sm.productid
  and li2.profitcenterid=sm.profitcenterid
  and sm.profitcenterid is not null

“show index from em_submenuitemassoc;” returns this:

+---------------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table               | Non_unique | Key_name       | Seq_in_index | Column_name        | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+
| em_submenuitemassoc |          1 | productid_3    |            1 | productid          | A         |        1136 |     NULL | NULL   |      | BTREE      |         | 
| em_submenuitemassoc |          1 | productid_3    |            2 | profitcenterid     | A         |        1136 |     NULL | NULL   |      | BTREE      |         | 
| em_submenuitemassoc |          1 | productid_3    |            3 | submenuid          | A         |        1136 |     NULL | NULL   |      | BTREE      |         | 
+---------------------+------------+----------------+--------------+--------------------+-----------+-------------+----------+--------+------+------------+---------+

This is the explain plan:

+----+-------------+-------+------+------------------------+-------------+---------+--------------------------------------------------+------+-------+
| id | select_type | table | type | possible_keys          | key         | key_len | ref                                              | rows | Extra |
+----+-------------+-------+------+------------------------+-------------+---------+--------------------------------------------------+------+-------+
|  1 | SIMPLE      | sm    | ALL  | NULL                   | NULL        | NULL    | NULL                                             | 1136 |       | 
|  1 | SIMPLE      | li2   | ref  | locationid,productid_2 | productid_2 | 10      | datatest.sm.profitcenterid,datatest.sm.productid |    1 |       | 
+----+-------------+-------+------+------------------------+-------------+---------+--------------------------------------------------+------+-------+

What’s so horribly wrong with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:56:11+00:00Added an answer on May 25, 2026 at 4:56 pm

    See that your query acces type for table sm is “ALL”. The optimizer knows it has to read all the rows of sm regardless of the join conditions.

    This is probably because you’re using LEFT OUTER JOIN which returns all the rows of the left table in the join, regardless of whether there are matching rows in the right table of the join.

    You’re also using SELECT * so it’ll have to fetch all the columns of sm anyway. If the query required only the columns in the index, it would be able to skip reading the base table.

    So there’s no benefit in this query to using the index, and MySQL gains more efficiency by skipping reading the index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
We have 2 databases that should have matching tables. I have an (In-Production) report
I have two very large tables, Table1 and Table2. They look like this: Table1
Unicode has a bunch of arrows, but many of them don't have matching right
I have working code but don't like it, its very unruby like to me.
I have the following pattern matching case in a scala function: def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int]
I have a PMS(Pantone Matching System) color value. I need to find the corresponding
I have a question about matching columns and replace this with a 1 if
I have found the following resources on Balanced Matching for .net Regexes: http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx http://blogs.msdn.com/bclteam/archive/2005/03/15/396452.aspx
I have directory A with files matching directory B. Directory A may have other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.