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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:02:55+00:00 2026-06-15T17:02:55+00:00

I’ve checked numerous other SO posts and MySQL docs but can’t seem to get

  • 0

I’ve checked numerous other SO posts and MySQL docs but can’t seem to get an answer on why an index isn’t being used, and how to force it to be used – I can see many others are having similar problems, but can’t find a solution.

The table looks like this

CREATE TABLE `countries_ip` (
`ipfrom` INT(10) UNSIGNED ZEROFILL NOT NULL,
`ipto` INT(10) UNSIGNED ZEROFILL NOT NULL,
`countrySHORT` CHAR(2) NULL DEFAULT NULL,
`country_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`ipfrom`, `ipto`, `country_id`),
INDEX `from_to_index` (`ipfrom`, `ipto`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

Not sure why the “from_to_index” is there – seems redundant to me. But anyway, the EXPLAIN query looks like this

EXPLAIN SELECT *
        FROM track_report t, countries_ip ip
        WHERE t.ip BETWEEN ip.ipfrom AND ip.ipto

and the result of the EXPLAIN is as follows:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  t   ALL getwebmaster    NULL    NULL    NULL    36291   
1   SIMPLE  ip  ALL PRIMARY,from_to_index   NULL    NULL    NULL    153914  Range checked for each record (index map: 0x3)

As you can see, the PRIMARY KEY from the countries_ip table isn’t being used and so the query takes a LONG time (countries_ip has over 150k records)

I’m probably missing something simple, but any advice would be appreciated on how I can optimize this query. Thanks in advance.

  • 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-15T17:02:56+00:00Added an answer on June 15, 2026 at 5:02 pm

    It might help to define an index on track_report.ip. See SQL Fiddle.

    I modified the where clause to do an explicit comparison, and now it uses the from_to_index.

    SELECT ip
    FROM track_report t, countries_ip ip
    where t.ip >= ip.ipfrom and t.ip <= ip.ipto
    

    See SQL Fiddle.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was

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.