使用ADO.NET开发的Web API 2方法表现不佳,需要53秒

时间:2016-04-17 04:26:37

标签: entity-framework sql-server-2008 stored-procedures linq-to-sql ado.net

我正在使用Asp.Net Web API2为Android应用创建API。有一种从表中获取成员数据的方法。我根据安排(int)和名称(字符串)对成员进行排序和列出。第一种排序的值不是0.然后按名称排序,值为0.一切正常但查询执行需要53234毫秒,如postman.PErformance所示,当我使用EF6时,性能最差。我使用实时URL测试它,而不是在localhost上测试它。我有云服务器。请检查并帮助我改进性能。仅有大约1000行/记录。还有另一个查询/存储过程,其基于纬度和经度列出附近的成员,并且执行时间几乎不是2秒。如果您需要更多信息,请与我们联系。感谢

API方法

    [HttpGet]
    [ActionName("GetAllMembersOfClub")]
    public APIResult GetAllMembersOfClub()
    {
        APIResult apiResult = new APIResult();

        List<MemberData> lstMember = new List<MemberData>();

        var cmd = new SqlCommand("Rotary1.usp_GetMembersWithArrangements", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@clubId", DbType.Int32).Value = clubID;       
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            var objMember = new MemberData();


            objMember.CountryCode = dr["CountryCode"].ToString();
            objMember.ClubName = dr["ClubName"].ToString();

            objMember.Name = dr["name"].ToString();
            objMember.Classification = dr["Classification"].ToString();
            objMember.OfficeAdrs1 = dr["OfficeAdrs1"].ToString();
            objMember.OfficeAdrs2 = dr["OfficeAdrs2"].ToString();
            objMember.OfficeAdrs3 = dr["OfficeAdrs3"].ToString();
            objMember.ResAdrs1 = dr["ResAdrs1"].ToString();
            objMember.ResAdrs2 = dr["ResAdrs2"].ToString();
            objMember.ResAdrs3 = dr["ResAdrs3"].ToString();
            objMember.Spouse = dr["Wife"].ToString();
            objMember.Phone = dr["Phone"].ToString();
            objMember.Mobile = "+" + dr["CountryCode"].ToString() + dr["Mobile"].ToString();
            objMember.EmailId = dr["EmailId"].ToString();

            objMember.Since = dr["Since"].ToString();
            objMember.Title1 = dr["title1"].ToString();
            objMember.Title2 = dr["title2"].ToString();
            objMember.Title3 = dr["title3"].ToString();
            objMember.Title4 = dr["title4"].ToString();
            objMember.PostHeld = dr["PostHeld"].ToString();
            objMember.Imgg = dr["imgg"].ToString();


            objMember.Anniversary = dr["Aniversary"].ToString();
            objMember.Position = dr["Position"].ToString();
            objMember.Ophone = dr["ophone"].ToString();
            objMember.Children = dr["Children"].ToString();

            objMember.Spouse = dr["Wife"].ToString();
            objMember.Latitude = dr["Latitude"].ToString();
            objMember.Longitude = dr["Longitude"].ToString();
            objMember.Mobile10 = dr["mobile10"].ToString();
            objMember.Mobile2 = dr["mobile2"].ToString();
            objMember.Mobile3 = dr["mobile3"].ToString();
            objMember.Mobile4 = dr["mobile4"].ToString();
            objMember.Mobile5 = dr["mobile5"].ToString();
            objMember.Mobile6 = dr["mobile6"].ToString();
            objMember.Mobile7 = dr["mobile7"].ToString();
            objMember.Mobile8 = dr["mobile8"].ToString();
            objMember.Mobile9 = dr["mobile9"].ToString();
            objMember.Noti = dr["noti"].ToString();
            objMember.Arrange = dr["arrange"].ToString();
            try
            {
                objMember.DOB = Convert.ToDateTime(dr["DOB"].ToString()).ToString("dd-MM-yyy");
            }
            catch (Exception)
            {
                objMember.DOB = "";
            }



            if (!string.IsNullOrWhiteSpace(objMember.Mobile2) || objMember.Mobile2 != "")
            {
                objMember.Mobile2 = "+" + dr["CountryCode"].ToString() + dr["mobile2"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile3) || objMember.Mobile3 != "")
            {
                objMember.Mobile3 = "+" + dr["CountryCode"].ToString() + dr["mobile3"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile4) || objMember.Mobile4 != "")
            {
                objMember.Mobile4 = "+" + dr["CountryCode"].ToString() + dr["mobile4"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile5) || objMember.Mobile5 != "")
            {
                objMember.Mobile5 = "+" + dr["CountryCode"].ToString() + dr["mobile5"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile6) || objMember.Mobile6 != "")
            {
                objMember.Mobile6 = "+" + dr["CountryCode"].ToString() + dr["mobile6"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile7) || objMember.Mobile7 != "")
            {
                objMember.Mobile7 = "+" + dr["CountryCode"].ToString() + dr["mobile7"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile8) || objMember.Mobile8 != "")
            {
                objMember.Mobile8 = "+" + dr["CountryCode"].ToString() + dr["mobile8"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile9) || objMember.Mobile9 != "")
            {
                objMember.Mobile9 = "+" + dr["CountryCode"].ToString() + dr["mobile9"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile10) || objMember.Mobile10 != "")
            {
                objMember.Mobile10 = "+" + dr["CountryCode"].ToString() + dr["mobile10"].ToString();
            }
            lstMember.Add(objMember);
        }
        dr.Close();
        conn.Close();

         cmd = new SqlCommand("Rotary1.usp_GetMembersWithArrangements1", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@clubId", DbType.Int32).Value = clubID;
        conn.Open();
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            var objMember = new MemberData();


            objMember.CountryCode = dr["CountryCode"].ToString();
            objMember.ClubName = dr["ClubName"].ToString();

            objMember.Name = dr["name"].ToString();
            objMember.Classification = dr["Classification"].ToString();
            objMember.OfficeAdrs1 = dr["OfficeAdrs1"].ToString();
            objMember.OfficeAdrs2 = dr["OfficeAdrs2"].ToString();
            objMember.OfficeAdrs3 = dr["OfficeAdrs3"].ToString();
            objMember.ResAdrs1 = dr["ResAdrs1"].ToString();
            objMember.ResAdrs2 = dr["ResAdrs2"].ToString();
            objMember.ResAdrs3 = dr["ResAdrs3"].ToString();
            objMember.Spouse = dr["Wife"].ToString();
            objMember.Phone = dr["Phone"].ToString();
            objMember.Mobile = "+" + dr["CountryCode"].ToString() + dr["Mobile"].ToString();
            objMember.EmailId = dr["EmailId"].ToString();

            objMember.Since = dr["Since"].ToString();
            objMember.Title1 = dr["title1"].ToString();
            objMember.Title2 = dr["title2"].ToString();
            objMember.Title3 = dr["title3"].ToString();
            objMember.Title4 = dr["title4"].ToString();
            objMember.PostHeld = dr["PostHeld"].ToString();
            objMember.Imgg = dr["imgg"].ToString();


            objMember.Anniversary = dr["Aniversary"].ToString();
            objMember.Position = dr["Position"].ToString();
            objMember.Ophone = dr["ophone"].ToString();
            objMember.Children = dr["Children"].ToString();

            objMember.Spouse = dr["Wife"].ToString();
            objMember.Latitude = dr["Latitude"].ToString();
            objMember.Longitude = dr["Longitude"].ToString();
            objMember.Mobile10 = dr["mobile10"].ToString();
            objMember.Mobile2 = dr["mobile2"].ToString();
            objMember.Mobile3 = dr["mobile3"].ToString();
            objMember.Mobile4 = dr["mobile4"].ToString();
            objMember.Mobile5 = dr["mobile5"].ToString();
            objMember.Mobile6 = dr["mobile6"].ToString();
            objMember.Mobile7 = dr["mobile7"].ToString();
            objMember.Mobile8 = dr["mobile8"].ToString();
            objMember.Mobile9 = dr["mobile9"].ToString();
            objMember.Noti = dr["noti"].ToString();
            objMember.Arrange = dr["arrange"].ToString();
            try
            {
                objMember.DOB = Convert.ToDateTime(dr["DOB"].ToString()).ToString("dd-MM-yyy");
            }
            catch (Exception)
            {
                objMember.DOB = "";
            }



            if (!string.IsNullOrWhiteSpace(objMember.Mobile2) || objMember.Mobile2 != "")
            {
                objMember.Mobile2 = "+" + dr["CountryCode"].ToString() + dr["mobile2"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile3) || objMember.Mobile3 != "")
            {
                objMember.Mobile3 = "+" + dr["CountryCode"].ToString() + dr["mobile3"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile4) || objMember.Mobile4 != "")
            {
                objMember.Mobile4 = "+" + dr["CountryCode"].ToString() + dr["mobile4"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile5) || objMember.Mobile5 != "")
            {
                objMember.Mobile5 = "+" + dr["CountryCode"].ToString() + dr["mobile5"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile6) || objMember.Mobile6 != "")
            {
                objMember.Mobile6 = "+" + dr["CountryCode"].ToString() + dr["mobile6"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile7) || objMember.Mobile7 != "")
            {
                objMember.Mobile7 = "+" + dr["CountryCode"].ToString() + dr["mobile7"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile8) || objMember.Mobile8 != "")
            {
                objMember.Mobile8 = "+" + dr["CountryCode"].ToString() + dr["mobile8"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile9) || objMember.Mobile9 != "")
            {
                objMember.Mobile9 = "+" + dr["CountryCode"].ToString() + dr["mobile9"].ToString();
            }

            if (!string.IsNullOrWhiteSpace(objMember.Mobile10) || objMember.Mobile10 != "")
            {
                objMember.Mobile10 = "+" + dr["CountryCode"].ToString() + dr["mobile10"].ToString();
            }


            lstMember.Add(objMember);
        }

        dr.Close();
        conn.Close();

        apiResult.ReturnData = lstMember;
        apiResult.ReturnCode = "1";
        apiResult.ReturnMessage = "success";

        return apiResult;
    }

