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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:01:56+00:00 2026-05-12T21:01:56+00:00

We have a MySQL table with about 3.5 million IP entries. The structure: CREATE

  • 0

We have a MySQL table with about 3.5 million IP entries.

The structure:

CREATE TABLE IF NOT EXISTS `geoip_blocks` (
  `uid` int(11) NOT NULL auto_increment,
  `pid` int(11) NOT NULL,
  `startipnum` int(12) unsigned NOT NULL,
  `endipnum` int(12) unsigned NOT NULL,
  `locid` int(11) NOT NULL,
  PRIMARY KEY  (`uid`),
  KEY `startipnum` (`startipnum`),
  KEY `endipnum` (`endipnum`)
) TYPE=MyISAM  AUTO_INCREMENT=3538967 ;

The problem: A query takes more than 3 seconds.

SELECT uid FROM `geoip_blocks` WHERE 1406658569 BETWEEN geoip_blocks.startipnum AND geoip_blocks.endipnum LIMIT 1

– about 3 seconds

SELECT uid FROM `geoip_blocks` WHERE startipnum < 1406658569 and endipnum > 1406658569 limit 1

– no gain, about 3 seconds

How can this be improved?

  • 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-12T21:01:56+00:00Added an answer on May 12, 2026 at 9:01 pm

    The solution to this is to grab a BTREE/ISAM library and use that (like BerkelyDB). Using ISAM this is a trivial task.

    Using ISAM, you would set your start key to the number, do a “Find Next”, (to find the block GREATER or equal to your number), and if it wasn’t equal, you’d “findPrevious” and check that block. 3-4 disk hits, shazam, done in a blink.

    Well, it’s A solution.

    The problem that is happening here is that SQL, without a “sufficiently smart optimizer”, does horrible on this kind of query.

    For example, your query:

    SELECT uid FROM `geoip_blocks` WHERE startipnum < 1406658569 and endipnum > 1406658569 limit 1
    

    It’s going to “look at” ALL of the rows that are “less than” 1406658569. ALL of them, then it’s going to scan them looking for ALL of the rows that match the 2nd criteria.

    With a 3.5m row table, assuming “average” (i.e. it hits the middle), welcome to a 1.75m row table scan. Even worse, and index table scan. Ideally MySQl will “give up” and “just” table scan, as it’s faster.

    Clearly, this is not what you want.

    @Andomar’s solution is basically forcing you to “block” to data space, via the “network” criteria. Effectively breaking your table in to 255 pieces. So, instead of scanning 1.75m rows, you get to scan 6800 rows, a marked improvement at a cost of you breaking your blocks up on the network boundary.

    There is nothing wrong with range queries in SQL.

    SELECT * FROM table WHERE id between X and Y
    

    is a, typically, fast query, as the optimizer can readily delimit the range of rows using the index.

    But, that’s not your query, because you are not ranging you main ID in this case (startipnum).

    If you “know” that your block sizes are within a certain range (i.e. none of your blocks, EVER, have more than, say, 1000 ips), then you can block your query by adding “WHERE startipnum between {ipnum – 1000} and {ipnum + 1000}”. That’s not really different than the network blocking that was proposed, but here you don’t have to maintain it as much. Of course, you can learn this with:

    SELECT max(endipnum - startipnum) FROM table
    

    to get an idea what your largest range is.

    Another option, which I’ve seen, have never used, but is actually, well, perfect for this, is to look at MySql’s Spatial Extensions, since that’s what this really is.

    This is designed more for GIS applications, but you ARE searching for something in ranges, and that’s a lot of what GIS apps do. So, that may well be a solution for you as well.

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

Sidebar

Ask A Question

Stats

  • Questions 276k
  • Answers 276k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't see any easy way of doing it from… May 13, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer The only way to know why is to check the… May 13, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer See my answer to this question. If you need more… May 13, 2026 at 2:47 pm

Related Questions

I want grouped ranking on a very large table, I've found a couple of
Scenario: I load some data into a local MySQL database each day, about 2
We have an InnoDB database that is about 70 GB and we expect it
Yeah, so I'm filling out a requirements document for a new client project and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.