Ok so the query is dead simple. This is the table it is stored in:
CREATE TABLE `ips` (
`ip` int(11) unsigned NOT NULL DEFAULT '0',
`ip_start` int(11) unsigned NOT NULL DEFAULT '0',
`ip_end` int(11) unsigned NOT NULL DEFAULT '0',
KEY `IP` (`ip`),
KEY `IP_RANGE_END` (`ip_end`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This table is filled with either singular IPs (in the IP column) or an IP Range (ip_start to ip_end).
Below is the function I am using:
CREATE DEFINER=`root`@`%` FUNCTION `in_adometry`(in_ip VARCHAR(30)) RETURNS int(11)
DETERMINISTIC
BEGIN
SET @ip = 0;
SELECT COUNT(*) INTO @ip FROM ips WHERE (INET_ATON(in_ip) BETWEEN ip_start AND ip_end) OR ip = INET_ATON(in_ip) LIMIT 1;
IF @ip > 0 THEN
RETURN 1;
END IF;
RETURN 0;
END
I am currently hosting this on Amazon's RDS (medium size), and I can't get any more then 800 queries a second before the CPU hits over 100%. There is NOTHING else in this RDS but this one table. There are 1.1 million singular IPs, followed by about 3,000 IP Ranges (over 20 million IPs all together). What can I do to speed this up?