有个页面,有3张图表,我的绑定数据方法是把3个图表取完后放到一个大对象上,
我开发机器测试很快,但是连接服务器数据库就要好几秒,求高手指点优化方法,
取代码如下:
public static PictureDataModel GetAffairRankDataDay(DateTime firstDay)
{
var lastDay = firstDay.AddDays(1);
var selectedArea = Storage.Areas.FirstOrDefault(p => p.AreaId == Storage.SelectedAreaId);
//获取所有子节点
var childrenAreaId = AreaHelper.GetGrandChild(selectedArea).Select(x => x.AreaId);
//以上数据不操作数据库的
_db = new YmsDb();
var columnData = (from ai in _db.AffairsInfo
join a in _db.Affairs on ai.AffairId equals a.AffairId
where childrenAreaId.Contains(ai.AreaId) &&
ai.LastTransTime > firstDay && ai.LastTransTime < lastDay
group ai by new { a.AffairId, a.AffairName }
into g
select new ChartDataClass
{
Id = g.Key.AffairId,
XValue = g.Key.AffairName,
YValue = (double?)g.Count()
}).OrderByDescending(x => x.YValue).Take(10).ToList();
var pieData = (from ai in _db.AffairsInfo
join a in _db.Affairs on ai.AffairId equals a.AffairId
join w in _db.Window on a.WindowId equals w.WindowId
join l in _db.AffairLine on w.AffairLineId equals l.AffairLineId
where childrenAreaId.Contains(ai.AreaId) &&
ai.LastTransTime > firstDay && ai.LastTransTime < lastDay
group ai by new { l.AffairLineId, l.AffairLineName }
into g
select new ChartDataClass
{
Id = g.Key.AffairLineId,
XValue = g.Key.AffairLineName,
YValue = (double?)g.Count()
}).OrderByDescending(x => x.YValue).Take(10).ToList();
var affairInfoCount = from ai in _db.AffairsInfo.AsEnumerable()
join a in _db.Affairs on ai.AffairId equals a.AffairId
where childrenAreaId.Contains(ai.AreaId) &&
ai.LastTransTime > firstDay && ai.LastTransTime < lastDay && ai.LastTransTime.HasValue
group ai by ai.LastTransTime.Value.Hour
into g
select new { Hour = g.Key, ValueInt = g.Count() };
var lineData = WorkTimer.Select(hour =>
new ChartDataClass
{
XValue = hour + ":00",
YValue = affairInfoCount.Where(p => p.Hour == hour).Select(p => p.ValueInt).Sum()
}).ToList();
var countAllTime = affairInfoCount.Select(p => p.ValueInt).DefaultIfEmpty().Sum();
var countOnWorkTime = (int)lineData.Sum(x => x.YValue.GetValueOrDefault());
var chartDataOtherTime = new ChartDataClass
{
XValue = "其他时段",
YValue = countAllTime - countOnWorkTime
};
lineData.Add(chartDataOtherTime);
var obj = new PictureDataModel();
obj.ColumnData = columnData;
obj.PieData = pieData;
obj.LineData = lineData;
return obj;
}
绑定控件代码如下:
private void BindDataByDay()
{
LoadingBar.IsBusy = true;
Common.RunAsyncTask(
() =>
{
//Storage.SelectedAreaAffairInfo = AffairsInfoHelper.GetAffairInfoByDay(_chooseDate);
//_columnData = AffairsInfoHelper.GetAffairInfoCount();
//_lineData = AffairsInfoHelper.GetAffairInfoCountByDay();
//_pieData = AffairsInfoHelper.GetLineAffairInfoCount();
var obj = AffairsInfoHelper.GetAffairRankDataDay(_chooseDate);
_columnData = obj.ColumnData;
_lineData = obj.LineData;
_pieData = obj.PieData;
},
() =>
{
ChartColumn.ItemsSource = _columnData;
ChartLine.ItemsSource = _lineData;
ChartPie.ItemsSource = _pieData;
LoadingBar.IsBusy = false;
});
}
你贴这么多代码想说啥呢?
我开发机器测试很快,但是连接服务器数据库就要好几秒。
1、啥是连接服务器数据库要好几秒?是连接,还是包括取数据回来?
2、如果包括取数据回来,请问数据是多少?可以看看生成的SQL语句吗?
3、数据库在哪儿,离你的开发机器有多远?
1.我本机打开页面显示进度条到读取数据成功展示数据3秒内,数据库连服务器需要7,8秒
2获取的数据量不多,3个图形化报表,返回的数据 估计也就几十最多100左右了,系统刚上数据库的实际数据总量也不多,1,2千条吧
3.一个局域网
就假设我要获取abc3个表各10行10数据,加起来就是30行,但是我查询要查3次,这样感觉会很慢
@不要打冬冬: 数据库连服务器需要7,8秒 -〉知道这意味着什么吗?默认的连接超时才 15 秒,你这就占了一半。
@不要打冬冬: 我能说你再多看看书,或者找个靠谱点的老师学习学习吗?
你的回答完全看不出来你懂得什么叫连接数据库的时间,
什么叫取得数据的时间。
你把UI绑定,IE的反映速度与数据库的存取时间全部混在一起了。
正常情况下,30行数据的存取时间应该是在1秒以内,估计100MS以内都有可能。其他的时间都是别的时间,你得先懂得分清楚。