MySQL query packet (optimization for loop)

         BACKGROUND: provinces, cities, districts need to be assembled by interlocking three isolated district, then the data assembled for loop fixed to the front end structure returned json, respectively present three provinces table, isolated by provincial cities,, request more than 30 seconds

        Optimization: the isolated province, city isolated directly from provincial and municipal corresponding counties, districts using GROUP_CONCAT function json spliced ​​into a string and then returns to the front end node data structure. Entire request takes about 3 seconds

Table Structure:

province:

city:

 Area / County

sql:

String sql = "SELECT c.cityid value,c.city text,GROUP_CONCAT( '{\"text\":\"',area, '\",\"value\":\"', areaid,'\"}' ) children FROM `opc_area_cities` c JOIN opc_area_areas a ON a.cityid = c.cityid WHERE c.provinceid=? GROUP BY c.cityid ORDER BY c.cityid ASC";

search result:

Code:

List <Record> provinces = AreaProvinces.dao.getAreaProvincesList ( ); // save
for (the Record P: Provinces) {
  List <the Record> = AreaCities.dao.getAreaCities Cities (p.get ( "value")); // get and the corresponding city, district json string is
  for (the Record Record: Cities) {
    string = record.get Children ( "Children");
    the JSONArray jsonArr = JSONArray.fromObject ( "[" Children + + "]"); // after the return zone into json
    record.set ( "Children", jsonArr);
  }
  P.SET ( "Children", Cities);
}

children print the results:

   "{\"text\":\"延庆县\",\"value\":\"110229\"},{\"text\":\"密云县\",\"value\":\"110228\"}"

Guess you like

Origin www.cnblogs.com/binaryStar/p/12662552.html