查询/存储过程

USE [myDb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [Rotary1].[usp_GetMembersWithArrangements]    
@clubId INT    
As    
BEGIN     
BEGIN       
 select * from main where arrange<>0 and club=@clubId order by arrange desc  
END           
END
GO

第二程序

USE [myDb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [Rotary1].[usp_GetMembersWithArrangements1]    
@clubId INT    
As    
BEGIN     
BEGIN       
select * from main where arrange=0 and club=@clubId order by name   
END           
END
GO

以下是表格架构

CREATE TABLE [dbo].[main](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [title1] [varchar](max) NULL,
    [title2] [varchar](max) NULL,
    [title3] [varchar](max) NULL,
    [title4] [varchar](max) NULL,
    [Name] [varchar](max) NULL,
    [Classification] [varchar](max) NULL,
    [position] [varchar](max) NULL,
    [PostHeld] [varchar](max) NULL,
    [OfficeAdrs1] [text] NULL,
    [OfficeAdrs2] [text] NULL,
    [OfficeAdrs3] [text] NULL,
    [ophone] [varchar](max) NULL,
    [Phone] [varchar](max) NULL,
    [Mobile] [varchar](max) NULL,
    [EmailId] [varchar](max) NULL,
    [ResAdrs1] [text] NULL,
    [ResAdrs2] [text] NULL,
    [ResAdrs3] [text] NULL,
    [Wife] [varchar](max) NULL,
    [children] [varchar](max) NULL,
    [DOB] [date] NULL,
    [Since] [varchar](max) NULL,
    [imgg] [text] NULL,
    [Aniversary] [varchar](max) NULL,
    [arrange] [int] NULL,
    [club] [int] NULL,
    [noti] [int] NULL,
    [ClubName] [varchar](200) NULL,
    [Latitude] [varchar](200) NULL,
    [Longitude] [varchar](200) NULL,
    [mobile2] [varchar](20) NULL,
    [mobile3] [varchar](20) NULL,
    [mobile4] [varchar](20) NULL,
    [mobile5] [varchar](20) NULL,
    [mobile6] [varchar](20) NULL,
    [mobile7] [varchar](20) NULL,
    [mobile8] [varchar](20) NULL,
    [mobile9] [varchar](20) NULL,
    [mobile10] [varchar](20) NULL,
    [CountryCode] [varchar](10) NULL,
    [Status] [int] NULL CONSTRAINT [DF_main_Status]  DEFAULT ((1)),
 CONSTRAINT [PK_main] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

1 个答案:

答案 0 :(得分:0)

 public List<Member> GetMembers(string name="")
    {
        var param1 = new SqlParameter("@Name",name );
        var result = _Context.Database.SqlQuery<Member>("exec GetMembersList @Name", param1).ToList();
        return result;
    }