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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:08:02+00:00 2026-06-07T11:08:02+00:00

This is using MySQL 5.5. I cannot seem to convince MySQL to use indexes

  • 0

This is using MySQL 5.5.
I cannot seem to convince MySQL to use indexes for these queries and they are taking anywhere from 2-10 seconds to run on a table with 1.1 million rows.

Table:

CREATE TABLE `notifiable_events` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`key` varchar(255) NOT NULL,
`trigger_profile_id` int(10) unsigned DEFAULT NULL,
`model1` varchar(25) NOT NULL,
`model1_id` varchar(36) NOT NULL,
`model2` varchar(25) NOT NULL DEFAULT '',
`model2_id` varchar(36) NOT NULL DEFAULT '',
`event_data` text,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `key` (`key`),
KEY `notifiable_events__trigger_profile` (`trigger_profile_id`),
KEY `deleted` (`deleted`),
KEY `noti_evnts__m2` (`model2`),
KEY `noti_evnts__m1` (`model1`),
CONSTRAINT `notifiable_events__trigger_profile` FOREIGN KEY (`trigger_profile_id`) REFERENCES `profiles` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1177918 DEFAULT CHARSET=utf8

QUERY:

  SELECT * 
    FROM notifiable_events 
    WHERE (`model1` = 'page' AND `model1_id` = '54321') 
       OR (`model2` = 'page' AND `model2_id` = '12345');

EXPLAIN(S):

mysql> EXPLAIN EXTENDED SELECT * FROM notifiable_events WHERE (`model1` = 'page' AND `model1_id` = '922645') OR (`model2` = 'page' AND `model2_id` = '922645')\G

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: notifiable_events
             type: ALL
    possible_keys: noti_evnts__m2,noti_evnts__m1,noti_evnts__m1_m2
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 1033088
         filtered: 100.00
            Extra: Using where
    1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN EXTENDED SELECT * FROM notifiable_events WHERE (`model1` = 'page' AND `model1_id` = '922645') OR (`model1` = 'page' AND `model1_id` = '922645')\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: notifiable_events
         type: ref
possible_keys: noti_evnts__m1,noti_evnts__m1_m2
          key: noti_evnts__m1
      key_len: 77
          ref: const
         rows: 1
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN EXTENDED SELECT * FROM notifiable_events WHERE (`model2` = 'page' AND `model2_id` = '922645') OR (`model2` = 'page' AND `model2_id` = '922645')\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: notifiable_events
         type: ref
possible_keys: noti_evnts__m2
          key: noti_evnts__m2
      key_len: 77
          ref: const
         rows: 428920
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

You can see that if I only use model1 or only use model2 then it will use the index, however, as soon as I try to use both of them together it gives up on the index entirely and does a full table scan.
I have already tried FORCE INDEX and I have tried a combination of multi-key indexes (and I left one in place for this table as an example). I have also tried rearranging the order of elements in the query but that doesn’t seem to have any effect either.

UPDATE:
I forgot to mention that I already tried ANALYZE and OPTIMIZE (multiple times each, no change). I also already tried an index on the *_id (the cardinality is very bad and those columns are mostly unique entries) and a multi index with all 4 columns being used in the query. No improvement or use of the index in either of those cases either.

It seems like it should be really easy to use an index to limit the rows being checked here so I hope I am just missing something.

  • 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-06-07T11:08:04+00:00Added an answer on June 7, 2026 at 11:08 am

    or clauses goof up query optimization sometimes.

    You could try a union, something like this. It may reactivate the indexes.

    SELECT * 
      FROM notifiable_events 
     WHERE  `model1` = 'page' AND `model1_id` = '54321'
     UNION
    SELECT * 
      FROM notifiable_events 
     WHERE `model2` = 'page' AND `model2_id` = '12345'
    

    Edit, to answer the question in the comment

    If you’re trying to update records using this kind of selection scheme, you can try this:

    UPDATE notifiable_events
       SET what=ever,
           what=ever,
           what=else 
     WHERE id IN (
       SELECT id 
         FROM notifiable_events 
        WHERE  `model1` = 'page' AND `model1_id` = '54321'
        UNION
       SELECT id 
         FROM notifiable_events 
        WHERE `model2` = 'page' AND `model2_id` = '12345'
     )
    

    (Note that it’s helpful when you’re using Stackoverflow to explain what you’re actually trying to do. It is of course OK to reduce a complex problem to a simpler one if you can, but saying you’re doing a SELECT when you’re actually doing an UPDATE is, umm, an oversimplification.)

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

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I am using MySQL. I have several lookups like this: SELECT * from users
I've never had to use mysqli until today and cannot seem to get this
I've found a few answers for this using mySQL alone, but I was hoping
Currently using MySQL version 5.1.6 This is my first real world build and so
I'm using MySQL. This is table i have Level Programmingtime Clientname projectid 0 128
This is a winform and I'm using mysql as a database, here is my
I'm using MySQL with PHP. This is like my table: (I'm using 3 values,
I easily get Week Number using MySql Week function like this WEEK(SYSDATE()) I just
I'm using MySQL 5, and I need to do this sentence to get the

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.