問題:
最近程序總會拋出如下異常:
An exception of type'System.Data.SqlClient.SqlException' occurred and was caught.
----------------------------------------------------------------------------------
Type :System.Data.SqlClient.SqlException, System.Data, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Timeoutexpired. The timeout period elapsedprior to completion of the operation or the server is not responding.
Source : .Net SqlClient Data Provider......
......Database.ExecuteReader(DbCommandcommand)......
讀取數據超時。
然後就去看了下DataBase裏對應的tables,發現table的結構簡單,但是數據量很大,有幾十萬上百萬條。再看一下有沒有建Indexes/Keys,發現沒有,恍然大悟。
解決方法:
根據表的結構,建立了Indexes/Keys下的 Unique key,類似如下:
ALTER TABLE [dbo].[ZData]
add CONSTRAINT [IX_ZData] UNIQUE NONCLUSTERED
(
[Pid] ASC,
[EntryID] ASC,
[Date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
測試,發現速度快很多,減小了timeout的風險。