【问题标题】:Really Slow Performance Reading from Access 2007 (accdb) file from C#从 C# 读取 Access 2007 (accdb) 文件的性能真的很慢
【发布时间】:2011-11-28 04:01:57
【问题描述】:

我正在使用 C# 在 Visual Studio 2008 上编写应用程序。应用程序从访问文件中读取数据,然后生成一个 txt 文件。我正在使用具有 1.000.000 条记录和几乎 1GB 大小的 mdb 文件进行一些测试。 代码是这样的,整个过程需要 8 到 10 分钟才能完成。

var connStr =  string.Format("Provider =Microsoft.Jet.OLEDB.4.0; Data Source={0};Persist Security Info=False", this.dbPath);

using (var conn = new OleDbConnection(connStr))
{
            using (var command = conn.CreateCommand())
            {   

                command.CommandText = "SELECT * from Registros r, FOIDS f where r.TICKET = f.TICKET";
                command.CommandType = System.Data.CommandType.Text;
                conn.Open();

                int i = 0;
                string ticket = string.Empty;

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                         i++;

                            if (!reader.IsDBNull(reader.GetOrdinal("r.TICKET")))
                            {
                                ticket=reader.GetString(reader.GetOrdinal("r.TICKET"));
                                // Some process
                            }
                        }
                    }
                }
          }
    }

今天我收到了一个 accdb 文件(Access 2007),所以我把我的连接字符串改成了这个:

connStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False", this.dbPath);

但是在更改之后,每条记录读取新文件大约需要 4-5 秒!!!所以,我的整个过程需要很多天才能完成! 我尝试将 accdb 文件转换为旧的 mdb 文件,然后使用以前的连接字符串再次读取,但问题仍然存在。我认为是数据库本身的问题,但我不知道该怎么办,在互联网上我没有找到任何有关此类问题的信息。

有什么想法吗?有什么建议吗?

【问题讨论】:

  • 是否可能不是读取 MDB 花费了很多时间,而是“//某个进程”部分?你可以做异步的东西吗?
  • 我做了一些调试,延迟在 reader.Read() 指令上。此外,我添加了一些具有相同结果的日志,并且我已经用相同的结果评论了该过程。感谢您的快速回复!
  • 你试过压缩数据库吗?
  • 是否有可能将数据库改为 SQL?
  • 将 TICKET 列编入索引是必不可少的。

标签: c# performance ms-access ms-office oledb


【解决方案1】:

根据我 5 年前的经验。

其中一个 access 数据库超过 20000 条记录,大小约为 150 MB。它开始变慢,性能直线下降。所以转移到 SQL。

您可以考虑使用 SQL compact edition 或 sql lite

谢谢

【讨论】:

  • 150MB 对于 Access/Jet/ACE 来说太小了。如果这会导致性能问题,那么您的数据架构或应用程序中的设计都很糟糕。但是,在本例中,对于 1GB 文件,我建议升级到服务器数据库,因为这只是接近 Access/Jet/ACE 文件的 2GB 硬限制的一种方式。
  • 如果这么少的记录导致性能不佳,我首先会怀疑笛卡尔连接,其次是缺少索引,或者两者兼而有之。其他数据库系统可以优化笛卡尔连接,但 IIRC Jet 没有(或当时没有)。
【解决方案2】:

除了切换到建议的数据库之一,您还应该将ticket 的对象类型从string 更改为StringBuilder

记住……

The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

考虑到这一点,请考虑每次设置 ticket 变量时在 while 循环中创建的开销。 - 您提到您有 100 万条记录,这意味着您的代码正在创建 100 万个字符串对象。

使用StringBuilder 类型代替string 类型,您的代码可能如下所示...

....
StringBuilder ticket = new StringBuilder();

using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
         i++;

            if (!reader.IsDBNull(reader.GetOrdinal("r.TICKET")))
            {
                ticket.Append(reader.GetString(reader.GetOrdinal("r.TICKET")));
                // Some process
            }
        }
    }

    // Write the ticket content to a file
    using(StreamWriter sw = new StreamWriter("ticket.txt"))
    {
        sw.WriteLine(ticket.ToString());
    }
}

【讨论】:

  • 这假设票证变量在每次循环迭代中累积数据。这个问题似乎另有说明。
【解决方案3】:

我将数据结构从 MDB 转换为 ACCDB 文件格式的经验是:

  1. 在创建具有完全相同的对象和数据的新 accdb 文件并对其进行压缩后,accdb 比旧 mdb 文件增加了近 40% 的文件大小。

  2. 速度较慢。使用 Query 和/或 VB 代码的数据处理需要至少 3 倍以上的时间才能到达终点。

  3. 达到 2Gb(数据库大小限制)比以前的格式达到 1GB 快。

我意识到,当您压缩 accdb 文件时,它首先会生成一个 mdb 文件,然后将其重命名为 accdb 扩展名。这真是太可怕了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2013-03-27
    • 2015-08-26
    • 2017-02-27
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多