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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:57:23+00:00 2026-05-21T13:57:23+00:00

I have this query: SELECT `country` FROM `geoip_base` WHERE 1840344811 BETWEEN `start` AND `stop`

  • 0

I have this query:

SELECT `country`
FROM `geoip_base`
WHERE 1840344811 BETWEEN `start` AND `stop`

It’s badly use index (use, but parse big part of table) and work too slowly.
I tried use ORDER BY and LIMIT, but it hasn’t helped.

“start <= 1840344811 AND 1840344811 <= stop” works similar.

CREATE TABLE IF NOT EXISTS `geoip_base` (
  `start` decimal(10,0) NOT NULL,
  `stop` decimal(10,0) NOT NULL,
  `inetnum` char(33) collate utf8_bin NOT NULL,
  `country` char(2) collate utf8_bin NOT NULL,
  `city_id` int(11) NOT NULL,
  PRIMARY KEY  (`start`,`stop`),
  UNIQUE KEY `start` (`start`),
  UNIQUE KEY `stop` (`stop`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Table have 57,424 rows.

Explain for query “… BETWEEN START AND STOP ORDER BY START LIMIT 1”:
using key stop and get 24099 rows.
Without order and limit, mysql doesn’t use keys and gets all rows.

  • 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-21T13:57:24+00:00Added an answer on May 21, 2026 at 1:57 pm

    If your table is MyISAM, you can improve this query using SPATIAL indexes:

    ALTER TABLE
            geoip_base
    ADD     ip_range LineString;
    
    UPDATE  geoip_base
    SET     ip_range =
            LineString
                    (
                    Point(-1, `start`),
                    Point(1, `stop`)
                    );
    
    ALTER TABLE
            geoip_base
    MODIFY  ip_range NOT NULL;
    
    CREATE SPATIAL INDEX
            sx_geoip_range ON geoip_base (ip_range);
    
    SELECT  country
    FROM    geoip_base
    WHERE   MBRContains(ip_range, Point(0, 1840344811)
    

    This article may be of interest to you:

    • Banning IP’s

    Alternatively, if your ranges do not intersect (and from the nature of the database I except they don’t), you can create a UNIQUE index on geoip_base.start and use this query:

    SELECT  *
    FROM    geoip_base
    WHERE   1840344811 BETWEEN `start` AND `stop`
    ORDER BY
            `start` DESC
    LIMIT 1;
    

    Note the ORDER BY and LIMIT conditions, they are important.

    This query is similar to this:

    SELECT  *
    FROM    geoip_base
    WHERE   `start` <= 1840344811
            AND `stop` >= 1840344811
    ORDER BY
            `start` DESC
    LIMIT 1;
    

    Using ORDER BY / LIMIT makes the query to choose descending index scan on start which will stop on the first match (i. e. on the range with the start closest to the IP you enter). The additional filter on stop will just check whether the range contains this IP.

    Since your ranges do not intersect, either this range or no range at all will contain the IP you’re after.

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

Sidebar

Related Questions

I'm running this query SELECT country, countries.code, countries.lat, countries.lng, countries.zoom, worldip.start, worldip.end FROM countries,
I have this query: Select (Country.Name + ', ' + City.Name) as Location ...
I have this query: SELECT * From checkfinale where AssociateID = 51 AND `CompletedDate`
I have this query: select BUnit, value from myTable where BUnit in (555,556,557,558) and
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this Type of Query, select StateName ,countryName from country_master left join state_master
I have HQL query SELECT contact.countryCode, contact.firstName, contact.familyName, **contact.accounts.country** FROM Contact contact, IN (contact.accounts)accounts
I have this query = f.select(:city, Country.where(:country_code => es).collect(&:cities) {|p| [ p.city, p.id ]
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
i have this query: SELECT `completed`.`ID` AS `ID`,`completed`.`level` AS `level`,`completed`.`completed_in` AS `completed_in`, COUNT(1) AS

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.