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 3799218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:41:53+00:00 2026-05-19T13:41:53+00:00

I am trying to force MySQL to use two indexes. I am joining a

  • 0

I am trying to force MySQL to use two indexes. I am joining a table and I want to utilize the cross between the two indexes. The specific term is Using intersect and here is a link to MySQL documentation:

http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html

Is there any way to force this implementation? My query was using it (and it sped stuff up), but now for whatever reason it has stopped.

Here is the JOIN I want to do this on. The two indexes I want the query to use are scs.CONSUMER_ID_1 and scs_CONSUMER_ID_2

JOIN survey_customer_similarity AS scs
    ON cr.CONSUMER_ID=scs.CONSUMER_ID_2 
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_1 
    OR cr.CONSUMER_ID=scs.CONSUMER_ID_1 
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_2
  • 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-19T13:41:54+00:00Added an answer on May 19, 2026 at 1:41 pm

    See MySQL Docs for FORCE INDEX.

    JOIN survey_customer_similarity AS scs 
    FORCE INDEX (CONSUMER_ID_1,CONSUMER_ID_2)
    ON
    cr.CONSUMER_ID=scs.CONSUMER_ID_2 
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_1 
    OR cr.CONSUMER_ID=scs.CONSUMER_ID_1 
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_2
    

    As TheScrumMeister has pointed out below, it depends on your data, whether two indexes can actually be used at once.


    Here’s an example where you need to force the table to appear twice to control the query execution and intersection.

    Use this to create a table with >100K records, with roughly 1K rows matching the filter i in (2,3) and 1K rows matching j in (2,3):

    drop table if exists t1;
    create table t1 (id int auto_increment primary key, i int, j int);
    create index ix_t1_on_i on t1(i);
    create index ix_t1_on_j on t1(j);
    insert into t1 (i,j) values (2,2),(2,3),(4,5),(6,6),(2,6),(2,7),(3,2);
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i*2, j*2+i from t1;
    insert into t1 (i,j) select i, j from t1;
    insert into t1 (i,j) select i, j from t1;
    insert into t1 (i,j) select 2, j from t1 where not j in (2,3) limit 1000;
    insert into t1 (i,j) select i, 3 from t1 where not i in (2,3) limit 1000;
    

    When doing:

    select t.* from t1 as t where t.i=2 and t.j=3 or t.i=3 and t.j=2
    

    you get exactly 8 matches:

    +-------+------+------+
    | id    | i    | j    |
    +-------+------+------+
    |     7 |    3 |    2 |
    | 28679 |    3 |    2 |
    | 57351 |    3 |    2 |
    | 86023 |    3 |    2 |
    |     2 |    2 |    3 |
    | 28674 |    2 |    3 |
    | 57346 |    2 |    3 |
    | 86018 |    2 |    3 |
    +-------+------+------+
    

    Use EXPLAIN on the query above to get:

    id | select_type | table | type  | possible_keys         | key        | key_len | ref  | rows | Extra
    1  | SIMPLE      | t     | range | ix_t1_on_i,ix_t1_on_j | ix_t1_on_j | 5       | NULL | 1012 | Using where
    

    Even if we add FORCE INDEX to the query on two indexes EXPLAIN will return the exact same thing.

    To make it collect across two indexes, and then intersect them, use this:

    select t.* from t1 as a force index(ix_t1_on_i)
    
    join t1 as b force index(ix_t1_on_j) on a.id=b.id
    
    where a.i=2 and b.j=3 or a.i=3 and b.j=2
    

    Use that query with explain to get:

    id | select_type | table | type  | possible_keys | key        | key_len | ref  | rows | Extra
    1  | SIMPLE      | a     | range | ix_t1_on_i    | ix_t1_on_i | 5       | NULL | 1019 | Using where
    1  | SIMPLE      | b     | range | ix_t1_on_j    | ix_t1_on_j | 5       | NULL | 1012 | Using where; Using index
    

    This proves that the indexes are being used. But that may or may not be faster depending on many other factors.

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

Sidebar

Related Questions

I'm trying to force an inherited class to use a custom attribute. I'm creating
I am trying to grab a visual force element using jQuery and having some
I'm trying to force Webkit to use proxy definde in APN. My proxy configuration:
I'm trying to intercept any DELETE commands against a particular table. MySQL supports triggers
I'm trying to force Safari or IE7 to open a new page using a
I have been trying to force images to download using PHP Headers but there
I'm trying force BigDecimal to use symbols (Ex. Instead of Decimal.multiply, do Decimal *),
I am trying to force an iframe to the exact same height as the
I'm trying to force ssl on certain pages, but also on a directory. But
i'm trying to force a div to have a certain height depending on his

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.