linq查询非常慢 时间问题
几十万数据,分页查询10行,花了30秒,太离谱!
改进
1、连表查询拆分成两个查询,在内存中连表。
从30秒,变3秒
2、日期查询改成区间查询
airHourly.Date 数据举例:2015-01-01 02:00:00.000
System.Data.Entity.DbFunctions.TruncateTime(airHourly.Date) == dateHour
改成:(airHourly.Date >= dateHour && airHourly.Date < dateEnd)
3秒变100毫秒。
若仅改2,也是100毫秒。
说明主要原因是2。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | public JsonResult StreetStationHourlyData( int page, int rows, string sort, string order, DateTime dateHour, string stations) //, string param, string stationsType { PageModel pageInfo = new PageModel(); pageInfo.Page = page; pageInfo.Rows = rows; pageInfo.Order = order; pageInfo.Sort = sort; string [] stationArray = null ; if (stations != null ) { stationArray = stations.Split( ',' ); } ResultModel result = new ResultModel(); if (pageInfo.Page <= 0) { pageInfo.Page = 1; } if (pageInfo.Rows <= 0) { pageInfo.Rows = 20; } dateHour = dateHour.Date; var dateEnd = dateHour.Date.AddDays(1); var data = from airHourly in dbq.T_AirStreetDataHourly2 join station in dbq.T_AirStreetStation2 on airHourly.StationID equals station.StationID where airHourly.StationID != null && (airHourly.Date >= dateHour && airHourly.Date < dateEnd) && stationArray.Contains(airHourly.StationID) orderby airHourly.StationID, airHourly.Date select new AirStreetDataDaily2 { O3 = airHourly.O3, PM10 = airHourly.PM10, PM25 = airHourly.PM25, Date = airHourly.Date, Hour = airHourly.Date.Hour, StationName = station.StationName, StationID = station.StationID, Hierarchy = station.Hierarchy, SeatType = station.SeatType, PointNature = station.PointNature, IsStreet = station.IsStreet }; //var data = // from airHourly in dbq.T_AirStreetDataHourly2 // where airHourly.StationID != null && (airHourly.Date >= dateHour && airHourly.Date < dateEnd) && stationArray.Contains(airHourly.StationID) // //where stationArray.Contains(airHourly.StationID) && System.Data.Entity.DbFunctions.TruncateTime(airHourly.Date) == dateHour // orderby airHourly.StationID, airHourly.Date.Hour // select new AirStreetDataDaily2 { StationID = airHourly.StationID, O3 = airHourly.O3, PM10 = airHourly.PM10, PM25 = airHourly.PM25, Date = airHourly.Date, Hour = airHourly.Date.Hour }; //排序 if (pageInfo.Order != null && pageInfo.Sort != null ) { bool isAsc = pageInfo.Order == "asc" ; data = data.OrderBy( new [] { pageInfo.Sort }, new [] { isAsc }); } var data1 = data.Skip((pageInfo.Page - 1) * pageInfo.Rows).Take(pageInfo.Rows).ToList(); //var dataB = (from station in dbq.T_AirStreetStation2 // where stationArray.Contains(station.StationID) // select new AirStreetDataDaily2 { StationName = station.StationName, StationID = station.StationID, Hierarchy = station.Hierarchy, SeatType = station.SeatType, PointNature = station.PointNature, IsStreet = station.IsStreet } //).ToList(); foreach ( var item in data1) { //var station2= dataB.Where(d => d.StationID == item.StationID).Select(d=>d).FirstOrDefault(); //item.StationName = station2.StationName; //item.StationID = station2.StationID; //item.Hierarchy = station2.Hierarchy; //item.SeatType = station2.SeatType; //item.PointNature = station2.PointNature; //item.IsStreet = station2.IsStreet; if (item.O3 != null ) { item.O3 = Math.Round(( double )item.O3, 2, MidpointRounding.AwayFromZero); } if (item.PM10 != null ) { item.PM10 = Math.Round(( double )item.PM10, 2, MidpointRounding.AwayFromZero); } if (item.PM25 != null ) { item.PM25 = Math.Round(( double )item.PM25, 2, MidpointRounding.AwayFromZero); } } result.rows = data1; result.total = data.Count(); result.success = true ; return Json(result); //, JsonRequestBehavior.AllowGet } |
树立目标,保持活力,gogogo!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-02-17 PowerDesigner设计表时显示注释列Comment(转载)