C# 优化和加速非常慢的Linq/SQL代码

C# 优化和加速非常慢的Linq/SQL代码,c#,sql,sql-server,linq,sql-server-2008,C#,Sql,Sql Server,Linq,Sql Server 2008,我使用以下C#方法预加载股票数据表。虽然它工作得很好,但现在我的表中有很多行,加载速度可能非常慢 有人能推荐一种更好更快的方法吗?(最好是删除“foreach”代码,因为这是一个缓慢的代码!) Products和ProductStocks表的架构是什么?你有什么索引?从阅读开始 有些事情立竿见影: 您正在从客户机上的服务器获取所有数据。不要。后端的进程 使用Deleted位字段会导致(性能)灾难。您可以将其添加到最左边的聚集索引键中,最多只能得到可疑的结果。按分区可以有所帮助,但不是很多。没有

我使用以下C#方法预加载股票数据表。虽然它工作得很好,但现在我的表中有很多行,加载速度可能非常慢

有人能推荐一种更好更快的方法吗?(最好是删除“foreach”代码,因为这是一个缓慢的代码!)

Products和ProductStocks表的架构是什么?你有什么索引?从阅读开始

有些事情立竿见影:

  • 您正在从客户机上的服务器获取所有数据。不要。后端的进程
  • 使用
    Deleted
    位字段会导致(性能)灾难。您可以将其添加到最左边的聚集索引键中,最多只能得到可疑的结果。按分区可以有所帮助,但不是很多。没有银弹。尽量消除这一要求。删除已删除的行

没有太多的优化空间。停止提取所有数据。

最后,此函数被转换为存储过程,存储过程返回在服务器上而不是客户端上创建的表。这几乎是即时和大规模的性能改进

你的档案显示了什么?这段代码中db on的速度慢吗?Common.Utilities.IsValueValidCimal的代码是什么?请在发布之前花更多的精力格式化代码-查看您将要发布的问题,并询问它的格式是否符合您在回答时希望看到的格式。var query=(query).toList()。。这将把所有数据加载到查询变量中,我认为当您进行迭代时,这将提高性能。这是linq到sql还是实体框架?谢谢,我收集到了这将是响应。我将研究在服务器上而不是客户端上的处理。
public static DataTable GetProducts()
{
    DataTable table = new DataTable();

    using (DataClassesDataContext data = new DataClassesDataContext(cDbConnection.GetConnectionString()))
    {
        var query = (from p in data.Products
                     where p.Deleted == false
                     join s in data.ProductStocks on p.ProductID equals s.ProductID
                     group s by p into g
                     select new { g });


        table.Columns.Add("Barcode", typeof(string));
        table.Columns.Add("Stock Code", typeof(string));
        table.Columns.Add("Description", typeof(string));
        table.Columns.Add("Price", typeof(string));
        table.Columns.Add("Tax", typeof(string));
        table.Columns.Add("Stock", typeof(string));
        table.Columns.Add("Service Item", typeof(bool));
        table.Columns.Add("Deduct Stock", typeof(bool));

        if (query != null)
        {
            foreach (var item in query)
            {
                try
                {
                    decimal? Tax = 0;
                    if (item.g.Key.ProductTax != null)
                    {
                        Tax = Common.Utilities.IsValueValidDecimal(item.g.Key.ProductTax.TaxRate, 0);   // Tax
                    }
                    else
                    {
                        Tax = 0;
                    }

                    bool DeductStock = !Convert.ToBoolean(item.g.Key.ServiceItem);

                    string[] row = new string[] 
                    {
                        item.g.Key.EANCode.ToString(),       // Barcode
                        item.g.Key.OurStockCode.ToString(),  // Product Code
                        item.g.Key.Description.ToString(),   // desc
                        GetGUIDisplayPrice(item.g.Key.RetailPrice, item.g.Key.RetailPriceExVAT),  // cost
                        Tax.ToString(),                         // Tax   
                        item.g.Sum(s => s.QtyOnHand).ToString(), // Stock
                        item.g.Key.ServiceItem.ToString(),   // Service Item (non-stock)
                        DeductStock.ToString()                  // if not a service item, the its a stocked item so deduct!
                    };

                    table.Rows.Add(row);
                }
                catch (Exception ex)
                {
                }
            }
        }//ENDIF NULL
    }//END USING

    return table;
}
from p in data.Products
                 where p.Deleted == false
                 join s in data.ProductStocks on p.ProductID equals s.ProductID
                 group s by p into g
                 select new { g }