Excutereader通过oledbcommand对象获取数据时非常慢

发布于 2024-10-19 15:55:34 字数 1009 浏览 5 评论 0原文

我正在使用 oledbcommand 对象从 dBase4 数据库获取数据并将其加载到数据表中。但在 5-10 分钟左右获取 160 条记录需要花费太多时间。 请帮助我。

代码:

 using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + TrendFilePath + "\\" + Pathname + ";" + @"Extended Properties=dBASE III;"))

 using (OleDbCommand cm = cn.CreateCommand())
 {
    cn.Open();

    for (int L = 0; L <= months; L++)
     {
         DataTable dt_Dbf = new DataTable();
         From_Date = DateTime.ParseExact(frmdate, dateFormat2, provider);
         From_Date = From_Date.AddMonths(L);

         int month = From_Date.Month;
         string year = "1" + From_Date.Year.ToString().Substring(2, 2);

         if (L == 0)
         {

              cm.CommandText = @"SELECT * FROM  128.DBF where DATE_Y =" 
                               + year + " and DATE_M = " + month + " and DATE_D>=" + From_Day + ""; 
              dt_Dbf.Load(cm.ExecuteReader(CommandBehavior.CloseConnection));
         }
    }
}

I am fetching data from dBase4 database by using object of oledbcommand and load it into datatable. but it is taking too much time to fetch 160 records around 5-10 minutes.
Please Help me Out.

Code:

 using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + TrendFilePath + "\\" + Pathname + ";" + @"Extended Properties=dBASE III;"))

 using (OleDbCommand cm = cn.CreateCommand())
 {
    cn.Open();

    for (int L = 0; L <= months; L++)
     {
         DataTable dt_Dbf = new DataTable();
         From_Date = DateTime.ParseExact(frmdate, dateFormat2, provider);
         From_Date = From_Date.AddMonths(L);

         int month = From_Date.Month;
         string year = "1" + From_Date.Year.ToString().Substring(2, 2);

         if (L == 0)
         {

              cm.CommandText = @"SELECT * FROM  128.DBF where DATE_Y =" 
                               + year + " and DATE_M = " + month + " and DATE_D>=" + From_Day + ""; 
              dt_Dbf.Load(cm.ExecuteReader(CommandBehavior.CloseConnection));
         }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶气 2024-10-26 15:55:34

我怀疑您正在执行的查询是问题所在。除非查询正在过滤大量(数百万/数十亿)数据集,否则我不认为它有问题 - 即使您正在执行 12 个(我假设每个月?)这些查询。另外,在我写作时,您是否打算使用数据表?每个月,您都会填充表,然后将其丢弃。 (由于您提到数据库只有 ~300k,因此查询本身不可能占用任何大量时间)。

可能与网络或文件本身有关。正如我所说,您正在执行 12 个查询,但在每个查询成功后都会断开连接。这意味着您需要建立 12 个连接,并运行 12 个查询。您应该调查是否可以为需要执行的一组查询维护单个连接。

另一种可能性是锁定。数据库文件是否能够维护多个连接并为不同用户执行多个查询?我根本不了解 dbase 产品,但如果它是单用户/单线程,则可能是其他人正在使用该数据库并将您锁定一段时间。

尝试将数据库复制到本地计算机,更改连接字符串以引用本地副本,然后再次运行此代码。如果执行时间显着减少,我相信您可以自信地说存在某种网络问题。从那时起,您(可能)需要向组织中的某人寻求帮助。 5-10 分钟 12 个看似简单的查询简直是个笑话。

如果网络是问题的原因,您可以采取多种选择。

  • 请网络/基础设施人员进行调查。可能是共享上的磁盘,或者网络 IO 过载。
  • 在程序开始时将数据库复制到机器上。仅当您只进行读取并且不需要更新数据时,这才有效。或者,在每个查询开始时复制到本地,并且仅写入网络。这将维护(相对)新鲜的数据,并确保所有写入都是正确的。但是,如果您执行大量查询,复制整个文件的网络成本可能会降低性能。
  • 使用更好的数据库产品(我的推荐)。使用 Postgres 或 MySql(我建议使用 Postgres)设置数据库服务器,将 dbase 数据迁移到服务器,并使引用 dbase 文件的所有应用程序都指向数据库服务器。如果多个连接需要访问数据库,您确实应该这样做。如果只有一个连接需要访问数据库,那么数据库应该位于进行访问的机器上。

I doubt that the query you're executing is the problem. Unless the query is filtering on a massive (millions/billions) data set, I don't see a problem with it - even though you're executing 12 (I'm assuming for each month?) of these queries. Also, while I'm writing, were you planning on using the DataTable at all? For each month, you populate the table, then throw it away. (Since you mention the database is only ~300k, it's impossible that the queries themselves are taking up any significant time).

It probably has something to do with the network, or the file itself. As I said, you're executing 12 queries, but tearing down the connection as each one succeeds. This means that you need to establish 12 connections, along with running 12 queries. You should investigate whether or not you can maintain a single connection for the set of queries you need to execute.

Another possibility is locking. Is the database file capable of maintaining multiple connections and executing multiple queries for different users? I don't know the dbase product at all, but if it is single user / single threaded, it could be a case that other people are using the database and locking you out of it for a time.

Try copying the database to your local machine, changing the connection string to reference the local copy, and run this code again. If the execution time is significantly reduced, I believe you'll be able to confidently say that there is some kind of network issue. From that point, you will (probably) need to seek help from someone in your organisation. 5-10 minutes for 12 seemingly simple queries is a joke.

If the network is the cause of the issue, there are several options that you can take.

  • Ask the networking/infrastructure people to investigate. It may be disk on the share, or overloaded network IO.
  • Copy the database to the machine at the beginning of the program. This will only work if you are only doing reads, and you don't need updated data. Alternatively, copy to local on at the beginning of every query, and only write to the network. This will maintain (relatively) fresh data, and will ensure all writes are correct. However, if you do a lot of queries, the network cost of copying the entire file over will probably have worse performance.
  • Use a better database product (my recommendation). Set up a database server, using Postgres or MySql (I advise Postgres), migrate the dbase data to the server, and have all your applications that reference the dbase file point to the database server. You really should be doing this if more than one connection needs to access a database. If only one connection needs to access the database, then the database should be on the machine doing the accessing.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文