优化加载和搜索速度 ASP.NET C#

Optimizing load and search speeds ASP.NET C#

我是 ASP.NET 的新手,但是在学习了一些基础知识之后,多亏了 google,我设法设置了一个页面来满足我的需要。

Web 应用程序应该从我使用本地主机上的另一个 Windows 应用程序生成的 XML 文件加载数据,显示此数据并允许用户搜索它。

这个 XML 文件有超过 50 MB 和超过 120.000 个条目。

我正在将这个 XML 文件读入数据集,然后我将其绑定到 gridView。

问题是:

我该如何解决这个问题? 我尝试了 StateView,但是这导致了 "Ran out of Memory" 异常。 我做了一些研究,看来我可以将这个数据集保存在服务器缓存中,这样所有用户都可以立即访问它,而无需每次为每个用户重新加载 XML ?

这是我目前的代码,如果有什么问题请告诉我,因为我不知道 ASP.NET。谢谢

 public DataSet ds = new DataSet();
    public DataSet resultDS = new DataSet();
    public bool searchListActive = false;
    string _sortDirection = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
        if (!IsPostBack)
        {
            gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
            ViewState["searchListActive"] = false;
            BindCB();
        }

        gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
    }



    void BindGrid()
    {
        ds.ReadXml(Server.MapPath("~/lstData.xml"));
        gridView_IndexData.DataSource = ds;
        gridView_IndexData.DataBind();
    }

    void BindCB()
    {
        DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "forumName");
        var DR = dt.NewRow();
        DR["forumName"] = "All forums";
        dt.Rows.InsertAt(DR, 0);
        dt.AcceptChanges();
        ddList_Forum.DataSource = dt;
        ddList_Forum.DataTextField = "forumName";
        ddList_Forum.DataBind();
    }



    protected void btnSearchQuery_Click(object sender, EventArgs e)
    {
        resultDS = ds.Clone();

        string searchQuery = "";

        searchQuery = "TopicTitle LIKE '%" + tbSearchInput.Text + "%'";

        if (tbSearchByUsername.Text.Length > 0)
        {
            searchQuery += "AND UserName ='" + tbSearchByUsername.Text + "'";
        }

        if (ddList_Type.Text != "")
        {
            searchQuery += "AND Type ='" + ddList_Type.Text + ":'";
        }

        if (ddList_Forum.Text != "All forums")
        {
            searchQuery += "AND forumName ='" + ddList_Forum.Text + "'";
        }


        var results = ds.Tables[0].Select(searchQuery);
        resultDS.Tables.Add();

        foreach (DataRow dr in results)
        {
            resultDS.Tables[0].ImportRow(dr);
        }

        resultDS.AcceptChanges();
        gridView_IndexData.DataSource = resultDS.Tables[0];
        ViewState["searchListActive"] = true;
        ViewState["resultDS"] = resultDS;
        gridView_IndexData.DataBind();

    }

    protected void gridView_IndexData_Sorting(object sender, GridViewSortEventArgs e)
    {
        SetSortDirection(e.SortDirection.ToString());
        ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + _sortDirection;
        gridView_IndexData.DataSource = ds.Tables[0];
        gridView_IndexData.DataBind();
    }

    void SetSortDirection(string sortDirection)
    {
        if (sortDirection == "Descending")
        {
            _sortDirection = "DESC";
        }
        else
        {
            _sortDirection = "ASC";
        }
    }

    protected void gridView_IndexData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridView_IndexData.PageIndex = e.NewPageIndex;
        if ((bool)ViewState["searchListActive"] == true)
        {
            gridView_IndexData.DataSource = (DataSet)ViewState["resultDS"];
        }
        gridView_IndexData.DataBind();
    }

我会结合使用适当的 xml 处理方式,例如使用 xml 阅读器。这是一篇关于此的文章 (http://forums.asp.net/t/1939295.aspx?Most+efficient+way+to+iterate+through+XML). And, by all means maximize your use of caching when possible. Here are a few pointers on that (Data Caching in ASP.Net)。

1) 如果您将共享相同的数据集实例,那么当并发用户同时搜索时会出现错误

2) 优化搜索速度将您的结构转换为 LINQ 而不是数据集查询