亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

node省市區(qū)三級(jí)數(shù)據(jù)性能測(cè)評(píng)實(shí)例分析

 更新時(shí)間:2019年11月06日 10:01:24   作者:蒼青浪  
這篇文章主要介紹了node省市區(qū)三級(jí)數(shù)據(jù)性能,結(jié)合具體實(shí)例形式評(píng)測(cè)分析了node省市區(qū)三級(jí)數(shù)據(jù)的實(shí)現(xiàn)、改進(jìn)方法與運(yùn)行效率,需要的朋友可以參考下

本文實(shí)例講述了node省市區(qū)三級(jí)數(shù)據(jù)性能測(cè)評(píng)。分享給大家供大家參考,具體如下:

閑來無事,測(cè)試下node和egg

首先是數(shù)據(jù)庫,大概長(zhǎng)這樣

然后是代碼

'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
 async index() {
  const { ctx } = this;
  ctx.body = 'hi, egg';
 }
 async city() {
  const { ctx } = this;
  console.time("sql")
  const provinces = await this.app.mysql.select('provinces')
  const citys = await this.app.mysql.select('cities')
  const areas = await this.app.mysql.select('areas')
  console.timeEnd("sql")
  console.time('cal')
  provinces.forEach(province => {
   let provinceid = province.provinceid
   province.children = []
   citys.forEach(city => {
    city.children = []
    if (city.provinceid === provinceid) {
     province.children.push(city)
    }
    let cityid = city.cityid
    areas.forEach(area => {
     if (area.cityid === cityid) {
      city.children.push(area)
     }
    })
   })
  })
  console.timeEnd('cal')
  const result = {
   status: 1,
   data: provinces,
  }
  ctx.body = result;
 }
}
module.exports = HomeController;

執(zhí)行時(shí)間:

接著改進(jìn)

'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
 async index() {
  const { ctx } = this;
  ctx.body = 'hi, egg';
 }
 async city() {
  const { ctx } = this;
  console.time("sql")
  let provinces = await this.app.mysql.select('provinces')
  let citys = await this.app.mysql.select('cities')
  let areas = await this.app.mysql.select('areas')
  console.timeEnd("sql")
  console.time('cal')
  for (let i = 0, len = citys.length; i < len; i++) {
   let city = citys[i]
   city.children = []
   let cityid = city.cityid
   for (let j = 0, len1 = areas.length; j < len1; j++) {
    let area = areas[j]
    if (area.cityid === cityid) {
     city.children.push(areas.splice(j, 1)[0])
     len1--
     j--
    }
   }
  }
  provinces.forEach(province => {
   let provinceid = province.provinceid
   province.children = []
   for (let i = 0, len = citys.length; i < len; i++) {
    let city = citys[i]
    if (city.provinceid === provinceid) {
     province.children.push(city)
     citys.splice(i, 1)
     len--
     i--
    }
   }
  })
  console.timeEnd('cal')
  const result = {
   status: 1,
   data: provinces,
  }
  ctx.body = result;
 }
}
module.exports = HomeController;

本次優(yōu)化結(jié)果

可以看到,在組裝數(shù)據(jù)的過程中,時(shí)間縮短了近20倍!

后續(xù)版本繼續(xù)優(yōu)化,也歡迎有相關(guān)方面經(jīng)驗(yàn)的大神留言探討,給出更好的方案。

希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論