我自己整了个小东西,用aps.net mvc做到页面,数据使用ado.net Entityframwork,我用的应该算Code first,我先写了几个实体类,用实体类生成的数据,没有连接sql server,直接用的LocalDB,数据的提取代码如下:  [OutputCache(Duration = 60, VaryByParam = "*")]
        public ActionResult guohuwenda(int id = 1, int index = 1)
        {
            //传入进来的id 是类型id,index是页面的页码
            int s , x;
            List<articles> l = GetArticels(out s, out x, index, id);
            ViewBag.Previous = s;
            ViewBag.Next = x;
            ViewBag.CurrentIndex = index;
            ViewBag.categroy = id;
            return View(l);
        } 
      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s">上一页是否有效</param>
        /// <param name="x">下一页是否有效</param>
        /// <param name="p">当前的页码,当前第几页</param>
        /// <param name="rid">类型id,数据类型</param>
        /// <returns></returns>
        public List<articles> GetArticels(out int s,out int x,int p = 1,int rid=0)
        { 
            int pageSize = SysConfig.WenDaListPagesize, pageIndex = p-1;  
            List<articles> list = new List<articles>(); 
            if (rid == 0 || rid == 1)
            {
                var result = from i in DB.articless
                         orderby i.CreateTime descending
                         select i;
                int total = result.Count();
                s = p == 1 ? 0 : 1;
                if ((p * pageSize) >= total) x = 0; else x = 1;
                list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize); 
            }
            else {
                var result = from i in DB.articless
                             where i.classID== rid
                             orderby i.CreateTime descending
                             select i;
                int total = result.Count();
                s = p == 1 ? 0 : 1;
                if ((p * pageSize) >= total) x = 0; else x = 1;
                list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize); 
            }  
            //list = result.ToList();
            return list;     
        }
DB 是在BaseController中的一个属性:
 private ApplicationDbContext _db;
        /// <summary>
        /// 共用的数据库对象
        /// </summary>
        public ApplicationDbContext DB
        {
            get {
                if (_db == null)
                {
                    if (SysCache.GetCache(SysConfig.Sys_dbBaseCache) != null)
                    {
                        _db = (ApplicationDbContext)SysCache.GetCache(SysConfig.Sys_dbBaseCache);
                    }
                    else
                    {
                        _db = new ApplicationDbContext();
                        SysCache.SetCache(SysConfig.Sys_dbBaseCache, _db);
                    }
                }
                return _db;
            } 
        }
求大师们帮我分析分析,这个到底咋个的
网页访问这个列表页面,每次加载时间至少是10秒以上,这个太不科学了。没有找到原因哦!
 
这个是发布后的效果,很慢啊  
http://www.kdtianxia.com/qicheguohu/guohuwenda/1/1
另外也说说我的服务器,感觉不应该是服务器的问题,因为其他的页面都没有这么慢,阿里云的单核服务器,1M带宽,2G内存。

解决方案 »

  1.   

    其他地方你可能需要日志去分析慢的何处,有一个地方你最好改下
    if (SysCache.GetCache(SysConfig.Sys_dbBaseCache) != null)
                        {
                            _db = (ApplicationDbContext)SysCache.GetCache(SysConfig.Sys_dbBaseCache);
                        }
    上面代码判断和赋值连续调用两次相同方法,这其实可以只调用1次
      

  2.   

    别看量少,ViewBag.的赋值异常的耗时!!我觉得这货有什么存在的理由