简体   繁体   English

从数据库中检索顺序数据的最快方法是什么?

[英]What is the fastest way to retrieve sequential data from database?

I have a lot of rows in a database and it must be processed, but I can't retrieve all the data to the memory due to memory limitations.我在数据库中有很多行,必须对其进行处理,但由于 memory 的限制,我无法将所有数据检索到 memory。

At the moment, I using LIMIT and OFFSET to retrieve the data to get the data in some especified interval.目前,我使用 LIMIT 和 OFFSET 来检索数据以在某个指定的时间间隔内获取数据。

I want to know if the is the faster way or have another method to getting all the data from a table in database.我想知道这是更快的方法还是有另一种方法来从数据库中的表中获取所有数据。 None filter will be aplied, all the rows will be processed.将不应用任何过滤器,将处理所有行。

SELECT * FROM table ORDER BY column

There's no reason to be sucking the entire table in to RAM.没有理由将整个表吸入 RAM。 Simply open a cursor and start reading.只需打开 cursor 并开始阅读。 You can play games with fetch sizes and what not, but the DB will happily keep its place while you process your rows.您可以玩具有 fetch 大小的游戏等等,但是当您处理行时,DB 会很高兴地保留它的位置。

Addenda:附加物:

Ok, if you're using Java then I have a good idea what your problem is.好的,如果您使用的是 Java,那么我很清楚您的问题是什么。

First, just by using Java, you're using a cursor.首先,只需使用 Java,您就可以使用 cursor。 That's basically what a ResultSet is in Java.这基本上就是 Java 中的 ResultSet。 Some ResultSets are more flexible than others, but 99% of them are simple, forward only ResultSets that you call 'next' upon to get each row.一些结果集比其他结果集更灵活,但其中 99% 很简单,只转发您调用“下一个”以获取每一行的结果集。

Now as to your problem.现在关于你的问题。

The problem is specifically with the Postgres JDBC driver.问题出在 Postgres JDBC 驱动程序上。 I don't know why they do this, perhaps it's spec, perhaps it's something else, but regardless, Postgres has the curious characteristic that if your Connection has autoCommit set to true, then Postgres decides to suck in the entire result set on either the execute method or the first next method.我不知道他们为什么这样做,也许是规范,也许是别的东西,但无论如何,Postgres 有一个奇怪的特性,如果你的 Connection 将 autoCommit 设置为 true,那么 Postgres 决定在任一execute 方法或第一个 next 方法。 Not really important as to where, only that if you have a gazillion rows, you get a nice OOM exception.至于在哪里并不重要,只有当你有无数行时,你会得到一个很好的 OOM 异常。 Not helpful.没有帮助。

This can easily be exactly what you're seeing, and I appreciate how it can be quite frustrating and confusing.这很容易就是你所看到的,我很欣赏它是多么令人沮丧和困惑。

Most Connection default to autoCommit = true.大多数连接默认为 autoCommit = true。 Instead, simply set autoCommit to false.相反,只需将 autoCommit 设置为 false。

Connection con = ...get Connection...
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("SELECT * FROM table ORDER BY columm");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
    String col1 = rs.getString(1);
    ...and away you go here...
}
rs.close();
ps.close();
con.close();

Note the distinct lack of exception handling, left as an exercise for the reader.请注意明显缺乏异常处理,留给读者练习。

If you want more control over how many rows are fetched at a time into memory, you can use:如果您想更好地控制一次将多少行提取到 memory 中,您可以使用:

ps.setFetchSize(numberOfRowsToFetch);

Playing around with that might improve your performance.玩弄它可能会提高你的表现。

Make sure you have an appropriate index on the column you use in the ORDER BY if you care about sequencing at all.如果您完全关心排序,请确保您在 ORDER BY 中使用的列上有适当的索引。

Since its clear your using Java based on your comments:既然它根据您的评论清除了您使用 Java :

If you are using JDBC you will want to use: http://download.oracle.com/javase/1.5.0/docs/api/java/sql/ResultSet.html If you are using JDBC you will want to use: http://download.oracle.com/javase/1.5.0/docs/api/java/sql/ResultSet.html

If you are using Hibernate it gets trickier: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html If you are using Hibernate it gets trickier: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从String检索值的最快方法是什么? - What is the fastest way to retrieve values from String? 从在线托管的mysql数据库中检索数据的最快方法是什么? - What is the fastest way of retrieving data from mysql database hosted online? 从API检索数据的最快方法 - Fastest way to retrieve data from API 从ArrayList过滤数据的最快方法是什么? - What is the fastest way to filter data from ArrayList? 从Firestore获取数据的最快方法是什么? - What is the fastest way to get data from Firestore? 什么是从数据库存储和检索数据的最佳方法 - What is the best way to store and retrieve data from database 什么是从Oracle数据库获取大量数据到Java对象的最快方法 - what's the fastest way to get a large volume of data from an Oracle database into Java objects 将数据从应用程序(Java)导入临时表的最快方法是什么? - What is the fastest way to import data from application (Java) into temporary table? 将 memory 中的大量数据写入文件的最快方法是什么? - What is the fastest way to write a large amount of data from memory to a file? 从Java应用程序获取数据到Cassandra 2的最快方法是什么? - What is the fastest way to get data into Cassandra 2 from a Java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM