I have read all the questions from References, and understand that best way to storing the IPv4/IPv6 address for indexing is 2xBIGINTs. But, how can I index those in another table using foreign key with best perfomance?
My requirement is simple. I am trying to make a table for storing IP Address (IPv4, IPv6) with mask (table called subnets
). Now, I have another table called interfaces
where each interface can have an ip address. What is the best way to apply a foreign keys between subnets
and interfaces
table.
I have thought of this right now...
--
-- Table structure for IP addresses `subnets`. stores the IP addresses with version (v4/v6) and type (unicast/multicast)
--
CREATE TABLE subnets (
top_ip BIGINT UNSIGNED DEFAULT NULL COMMENT 'top half of the IP Address in INT. If IPv4, then its NULL',
bottom_ip BIGINT UNSIGNED NOT NULL COMMENT 'bottom half of the IPv6 Address or full IPv4 address in INT',
mask INTEGER UNSIGNED NOT NULL COMMENT 'IP subnet mask',
version CHAR(2) NOT NULL DEFAULT '' COMMENT 'IP version. can be v4 or v6',
type VARCHAR(20) NOT NULL DEFAULT '' COMMENT 'type of address. can be unicast or multicast',
created_date DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uniq_ip_addr (top_ip, bottom_ip)
PRIMARY KEY (top_ip, bottom_ip)
) DEFAULT CHARSET=utf8 ENGINE=InnoDB;
--
-- Table structure for table `interfaces`
--
CREATE TABLE device_interface (
intf_id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
intf_top_ip BIGINT UNSIGNED DEFAULT NULL,
intf_bottom_ip BIGINT UNSIGNED NOT NULL,
CONSTRAINT subnet FOREIGN KEY (intf_top_ip, intf_bottom_ip) REFERENCES subnets(top_ip, bottom_ip),
intf_name VARCHAR(100) NOT NULL,
created_date DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8 ENGINE=InnoDB;