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

  • Home
  • SEARCH
  • 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 7183971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:06:17+00:00 2026-05-28T18:06:17+00:00

Can somebody tell me why mysql is not using the correct index in the

  • 0

Can somebody tell me why mysql is not using the correct index in the following query

SELECT `Slugs`.`slug` FROM `slugs` AS `Slugs`   
WHERE `Slugs`.`country_id` = 1 AND `Slugs`.`expired` = 0    
LIMIT 308400,300

I have created a cmposite index for the two columns refered in the where clause

Table Structure for table slugs

CREATE TABLE IF NOT EXISTS `slugs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `slug` varchar(255) NOT NULL,
  `post_fields` text NOT NULL,
  `slugdata` text NOT NULL,
  `updated_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `country_id` int(11) NOT NULL DEFAULT '1',
  `expired` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug_2` (`slug`,`country_id`),
  KEY `updated_date` (`updated_date`),
  KEY `country_id` (`country_id`),
  KEY `slug` (`slug`),
  KEY `expired` (`expired`),
  KEY `country_id_2` (`country_id`,`expired`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1612485 ;

Indexes:

Keyname      Type   Unique  Packed  Field        Cardinality     Collation  Null    Comment
PRIMARY      BTREE  Yes     No      id               1406994      A     
slug_2       BTREE  Yes     No      slug             1406994      A     
                                    country_id       1406994      A 
updated_date BTREE  No      No      updated_date          21      A     
country_id   BTREE  No      No      country_id            21      A     
slug         BTREE  No      No      slug             1406994      A     
expired      BTREE  No      No      expired               21      A     
country_id_2 BTREE  No      No      country_id            21      A     
                                    expired               21      A

Output from Explain

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  Slugs   ref     country_id,expired,country_id_2     country_id  4   const   670284  Using where
  • 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-28T18:06:18+00:00Added an answer on May 28, 2026 at 6:06 pm

    What do you mean it’s not using the correct index?

    The cardinality for both country_id and country_id_2 is very low, and is in fact the same for both indexes – so there’s no benefit in using the index based on 2 columns. Which implies that there are very few records with expired<>0.

    But then why does the expired index have a cardinality of 21 too?

    What’s the output from:

    SELECT 'expired' AS fld
    , COUNT(*) AS distinct_values
    , AVG(n) AS rows
    FROM 
    (SELECT expired AS v
     , COUNT(*) AS n 
     FROM slugs 
     GROUP BY expired) ilv1
    UNION
    SELECT 'country_id',
    , COUNT(*) AS distinct_values
    , AVG(n) AS rows
    FROM 
    (SELECT country_id AS v
     , COUNT(*) AS n 
     FROM slugs 
     GROUP BY country_id) ilv2
        SELECT 'expired:country_id',
    , COUNT(*) AS distinct_values
    , AVG(n) AS rows
    FROM 
    (SELECT CONCAT(expired, country_id) AS v
     , COUNT(*) AS n 
     FROM slugs 
     GROUP BY CONCAT(expired, country_id)) ilv3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We want to store product prices and weight (kg/pound) in MySQL. Can somebody tell
can somebody tell me why my code is not working? class Connection { public
TO_CHAR(( TO_DATE(calendar_key,'YYYYMMDD' )+ 1),'IW') week Can somebody tell me what's happening in this query?
Can somebody tell me how to prevent exe file from being uploaded in a
can somebody tell me how to use this class timers from python in my
Can somebody tell me why code analysis shows issues from generated files? I do
can somebody tell me how to populate jquery fullcalendar events using ajax. There is
Can somebody tell me how to focus the html textfield using JavaScript? I am
Can somebody tell me why I ALWAYS get "1" from : public boolean onTouchEvent(MotionEvent
I'm using garagedoorjQuery in my drupal page. Can somebody tell me, why this code

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.