程序部署到服务器后非常慢!!!

Posted hao_1234_1234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了程序部署到服务器后非常慢!!!相关的知识,希望对你有一定的参考价值。

 

结论:

1、EF 查询 比ADO慢,甚至直接报告超时错误,原因不详。

2、在原生ADO.Net中 使用 参数化查询 比 直接使用sql拼接  慢几十倍!!!

 

ADO.Net代码测试

        public List<v_yjdateggjgModel> SelectList(int yjxzqid, int ncpid, DateTime start, DateTime end)
        {
            List<v_yjdateggjgModel> list = new List<v_yjdateggjgModel>();
       //第一条sql,采用参数化查询 用时36秒 查询1100条数据 // string sql = "select yjxzqid,ncpid,rq,sjttjg,sjpfjg,sjlsjg,ycttjg,ycpfjg,yclsjg from v_yjdateggjg where [email protected] and [email protected] and (rq>[email protected] and rq<[email protected])";
       //第二条 sql,采用 sql拼接 用时1.5秒 查询 1100数据 string sql = "select yjxzqid,ncpid,rq,sjttjg,sjpfjg,sjlsjg,ycttjg,ycpfjg,yclsjg from v_yjdateggjg where yjxzqid = "+ yjxzqid + " and ncpid = "+ ncpid + " and (rq >= ‘"+start.ToString("yyyy-MM-dd")+ "‘ and rq <= ‘" + end.ToString("yyyy-MM-dd") + "‘)"; //SqlParameter[] sqlparms = new SqlParameter[] //{ // new SqlParameter("@ncpid",ncpid) , // new SqlParameter("@yjxzqid",yjxzqid), // new SqlParameter("@start",start), // new SqlParameter("@end",end), //}; using (SqlDataReader reader = SqlHelper.ExecuteReader(sql)) { if (reader.HasRows) { while (reader.Read()) { v_yjdateggjgModel info = new v_yjdateggjgModel(); info.yjxzqid = (int)SqlHelper.FromDbNull(reader["yjxzqid"]); info.ncpid = (int)SqlHelper.FromDbNull(reader["ncpid"]); info.rq = (DateTime)SqlHelper.FromDbNull(reader["rq"]); info.sjttjg = (decimal?)SqlHelper.FromDbNull(reader["sjttjg"]); info.sjpfjg = (decimal?)SqlHelper.FromDbNull(reader["sjpfjg"]); info.sjlsjg = (decimal?)SqlHelper.FromDbNull(reader["sjlsjg"]); info.ycttjg = (decimal?)SqlHelper.FromDbNull(reader["ycttjg"]); info.ycpfjg = (decimal?)SqlHelper.FromDbNull(reader["ycpfjg"]); info.yclsjg = (decimal?)SqlHelper.FromDbNull(reader["yclsjg"]); list.Add(info); } } } return list; }

EF代码(已舍弃)

                // EF 查询方式一
                // var data1 = db.v_yjdateggjg.Where(d => d.yjxzqid == yjxzqid && d.ncpid == ncpid && (d.rq >= start && d.rq <= end)).ToList();
                // EF 查询方式二
                // string sql = "select *  from v_yjdateggjg where [email protected] and [email protected]  and (rq>[email protected] and rq<[email protected])";
                // var sqlparms = new SqlParameter[] {
                //     new SqlParameter("@ncpid",ncpid),
                //     new SqlParameter("@yjxzqid",yjxzqid),
                //     new SqlParameter("@start",start),
                //     new SqlParameter("@end",end),
                //};
                // var data1 = db.Database.SqlQuery<v_yjdateggjg>(sql, sqlparms).ToList();

  

数据库内部测试

//sql 参数化查询  1100条数据 3秒
declare @yjxzqid int =9; declare @ncpid int= 35; declare @start datetime = ‘2014-5-1‘; declare @end datetime=‘2017-5-1‘; select yjxzqid,ncpid,rq,sjttjg,sjpfjg,sjlsjg,ycttjg,ycpfjg,yclsjg from v_yjdateggjg where [email protected] and [email protected] and (rq>[email protected] and rq<[email protected]) // sql拼接 1100条 1秒 select yjxzqid,ncpid,rq,sjttjg,sjpfjg,sjlsjg,ycttjg,ycpfjg,yclsjg from v_yjdateggjg where yjxzqid=9 and ncpid=35 and (rq>‘2014-5-1‘ and rq<‘2017-5-1‘)

  

v_yjdateggjg  视图内部sql语句
   SELECT   dbo.v_yjdatejg.yjxzqid, dbo.datalocation.cjdd AS yjxzqname, dbo.v_yjdatejg.ncpid, dbo.products.ncpmc, 
                dbo.v_yjdatejg.rq, dbo.v_yjdatealljg.ttjg AS sjttjg, dbo.v_yjdatealljg.pfjg AS sjpfjg, dbo.v_yjdatealljg.lsjg AS sjlsjg, 
                dbo.v_yjdateycjg.ttjg AS ycttjg, dbo.v_yjdateycjg.pfjg AS ycpfjg, dbo.v_yjdateycjg.lsjg AS yclsjg
FROM      dbo.v_yjdatejg INNER JOIN
                dbo.datalocation ON dbo.v_yjdatejg.yjxzqid = dbo.datalocation.id INNER JOIN
                dbo.products ON dbo.v_yjdatejg.ncpid = dbo.products.id LEFT OUTER JOIN
                dbo.v_yjdateycjg ON dbo.v_yjdatejg.yjxzqid = dbo.v_yjdateycjg.yjxzqid AND 
                dbo.v_yjdatejg.ncpid = dbo.v_yjdateycjg.ncpid AND dbo.v_yjdatejg.rq = dbo.v_yjdateycjg.ycrq LEFT OUTER JOIN
                dbo.v_yjdatealljg ON dbo.v_yjdatejg.yjxzqid = dbo.v_yjdatealljg.yjxzqid AND 
                dbo.v_yjdatejg.ncpid = dbo.v_yjdatealljg.ncpid AND dbo.v_yjdatejg.rq = dbo.v_yjdatealljg.cjrq

 

sqlHelper类

    public static class SqlHelper
    {
        private static readonly string conStr = ConfigurationManager.ConnectionStrings["lyc2ConnString"].ConnectionString;

        //insert delete update
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        //返回单个值
        public static object ExecuteScalar(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

        //执行返回DataReader
        public static SqlDataReader ExecuteReader(string sql,  params SqlParameter[] pms)
        {
            SqlConnection con = new SqlConnection(conStr);
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                //con.Open();
                try
                {
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
            }
        }


        //查询多行 
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr))
            {
                if (pms != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(pms);
                }
                adapter.Fill(dt);
            }

            return dt;
        }

        /// <summary>
        /// 将DbNull转换成null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object FromDbNull(object obj)
        {
            if (obj == DBNull.Value)
            {
                return null;
            }
            else
            {
                return obj;
            }
        }

        /// <summary>
        /// 将null转换成DbNull
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object ToDbNull(object obj)
        {
            if (obj == null)
            {
                return DBNull.Value;
            }
            else
            {
                return obj;
            }
        }
    }

}

 

 博客园非常蛋疼的说:字数少有150字不能发布的首页。

答:亲,代码不算数吗?看来博客园的管理已经渐渐思维固话了,越来越像写中学生作文规范。






以上是关于程序部署到服务器后非常慢!!!的主要内容,如果未能解决你的问题,请参考以下文章

我最近使用谷歌应用程序引擎(灵活)部署了一个“hello,word”烧瓶应用程序,而且速度非常慢

Google Cloud Run 与本地机器相比非常慢

导致资产预编译在heroku部署上失败的代码片段

AMD 上的慢模板纹理

升级到最新的 SDK 版本 1.20.4 后运行/调试 Flutter 应用程序非常慢

Grails:部署时间非常慢。 'Resolving Dependencies...' 需要 10 多秒