对于我的一个应用程序,我正在运行SQL Server 2005,而我正在localhost上运行它。当我通过应用程序访问它时,如果我在localhost上访问它,则需要1-10秒才能开始显示数据。但是,当我离开localhost并从网络上的另一台计算机访问它时,访问时间大约是一秒钟。
为什么会这样,以及如何更正此问题,以便我可以在localhost上获得相同的访问时间?
连接字符串: Data Source=THESTUDIO\SQLEXPRESS;Initial Catalog=RentalEase;Integrated Security=True
使用类型化数据集进行查询(主要是)
加载表单时,我发现Visual Studio多次使用相同的查询查询数据库。虽然根据我的理解,数据库将使用查询的缓存版本进行响应。但是,我确实发现应用程序(来自winform的数据绑定功能)正在执行此查询:
exec sp_executesql N'UPDATE [tblTenant] SET [TenantStatusID] = @TenantStatusID, [PropertyID] = @PropertyID, [UnitID] = @UnitID, [TenantName] = @TenantName, [Sex] = @Sex, [BirthDate] = @BirthDate, [SSNO] = @SSNO, [CoTenant1] = @CoTenant1, [CoTenant1Sex] = @CoTenant1Sex, [CoTenant1BirthDate] = @CoTenant1BirthDate, [CoTenant1SSNO] = @CoTenant1SSNO, [CoTenant2] = @CoTenant2, [CoTenant2Sex] = @CoTenant2Sex, [CoTenant2BirthDate] = @CoTenant2BirthDate, [CoTenant2SSNO] = @CoTenant2SSNO, [CoTenant3] = @CoTenant3, [CoTenant3Sex] = @CoTenant3Sex, [CoTenant3BirthDate] = @CoTenant3BirthDate, [CoTenant3SSNO] = @CoTenant3SSNO, [CarColor] = @CarColor, [CarModel] = @CarModel, [CarYear] = @CarYear, [CarState] = @CarState, [CarPlateNumber] = @CarPlateNumber, [Memo] = @Memo, [Address1] = @Address1, [Address2] = @Address2, [Address3] = @Address3, [Address4] = @Address4, [Phone] = @Phone, [ReferBy] = @ReferBy, [BeginDate] = @BeginDate, [NoticeGiven] = @NoticeGiven, [LeaseMonth2Month] = @LeaseMonth2Month, [LeaseEnds] = @LeaseEnds, [DepositPaid] = @DepositPaid, [DepositRefundable] = @DepositRefundable, [RefundMemo] = @RefundMemo, [RentDueDay] = @RentDueDay, [Charge1] = @Charge1, [Charge1Amount] = @Charge1Amount, [Charge2] = @Charge2, [Charge2Amount] = @Charge2Amount, [Charge3] = @Charge3, [Charge3Amount] = @Charge3Amount, [Charge4] = @Charge4, [Charge4Amount] = @Charge4Amount WHERE (([ID] = @Original_ID) AND ([TenantStatusID] = @Original_TenantStatusID) AND ((@IsNull_PropertyID = 1 AND [PropertyID] IS NULL) OR ([PropertyID] = @Original_PropertyID)) AND ((@IsNull_UnitID = 1 AND [UnitID] IS NULL) OR ([UnitID] = @Original_UnitID)) AND ((@IsNull_TenantName = 1 AND [TenantName] IS NULL) OR ([TenantName] = @Original_TenantName)) AND ((@IsNull_Sex = 1 AND [Sex] IS NULL) OR ([Sex] = @Original_Sex)) AND ((@IsNull_BirthDate = 1 AND [BirthDate] IS NULL) OR ([BirthDate] = @Original_BirthDate)) AND ((@IsNull_SSNO = 1 AND [SSNO] IS NULL) OR ([SSNO] = @Original_SSNO)) AND ((@IsNull_CoTenant1 = 1 AND [CoTenant1] IS NULL) OR ([CoTenant1] = @Original_CoTenant1)) AND ((@IsNull_CoTenant1Sex = 1 AND [CoTenant1Sex] IS NULL) OR ([CoTenant1Sex] = @Original_CoTenant1Sex)) AND ((@IsNull_CoTenant1BirthDate = 1 AND [CoTenant1BirthDate] IS NULL) OR ([CoTenant1BirthDate] = @Original_CoTenant1BirthDate)) AND ((@IsNull_CoTenant1SSNO = 1 AND [CoTenant1SSNO] IS NULL) OR ([CoTenant1SSNO] = @Original_CoTenant1SSNO)) AND ((@IsNull_CoTenant2 = 1 AND [CoTenant2] IS NULL) OR ([CoTenant2] = @Original_CoTenant2)) AND ((@IsNull_CoTenant2Sex = 1 AND [CoTenant2Sex] IS NULL) OR ([CoTenant2Sex] = @Original_CoTenant2Sex)) AND ((@IsNull_CoTenant2BirthDate = 1 AND [CoTenant2BirthDate] IS NULL) OR ([CoTenant2BirthDate] = @Original_CoTenant2BirthDate)) AND ((@IsNull_CoTenant2SSNO = 1 AND [CoTenant2SSNO] IS NULL) OR ([CoTenant2SSNO] = @Original_CoTenant2SSNO)) AND ((@IsNull_CoTenant3 = 1 AND [CoTenant3] IS NULL) OR ([CoTenant3] = @Original_CoTenant3)) AND ((@IsNull_CoTenant3Sex = 1 AND [CoTenant3Sex] IS NULL) OR ([CoTenant3Sex] = @Original_CoTenant3Sex)) AND ((@IsNull_CoTenant3BirthDate = 1 AND [CoTenant3BirthDate] IS NULL) OR ([CoTenant3BirthDate] = @Original_CoTenant3BirthDate)) AND ((@IsNull_CoTenant3SSNO = 1 AND [CoTenant3SSNO] IS NULL) OR ([CoTenant3SSNO] = @Original_CoTenant3SSNO)) AND ((@IsNull_CarColor = 1 AND [CarColor] IS NULL) OR ([CarColor] = @Original_CarColor)) AND ((@IsNull_CarModel = 1 AND [CarModel] IS NULL) OR ([CarModel] = @Original_CarModel)) AND ((@IsNull_CarYear = 1 AND [CarYear] IS NULL) OR ([CarYear] = @Original_CarYear)) AND ((@IsNull_CarState = 1 AND [CarState] IS NULL) OR ([CarState] = @Original_CarState)) AND ((@IsNull_CarPlateNumber = 1 AND [CarPlateNumber] IS NULL) OR ([CarPlateNumber] = @Original_CarPlateNumber)) AND ((@IsNull_Address1 = 1 AND [Address1] IS NULL) OR ([Address1] = @Original_Address1)) AND ((@IsNull_Address2 = 1 AND [Address2] IS NULL) OR ([Address2] = @Original_Address2)) AND ((@IsNull_Address3 = 1 AND [Address3] IS NULL) OR ([Address3] = @Original_Address3)) AND ((@IsNull_Address4 = 1 AND [Address4] IS NULL) OR ([Address4] = @Original_Address4)) AND ((@IsNull_Phone = 1 AND [Phone] IS NULL) OR ([Phone] = @Original_Phone)) AND ((@IsNull_ReferBy = 1 AND [ReferBy] IS NULL) OR ([ReferBy] = @Original_ReferBy)) AND ((@IsNull_BeginDate = 1 AND [BeginDate] IS NULL) OR ([BeginDate] = @Original_BeginDate)) AND ((@IsNull_NoticeGiven = 1 AND [NoticeGiven] IS NULL) OR ([NoticeGiven] = @Original_NoticeGiven)) AND ((@IsNull_LeaseMonth2Month = 1 AND [LeaseMonth2Month] IS NULL) OR ([LeaseMonth2Month] = @Original_LeaseMonth2Month)) AND ((@IsNull_LeaseEnds = 1 AND [LeaseEnds] IS NULL) OR ([LeaseEnds] = @Original_LeaseEnds)) AND ((@IsNull_DepositPaid = 1 AND [DepositPaid] IS NULL) OR ([DepositPaid] = @Original_DepositPaid)) AND ((@IsNull_DepositRefundable = 1 AND [DepositRefundable] IS NULL) OR ([DepositRefundable] = @Original_DepositRefundable)) AND ((@IsNull_RentDueDay = 1 AND [RentDueDay] IS NULL) OR ([RentDueDay] = @Original_RentDueDay)) AND ((@IsNull_Charge1 = 1 AND [Charge1] IS NULL) OR ([Charge1] = @Original_Charge1)) AND ((@IsNull_Charge1Amount = 1 AND [Charge1Amount] IS NULL) OR ([Charge1Amount] = @Original_Charge1Amount)) AND ((@IsNull_Charge2 = 1 AND [Charge2] IS NULL) OR ([Charge2] = @Original_Charge2)) AND ((@IsNull_Charge2Amount = 1 AND [Charge2Amount] IS NULL) OR ([Charge2Amount] = @Original_Charge2Amount)) AND ((@IsNull_Charge3 = 1 AND [Charge3] IS NULL) OR ([Charge3] = @Original_Charge3)) AND ((@IsNull_Charge3Amount = 1 AND [Charge3Amount] IS NULL) OR ([Charge3Amount] = @Original_Charge3Amount)) AND ((@IsNull_Charge4 = 1 AND [Charge4] IS NULL) OR ([Charge4] = @Original_Charge4)) AND ((@IsNull_Charge4Amount = 1 AND [Charge4Amount] IS NULL) OR ([Charge4Amount] = @Original_Charge4Amount)));
SELECT ID, TenantStatusID, PropertyID, UnitID, TenantName, Sex, BirthDate, SSNO, CoTenant1, CoTenant1Sex, CoTenant1BirthDate, CoTenant1SSNO, CoTenant2, CoTenant2Sex, CoTenant2BirthDate, CoTenant2SSNO, CoTenant3, CoTenant3Sex, CoTenant3BirthDate, CoTenant3SSNO, CarColor, CarModel, CarYear, CarState, CarPlateNumber, Memo, Address1, Address2, Address3, Address4, Phone, ReferBy, BeginDate, NoticeGiven, LeaseMonth2Month, LeaseEnds, DepositPaid, DepositRefundable, RefundMemo, RentDueDay, Charge1, Charge1Amount, Charge2, Charge2Amount, Charge3, Charge3Amount, Charge4, Charge4Amount FROM tblTenant WHERE (ID = @ID)',N'@TenantStatusID int,@PropertyID int,@UnitID int,@TenantName nvarchar(4),@Sex nvarchar(1),@BirthDate datetime,@SSNO nvarchar(9),@CoTenant1 nvarchar(4000),@CoTenant1Sex nvarchar(4000),@CoTenant1BirthDate datetime,@CoTenant1SSNO nvarchar(4000),@CoTenant2 nvarchar(4000),@CoTenant2Sex nvarchar(4000),@CoTenant2BirthDate datetime,@CoTenant2SSNO nvarchar(4000),@CoTenant3 nvarchar(2),@CoTenant3Sex nvarchar(4000),@CoTenant3BirthDate datetime,@CoTenant3SSNO nvarchar(4000),@CarColor nvarchar(4000),@CarModel nvarchar(4000),@CarYear nvarchar(4000),@CarState nvarchar(4000),@CarPlateNumber nvarchar(7),@Memo nvarchar(4000),@Address1 nvarchar(10),@Address2 nvarchar(10),@Address3 nvarchar(10),@Address4 nvarchar(10),@Phone nvarchar(4000),@ReferBy nvarchar(9),@BeginDate datetime,@NoticeGiven datetime,@LeaseMonth2Month bit,@LeaseEnds datetime,@DepositPaid money,@DepositRefundable money,@RefundMemo nvarchar(4000),@RentDueDay nvarchar(4000),@Charge1 int,@Charge1Amount money,@Charge2 int,@Charge2Amount money,@Charge3 int,@Charge3Amount money,@Charge4 int,@Charge4Amount money,@Original_ID int,@Original_TenantStatusID int,@IsNull_PropertyID int,@Original_PropertyID int,@IsNull_UnitID int,@Original_UnitID int,@IsNull_TenantName int,@Original_TenantName nvarchar(4),@IsNull_Sex int,@Original_Sex nvarchar(1),@IsNull_BirthDate int,@Original_BirthDate datetime,@IsNull_SSNO int,@Original_SSNO nvarchar(9),@IsNull_CoTenant1 int,@Original_CoTenant1 nvarchar(4000),@IsNull_CoTenant1Sex int,@Original_CoTenant1Sex nvarchar(4000),@IsNull_CoTenant1BirthDate int,@Original_CoTenant1BirthDate datetime,@IsNull_CoTenant1SSNO int,@Original_CoTenant1SSNO nvarchar(4000),@IsNull_CoTenant2 int,@Original_CoTenant2 nvarchar(4000),@IsNull_CoTenant2Sex int,@Original_CoTenant2Sex nvarchar(4000),@IsNull_CoTenant2BirthDate int,@Original_CoTenant2BirthDate datetime,@IsNull_CoTenant2SSNO int,@Original_CoTenant2SSNO nvarchar(4000),@IsNull_CoTenant3 int,@Original_CoTenant3 nvarchar(2),@IsNull_CoTenant3Sex int,@Original_CoTenant3Sex nvarchar(4000),@IsNull_CoTenant3BirthDate int,@Original_CoTenant3BirthDate datetime,@IsNull_CoTenant3SSNO int,@Original_CoTenant3SSNO nvarchar(4000),@IsNull_CarColor int,@Original_CarColor nvarchar(4000),@IsNull_CarModel int,@Original_CarModel nvarchar(4000),@IsNull_CarYear int,@Original_CarYear nvarchar(4000),@IsNull_CarState int,@Original_CarState nvarchar(4000),@IsNull_CarPlateNumber int,@Original_CarPlateNumber nvarchar(7),@IsNull_Address1 int,@Original_Address1 nvarchar(10),@IsNull_Address2 int,@Original_Address2 nvarchar(10),@IsNull_Address3 int,@Original_Address3 nvarchar(10),@IsNull_Address4 int,@Original_Address4 nvarchar(10),@IsNull_Phone int,@Original_Phone nvarchar(4000),@IsNull_ReferBy int,@Original_ReferBy nvarchar(9),@IsNull_BeginDate int,@Original_BeginDate datetime,@IsNull_NoticeGiven int,@Original_NoticeGiven datetime,@IsNull_LeaseMonth2Month int,@Original_LeaseMonth2Month bit,@IsNull_LeaseEnds int,@Original_LeaseEnds datetime,@IsNull_DepositPaid int,@Original_DepositPaid money,@IsNull_DepositRefundable int,@Original_DepositRefundable money,@IsNull_RentDueDay int,@Original_RentDueDay nvarchar(4000),@IsNull_Charge1 int,@Original_Charge1 int,@IsNull_Charge1Amount int,@Original_Charge1Amount money,@IsNull_Charge2 int,@Original_Charge2 int,@IsNull_Charge2Amount int,@Original_Charge2Amount money,@IsNull_Charge3 int,@Original_Charge3 int,@IsNull_Charge3Amount int,@Original_Charge3Amount money,@IsNull_Charge4 int,@Original_Charge4 int,@IsNull_Charge4Amount int,@Original_Charge4Amount money,@ID int',@TenantStatusID=1,@PropertyID=111,@UnitID=2175,@TenantName=N'Judy',@Sex=N'1',@BirthDate='Apr 29 1980 3:26:06:000PM',@SSNO=N'333333333',@CoTenant1=NULL,@CoTenant1Sex=NULL,@CoTenant1BirthDate=NULL,@CoTenant1SSNO=NULL,@CoTenant2=NULL,@CoTenant2Sex=NULL,@CoTenant2BirthDate=NULL,@CoTenant2SSNO=NULL,@CoTenant3=N'51',@CoTenant3Sex=NULL,@CoTenant3BirthDate=NULL,@CoTenant3SSNO=NULL,@CarColor=NULL,@CarModel=NULL,@CarYear=NULL,@CarState=NULL,@CarPlateNumber=N'001122B',@Memo=NULL,@Address1=N'blah line1',@Address2=N'blah line2',@Address3=N'blah line3',@Address4=N'blah line4',@Phone=NULL,@ReferBy=N'McDonalds',@BeginDate='May 6 2009 4:54:28:000PM',@NoticeGiven='May 6 2009 11:58:45:000AM',@LeaseMonth2Month=1,@LeaseEnds='May 21 2009 10:06:25:000AM',@DepositPaid=50.0000,@DepositRefundable=0.0000,@RefundMemo=NULL,@RentDueDay=NULL,@Charge1=6,@Charge1Amount=50.0000,@Charge2=49,@Charge2Amount=50.0000,@Charge3=45,@Charge3Amount=5.0000,@Charge4=47,@Charge4Amount=7.0000,@Original_ID=10,@Original_TenantStatusID=1,@IsNull_PropertyID=0,@Original_PropertyID=111,@IsNull_UnitID=0,@Original_UnitID=2175,@IsNull_TenantName=0,@Original_TenantName=N'Judy',@IsNull_Sex=0,@Original_Sex=N'1',@IsNull_BirthDate=0,@Original_BirthDate='Apr 29 1980 3:26:06:000PM',@IsNull_SSNO=0,@Original_SSNO=N'333333333',@IsNull_CoTenant1=1,@Original_CoTenant1=NULL,@IsNull_CoTenant1Sex=1,@Original_CoTenant1Sex=NULL,@IsNull_CoTenant1BirthDate=1,@Original_CoTenant1BirthDate=NULL,@IsNull_CoTenant1SSNO=1,@Original_CoTenant1SSNO=NULL,@IsNull_CoTenant2=1,@Original_CoTenant2=NULL,@IsNull_CoTenant2Sex=1,@Original_CoTenant2Sex=NULL,@IsNull_CoTenant2BirthDate=1,@Original_CoTenant2BirthDate=NULL,@IsNull_CoTenant2SSNO=1,@Original_CoTenant2SSNO=NULL,@IsNull_CoTenant3=0,@Original_CoTenant3=N'51',@IsNull_CoTenant3Sex=1,@Original_CoTenant3Sex=NULL,@IsNull_CoTenant3BirthDate=1,@Original_CoTenant3BirthDate=NULL,@IsNull_CoTenant3SSNO=1,@Original_CoTenant3SSNO=NULL,@IsNull_CarColor=1,@Original_CarColor=NULL,@IsNull_CarModel=1,@Original_CarModel=NULL,@IsNull_CarYear=1,@Original_CarYear=NULL,@IsNull_CarState=1,@Original_CarState=NULL,@IsNull_CarPlateNumber=0,@Original_CarPlateNumber=N'001122B',@IsNull_Address1=0,@Original_Address1=N'blah line1',@IsNull_Address2=0,@Original_Address2=N'blah line2',@IsNull_Address3=0,@Original_Address3=N'blah line3',@IsNull_Address4=0,@Original_Address4=N'blah line4',@IsNull_Phone=1,@Original_Phone=NULL,@IsNull_ReferBy=0,@Original_ReferBy=N'McDonalds',@IsNull_BeginDate=0,@Original_BeginDate='May 6 2009 4:54:28:000PM',@IsNull_NoticeGiven=0,@Original_NoticeGiven='May 6 2009 11:58:45:000AM',@IsNull_LeaseMonth2Month=0,@Original_LeaseMonth2Month=1,@IsNull_LeaseEnds=0,@Original_LeaseEnds='May 21 2009 10:06:25:000AM',@IsNull_DepositPaid=0,@Original_DepositPaid=50.0000,@IsNull_DepositRefundable=0,@Original_DepositRefundable=0.0000,@IsNull_RentDueDay=1,@Original_RentDueDay=NULL,@IsNull_Charge1=0,@Original_Charge1=6,@IsNull_Charge1Amount=0,@Original_Charge1Amount=50.0000,@IsNull_Charge2=0,@Original_Charge2=49,@IsNull_Charge2Amount=0,@Original_Charge2Amount=50.0000,@IsNull_Charge3=0,@Original_Charge3=45,@IsNull_Charge3Amount=0,@Original_Charge3Amount=5.0000,@IsNull_Charge4=0,@Original_Charge4=47,@IsNull_Charge4Amount=0,@Original_Charge4Amount=7.0000,@ID=10
需要大约一整秒才能完成。我该怎么办呢?
答案 0 :(得分:2)
您可以将Sql Profiler附加到服务器以查看到底发生了什么,以及Sql语句是否真的相同。
如果它们存在差异,那么其中一个应用程序可能正在使用不同的执行计划。使用以下方法清理缓存:
DBCC FREEPROCCACHE
并检查它是否有所作为。
此related post中有许多其他建议。
答案 1 :(得分:2)
我的猜测是,10秒是本地计算机上(不经常使用的)数据库启动所需的时间。我实际上并没有尝试解决这个问题,但我的第一个猜测是尝试禁用该数据库的“自动关闭”属性。
有关“自动关闭”属性的进一步说明,请参阅this exerpt from the book titled 'Designing SQL Server 2000 Databases for .NET Enterprise Servers'。
答案 2 :(得分:1)
您的本地计算机还在做什么?你好吗?如果延迟在数据库中,我会感到惊讶,除非其他加载的应用程序导致交换。
答案 3 :(得分:1)
本地计算机和远程计算机可能具有不同的客户端网络协议设置。例如。本地计算机配置为首先尝试共享内存然后尝试net-pipes然后tcp,但服务器配置为不侦听共享内存(异常,但可能)。远程机器配置为直接尝试tcp,因此他们从第一次尝试中获得了累积奖金。只是在黑暗中拍摄,可以获得更多信息。
您应首先分析问题并确定10秒延迟发生的位置:打开连接时,运行查询时,将结果返回给客户端等。
答案 4 :(得分:0)
可能由SQL Server尝试在本地计算机上使用的特定通信协议决定。
你需要发帖 - 你的连接字符串 - 服务器计算机上启用的协议(来自SQL Server配置管理器)。