zoukankan      html  css  js  c++  java
  • 代码性能优化-----减少数据库读取次数

    对于从相同数据源读取不同要求的数据,可以只读取一次数据库,用linq进行数据的分类。

    数据库示例:

    --优化前读取7此数据库
    SELECT COUNT(DISTINCT p) FROM aa WITH(NOLOCK) WHERE DateDiff(dd, c, getdate())=0
    SELECT COUNT(DISTINCT p) FROM aa WITH(NOLOCK) WHERE DateDiff(dd, c, getdate())=1
    SELECT COUNT(DISTINCT p) FROM aa WITH(NOLOCK)
    SELECT SUM(at) FROM aa WITH(NOLOCK) WHERE s=30
    SELECT MAX(at) FROM aa WITH(NOLOCK) WHERE s=30
    SELECT SUM(at) FROM aa WITH(NOLOCK) WHERE s=10
    SELECT SUM(at) FROM aa WITH(NOLOCK) WHERE s=30 and d=1
    
    --优化后读取一次数据库
    SELECT at,s,d,p,c FROM aa WITH(NOLOCK)

    获取数据示例:

           //代码优化读7次数据库,改为读一次数据库
                var tempt = RB.GetAA(Config.ConStrRead);
                if (tempt != null&& tempt.Rows.Count>0)
                {
                    var enumer = tempt.AsEnumerable();
                    var todayPhones= enumer.Where(m => SlConvert.TryToDateTime(m["c"]) >= DateTime.Today).Select(m => SlConvert.TryToInt64(m["Phone"]));
                    outModel.TodayRecv = todayPhones.ToList().Distinct().Count();
    
                    //var yesterdayPhones = enumer.Where(m => SlConvert.TryToDateTime(m["c"]) >= DateTime.Today.AddDays(-1)).Select(m => SlConvert.TryToInt64(m["Phone"]));
                    //outModel.YesterdayRecv = yesterdayPhones.ToList().Distinct().Count();
    
                    var totalPhones = enumer.Select(m => SlConvert.TryToInt64(m["p"]));
                    outModel.TotalRecv = totalPhones.ToList().Distinct().Count();
    
                    var cashed = enumer.Where(m => SlConvert.TryToInt32(m["s"]) == 30).ToList();
                    var cashedAfterTaxMoney = cashed.Select(m => SlConvert.TryToInt64(m["at"])).ToList();
                    outModel.TotalSaveMoney = cashedAfterTaxMoney.Sum();
    
                    //outModel.ServerMoneyAll = cashed.Where(m => SlConvert.TryToInt32(m["d"]) == 1).Select(m => SlConvert.TryToInt64(m["at"])).Sum();
    
                    outModel.MaxRedBagMoney = cashedAfterTaxMoney.Max();
    
                    outModel.RestRedBagMoney = enumer.Where(m => SlConvert.TryToInt32(m["s"]) == 10).Select(m => SlConvert.TryToInt64(m["at"])).Sum();
                }
  • 相关阅读:
    传智博客.NET培训第13季 Ajax教程(共十三季) 学习资源
    一些sql语句的常用总结(重要)
    处理oracle的死锁
    Adroid 总结--android ListView美化,个性化更改的属性
    如何远程备份sql server数据库
    VSS (Visual Source Safe 2005) 用法详解
    php插入代码数据库
    PHP之PHP文件引用详解
    需要引入库:vue-resource
    axios调用详解
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/6213939.html
Copyright © 2011-2022 走看看