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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:00:58+00:00 2026-06-04T16:00:58+00:00

I run into an interesting problem I want to understand. I have a table:

  • 0

I run into an interesting problem I want to understand. I have a table:

        `id` BIGINT(20) unsigned NOT NULL AUTO_INCREMENT,
        `user_id` BIGINT(20) unsigned NOT NULL,

        `followers_count` INT(10) unsigned NOT NULL DEFAULT 0,
        `friends_count` INT(10) unsigned NOT NULL DEFAULT 0,
        `statuses_count` INT(10) unsigned NOT NULL DEFAULT 0,
        `favourites_count` INT(10) unsigned NOT NULL DEFAULT 0,
        `listed_count` INT(10) unsigned NOT NULL DEFAULT 0,

        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

        PRIMARY KEY (`id`)

I execute the following query:

SELECT followers_count,friends_count,statuses_count,favourites_count,listed_count, UNIX_TIMESTAMP(DATE(created_at)) AS id FROM user_track WHERE created_at>=DATE_SUB(NOW(),INTERVAL 14 DAY) AND user_id=’1234567′

Now, the interesting part:

1: With the following index on that table, the query takes several minutes to complete:

INDEX user_numbers (created_at,user_id,followers_count,friends_count,statuses_count,favourites_count,listed_count)

Explain query with the above index 1:

id  select_type table       type    possible_keys   key             key_len     ref     rows    Extra
1   SIMPLE      user_track  range   user_numbers    user_numbers    12          NULL    1119318 Using where; Using index

2: However, with the following index on that table, it takes less than 200ms:

INDEX user_report (user_id,id,created_at,followers_count,friends_count,statuses_count,favourites_count,listed_count)

Explain query with the above index 2:

id  select_type table       type    possible_keys   key             key_len     ref     rows    Extra
1   SIMPLE      user_track  ref     user_report     user_report     8           const   1       Using where; Using index

By explaining the query I see that the first index causes a lot of rows to be scanned, while the second index has “ref: const” and only a few rows scanned. But I would like to understand why this happens.

  • 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-04T16:00:59+00:00Added an answer on June 4, 2026 at 4:00 pm

    As stated in the manual:

    If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).

    MySQL cannot use the index to perform lookups if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:

    SELECT * FROM tbl_name WHERE col1=val1;
    SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
    
    SELECT * FROM tbl_name WHERE col2=val2;
    SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;

    If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

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

Sidebar

Related Questions

I run into this problem. I have a textarea which I only want to
I have run into an interesting problem. I am currently developing php page and
I have run into a very interesting problem trying to debug my custom PHP
Summary: I've run into an interesting problem, and I'm not quite sure how to
I have run into an interesting problem. I have inherited a database for daemon
I have run into an interesting problem which I'm pretty sure is the fault
I have run into an interesting problem lately: Lets say I have an array
I have run into an interesting problem with Entity Framework and based on the
I run into an interesting problem while was using combos in input form. My
I've run into an interesting problem. At least in Vista, getSystemMetrics(SM_CXSCREEN) returns an incorrect

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.