提取数据大约需要12-15分钟。
如何优化以下查询:
SELECT Country.id AS Country_id,
Country.country AS Country_country,
Country.countrycode AS Country_countrycode,
State.id AS State_id,
State.statecode AS State_statecode,
City.id AS City_id,
City.city AS City_city,
Zipcode.id AS Zipcode_id,
Zipcode.zipcode AS Zipcode_zipcode
FROM c_country Country
LEFT JOIN c_state State
ON Country.id = ( State.country_id )
LEFT JOIN c_city City
ON State.id = ( City.state_id )
LEFT JOIN c_zipcode Zipcode
ON City.id = ( Zipcode.city_id )
以下是表格结构:
CREATE TABLE `c_city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_id` int(11) NOT NULL,
`city` varchar(256) NOT NULL,
`zone_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7027 DEFAULT CHARSET=latin1;
CREATE TABLE `c_state` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) NOT NULL,
`state` varchar(256) NOT NULL,
`statecode` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
CREATE TABLE `c_country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(256) NOT NULL,
`countrycode` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE `c_zipcode` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`city_id` int(11) NOT NULL,
`zipcode` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=539420 DEFAULT CHARSET=latin1;
下面是EXPLAIN SELECT的屏幕截图:
答案 0 :(得分:0)
要尝试提高性能,您可以为
添加索引table c_zipcode column city_id
table c_city column state_id
table c_state column country_id
或用于使用索引
解析所有列值table c_zipcode column city_id, zipcode
table c_city column state_id, city
table c_state column country_id , statecode
答案 1 :(得分:0)
我认为您的查询非常慢,因为缺少一些索引。您没有声明任何外键,在mysql InnoDB中,外键很重要,因为它们是数据完整性的保证,但MySQL也会在此外键上自动创建索引。
您必须更改表以添加外键:
.PactUri(@"..\..\..\..\pacts")
.PactUri(@"..\..\..\..\pacts\*.json")