내가 가지고있는 도시 이름, 도시 ID, 지역 이름, 그 레코드에 대한 중복 된 이름의 수를 얻을 수 다음 쿼리 :최적화 쿼리
SELECT Country_CA.City AS currentCity, Country_CA.CityID, globe_region.region_name, (SELECT count(Country_CA.City) FROM Country_CA WHERE City LIKE currentCity) as counter FROM Country_CA LEFT JOIN globe_region ON globe_region.region_id = Country_CA.RegionID AND globe_region.country_code = Country_CA.CountryCode ORDER BY City
이 예는 캐나다입니다 도시가 드롭 다운 목록에 표시됩니다.
캐나다에는 이름이 같은 몇 개 도시가 있고 다른 국가에서는 몇 개의 도시가 있습니다. 따라서 같은 이름의 지역이 하나 이상 있는지 알고 싶다면 지역 이름이 마을 이름에 추가됩니다. 지역 이름은 globe_region 테이블에 있습니다.
Country_CA 및 globe_region이 유사은
CREATE TABLE IF NOT EXISTS `Country_CA` ( `City` varchar(75) NOT NULL DEFAULT '', `RegionID` varchar(10) NOT NULL DEFAULT '', `CountryCode` varchar(10) NOT NULL DEFAULT '', `CityID` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`City`,`RegionID`), KEY `CityID` (`CityID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
및
CREATE TABLE IF NOT EXISTS `globe_region` ( `country_code` char(2) COLLATE utf8_unicode_ci NOT NULL, `region_code` char(2) COLLATE utf8_unicode_ci NOT NULL, `region_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`country_code`,`region_code`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
상단에 쿼리 내가 원하는 않습니다 정확히 (I 시각화 목적을 위해 몇 가지 변경) 하지만 5000 레코드의 목록을 생성하는 데 너무 오래 걸립니다. 같은 결과를 더 빨리 얻기 위해 하위 쿼리를 최적화하는 방법이 있는지 알고 싶습니다. 이 테이블을 참조 할 필요가 없습니다으로
결과, 하위 선택의 속도를 충분히 수 있습니다 Country_CA.City에이
City CityID region_name counter sheraton 2349269 British Columbia 1 sherbrooke 2349270 Quebec 2 sherbrooke 2349271 Nova Scotia 2 shere 2349273 British Columbia 1 sherridon 2349274 Manitoba 1