一.建立數據庫Liezui_Test
ID int 主鍵 自增
Title varchar(100)
ReadNum int
二.向數據庫中插入100萬條數據
declare @i int
set @i=1
while @i<=500000
begin
insert into Liezui_Test(Title,ReadNum) values('執行總數統計',@i)
set @i=@i+1
end
GO
declare @i int
set @i=1
while @i<=500000
begin
insert into Liezui_Test(Title,ReadNum) values('毛巾因經常處於潮濕狀態而極易滋生有害細菌',@i)
set @i=@i+1
end
GO
三.增加SelectByTitle存儲過程
Create PROCEDURE [dbo].[SelectByTitle]
AS
BEGIN
Select top 10000 * from Liezui_Test where Title Like '%執行%'
END
三.開始測試
首先在頁面內放一個repeater 用於綁定數據 二個label 用於顯示結果
測試場景一 : 不綁定Repeater,只進行數據庫源的綁定
代碼如下:
Stopwatch st = new Stopwatch();
st.Start();
Repeater1.DataSource = Jinlong.Data.DBHelper.ReturnDataSet("Select top 10000 * from Liezui_Test where Title Like '%執行%'").Tables[0];
st.Stop();
Label1.Text = st.ElapsedMilliseconds.ToString() + "ms";
Stopwatch st2 = new Stopwatch();
st2.Start();
SqlParameter[] para = { };
Repeater1.DataSource = Jinlong.Data.DBHelper.RunProcedure("SelectByTitle", para, "ds");
st2.Stop();
Label2.Text = st2.ElapsedMilliseconds.ToString() + "ms";
結果如下:
Label1 Label2
52ms 48ms
39ms 46ms
45ms 44ms
43ms 42ms
37ms 40ms
43ms 44ms
結論:用Sql語句和存儲過程的速度差不多.
測試場景二 : 綁定Repeater
代碼如下:
Stopwatch st = new Stopwatch();
st.Start();
Repeater1.DataSource = Jinlong.Data.DBHelper.ReturnDataSet("Select top 10000 * from Liezui_Test where Title Like '%執行%'").Tables[0];
Repeater1.DataBind();
st.Stop();
Label1.Text = st.ElapsedMilliseconds.ToString() + "ms";
Stopwatch st2 = new Stopwatch();
st2.Start();
SqlParameter[] para = { };
Repeater1.DataSource = Jinlong.Data.DBHelper.RunProcedure("SelectByTitle", para, "ds");
Repeater1.DataBind();
st2.Stop();
Label2.Text = st2.ElapsedMilliseconds.ToString() + "ms";
結果如下:
Label1 Label2
161ms 192ms
205ms 191ms
142ms 208ms
153ms 198ms
134ms 209ms
280ms 335ms
結論:用存儲過程的速度居然比直接用Sql語句還要慢.