GridView Paging 성능 비교

안녕 하세요?? ㅋㅋ 오랜만에 인사를 하죠?

이번에는 전에 제가 올렸던 두가지 GridView Paging 방법에 대한 성능을 비교해 보겠습니다.. 재미 날것 같죠??

제가 비교할 방법은 앞에서 올렸던 글 - "GridView Paging width LINQ To DataSet "글 - "GridView Paging width LINQ using stored procedure " 입니다.
채택한 방법은 System.Diagnostics 네임 스페이스 밑에 있는 Stopwatch 클래스를 사용 하였습니다.
사용할 DATABASE는 Northwind 이고, 사용할 테이블은 Products 입니다.
우선 위의 두 글에서 사용했던 함수의 성능을 비교 하기 위해 클래스 하나를 추가 하겠습니다. 저는 App_Code폴더 밑에 "Performence"라는 이름으로 클래스를 추가 했습니다. 이 클래스에서 사용할 함수는 위의 두 글에서 사용했던 겉과 똑같은 함수 입니다. 다른점은 제가 성능을 비교하기 위하여 추가된 Stopwatch 타입의 인스턴스와 Return 타입 입니다. 그 함수들은 아래와 같습니다.

public string GetProduct(int maximumRows, int startRowIndex)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        NorthwindClassesDataContext db = new NorthwindClassesDataContext();
        var result = (from p in db.Products
                      select new
                      {
                          productid = p.ProductID,
                          productname = p.ProductName,
                          unitprice = p.UnitPrice
                      }).Skip(startRowIndex).Take(maximumRows);
        DataTable dt = new DataTable();
        dt.Columns.Add("productid", typeof(string));
        dt.Columns.Add("productname", typeof(string));
        dt.Columns.Add("unitprice", typeof(decimal));

        foreach (var product in result)
        {
            dt.Rows.Add(new object[] { product.productid, product.productname, product.unitprice });
        }

        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        sw.Stop();
        string time = sw.Elapsed.ToString() ;
        return time;
    }

public string getProductsByStoredProcedure(int startRowIndex, int maximumRows)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        startRowIndex += 1;
        NorthwindClassesDataContext db = new NorthwindClassesDataContext();
        IList product = db.usp_pagingProducts(startRowIndex, startRowIndex + maximumRows - 1).ToList();
        string time = sw.Elapsed.ToString();
        return time;
    }

여기서 사용할 Stored Procedure은 기존에 사용 했던 것인데 편리를 위하여 다시 올려 드리겠습니다.
CREATE procedure usp_pagingProducts
@startIndex int,
@endIndex int
AS
select ProductID, ProductName,unitprice,row_num from
(
 select *, row_number() over (order by ProductID) as row_num from Products
) product
where row_num between @startIndex and @endIndex
당연히 이 Stored Procedure은 .dbml파일에 추가를 해야 겠죠?

============================================================================================
위의 클래스를 완성 했으면 웹폼을 하나 추가 하고 아래와 같이 .aspx의 코드를 수정 합니다.
***.aspx
============
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="border:solid 1px #555555">
            <tr>
                <td>LINQ TO DATASET:</td>
                <td><asp:Label ID="lblDataSet" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td>Stored Procedure:</td>
                <td><asp:Label ID="lblStoredProcedure" runat="server"></asp:Label></td>
            </tr>
        </table>
        <asp:Button ID="Button1" runat="server" Text="클릭" OnClick="Button1_OnClick" />
    </div>
    </form>
</body>
</html>

위의 버튼과 매핑되는 메서드는 아래와 같습니다.
protected void Button1_OnClick(object sender, EventArgs e)
    {
        string time1 = new Performence().GetProduct(21, 30);
        string time2 = new Performence().getProductsByStoredProcedure(21, 30);
        lblDataSet.Text = time1;
        lblStoredProcedure.Text = time2;
    }
===========================================================================================
모두 완성 되였으면 실행 시켜 보시고 결과를 확인 하세요. 제가 확인한 결과 중에서 임의의 두개를 이미지로 올렸습니다.


보시는 바와 같이 성능면에서 Stored Procedure가 LINQ To DataSet보다 7배 이상 빠른거죠...

posted @ 2009-04-26 14:17  OOK  阅读(352)  评论(6编辑  收藏  举报