嵌套循环优化

时间:2012-10-04 20:07:19

标签: c# sql optimization nested

大家好我有一个奇怪的优化问题,这里是我为了简单而更改了一些名称的代码

CollectionObject mycollobj = new CollectionObject();
List<string> MyProducts = new List<string>();
//get collection of selected customers that were passed in
var chckedValues = form.GetValues("assignChkBx");
foreach(string customer in chckedValues )
{
    MyProducts.Clear();
    //MyProducts is then set to a data access method in my data access class
    MyProducts = DataLayerClass.GetProductsFromCustomer(customer);
    foreach(string product in MyProducts)
    {
       string item1 = DataLayerClass.GetItem1(product);
       string item2 = DataLayerClass.GetItem2(product);
       mycollobj.loaditems(item1, item2);
    }
}

基本上mycollobj是一个黑盒子,用于一些相当复杂的分析(我无法控制)。有没有更好的方法来运行这种嵌套算法?任何建议都很重要,请询问您是否需要澄清任何内容。谢谢!

1 个答案:

答案 0 :(得分:3)

是的,这一行:MyProducts = DataLayerClass.GetProductsFromCustomer(customer);会减慢速度(每个客户的数据库调用),嵌套的DataLayerClass.GetItem1()/GetItem2也会让事情变得更糟。而是将所有checkedValues发送到数据库并返回与客户的查询以及包含Tupleitem1的{​​{1}}:

item2

简而言之,将逻辑移至单个数据库查询。

相关问题