I have over 1.7 million records in a table which contains ip address range(begin and end) both primary key and corresponding details.
The Table structure is
mysql> desc csv;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| begin | bigint(20) | NO | PRI | 0 | |
| end | bigint(20) | NO | PRI | 0 | |
| code | char(2) | YES | | NULL | |
| country | varchar(50) | YES | | NULL | |
| city | varchar(50) | YES | | NULL | |
| area | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
Because of Indexing in Primary Key, the search is fast when an exact match is to be made like this
mysql> SELECT * FROM csv WHERE begin=3338456576;
+------------+------------+------+---------------+----------+---------------+
| begin | end | code | country | city | area |
+------------+------------+------+---------------+----------+---------------+
| 3338456576 | 3338456831 | US | UNITED STATES | NEW YORK | NEW YORK CITY |
+------------+------------+------+---------------+----------+---------------+
1 row in set (0.03 sec)
But when I try to search within a range, It takes longer time.
mysql> SELECT * FROM csv WHERE begin<3338456592 AND end>3338456592;
+------------+------------+------+---------------+----------+---------------+
| begin | end | code | country | city | area |
+------------+------------+------+---------------+----------+---------------+
| 3338456576 | 3338456831 | US | UNITED STATES | NEW YORK | NEW YORK CITY |
+------------+------------+------+---------------+----------+---------------+
1 row in set (1.59 sec)
Is there any way I can optimize my query to search ip address within a range?
EDIT
Create table Statement
CREATE TABLE `csv` (
`begin` bigint(20) NOT NULL DEFAULT '0',
`end` bigint(20) NOT NULL DEFAULT '0',
`code` char(2) DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`area` varchar(50) DEFAULT NULL,
PRIMARY KEY (`begin`,`end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;