0

我的查询似乎每通过查询一次就停止了。

        status_text.Text = "Check existing records...";
        status_text.Refresh();
        using (StreamReader reader = new StreamReader(df_text_filename))
        {
            using (StreamWriter writer = new StreamWriter(df_text_filename + "_temp"))
            {
                while ((product = reader.ReadLine()) != null)
                {
                    if (product != _aff_svc.DFHeaderProd)
                    {
                        df_product = _product_factory.GetProductData(_vsi, product);
                    }
                    status_text.Text = "Checking for existing record of vendor record ID " + df_product.SKU;
                    status_text.Refresh();
                    if (_pctlr.GetBySKU(df_product.SKU) != null)
                    {
                        continue;
                    }
                    writer.WriteLine(product);
                    Application.DoEvents();
                }
                writer.Close();
            }
            reader.Close();
        }
        System.IO.File.Delete(df_text_filename);
        System.IO.File.Move(df_text_filename + "_temp", df_text_filename);  

代码快速运行 GetBySKU 大约 10 次,暂停大约一秒钟左右,然后快速执行另外 10 条记录。这发生在我的整个过程中,而不仅仅是这个特定的查询。

无论我是否有 Application.DoEvents() 触发,它也会发生。

另一个问题是不一致。我可以像这样工作几个小时,然后突然之间,它会按预期(预期)通过循环。

我的 SQL 服务器与程序在同一台机器上运行。

我研究了将资源专用于服务器以减轻这种行为,但一无所获。

4

2 回答 2

1

看起来你的程序正在解析一个文本文件以获取产品信息,然后在它解析时,在一个 while循环中你正在执行几个 SQL 查询。在循环中进行 SQL 往返几乎总是一个坏主意。

相反,我会考虑解析文件,收集所有产品创意,关闭文件,然后调用 sql 将一个/许多TVP(表值参数)传递给一个存储过程,并从该存储过程返回您需要的所有数据 -可能有尽可能多的表。

编辑:您在评论中提到该文件非常大,需要进行大量处理。您可以考虑批量处理 SQL 工作,比如说 100?

此外,如果您没有调整 SQL,它会随着写入更多数据而不断变慢。问题中没有足够的信息来分析索引、查询计划等……但是随着数据集的增长,请看一下。

于 2013-05-04T12:48:27.543 回答
0

稍后我将处理批处理解决方案,但是,这比以前的代码工作得快得多。完全没有停顿。

            List<Product> _prod_list = new List<Product>();
            _prod_list = ProductDataFactory.GetProductListByVendor(vendor_name);
            if (_prod_list.Count() > 0)
            {
                using (StreamReader reader = new StreamReader(df_text_filename))
                {
                    using (StreamWriter writer = new StreamWriter(df_text_filename + "_temp"))
                    {
                        while ((product = reader.ReadLine()) != null)
                        {
                            if (product != _aff_svc.DFHeaderProd)
                            {
                                df_product = _product_factory.GetProductData(_vsi, product);
                            }
                            if (_prod_list.Find(o => o.SKU == df_product.SKU) != null)
                            {
                                continue;
                            }
                            writer.WriteLine(product);
                        }
                        writer.Close();
                    }
                    reader.Close();
                }
                System.IO.File.Delete(df_text_filename);
                System.IO.File.Move(df_text_filename + "_temp", df_text_filename);
            }

只需拉出产品对象列表并查询现有记录(如果有的话);如果没有,它当然会跳过整个过程。也不需要在循环中点击数据库。

谢谢。

于 2013-05-05T09:11:20.873 回答