简体   繁体   English

使用 PostgreSQL 分块 SELECT 查询

[英]SELECT query in chunks using PostgreSQL

I have a table with well over a million entries.我有一张表,里面有超过一百万个条目。 I'm putting all this data into perl and storing it into a variable ($query = "SELECT * FROM table1". The problem now is that I'm using a lot of resources (mostly memory). Given my novice state with perl and postgres, How would I transform that query into chunks or parts?我将所有这些数据放入 perl 并将其存储到一个变量中 ($query = "SELECT * FROM table1"。现在的问题是我使用了大量资源(主要是内存)。鉴于我对 perl 的新手状态和 postgres,我将如何将该查询转换为块或部分?

For example,例如,

$query = "SELECT * FROM table1 LIMIT 100000";

Would put 100K results into $query.将 100K 结果放入 $query。 table1 is 10M records in size. table1 的大小为 10M 记录。 How would I transform it so that only 100K results are stored into $query at a time until the query is finished?我将如何转换它以便在查询完成之前一次只将 100K 结果存储到 $query 中?

First of all do you really need all 10M records?首先,您真的需要所有 10M 记录吗? don't think so and thus retrieve only the records for your work to be done.不要这么认为,因此只检索您要完成的工作的记录。 Second, do you really need all columns data?其次,你真的需要所有列数据吗? may not be and in such case select only the columns needed saying select col1, col2, col3 ... from table instead of doing a select *... .可能不是,在这种情况下,只选择需要的列,说select col1, col2, col3 ... from table而不是执行select *... Cause, there is no point in getting all data and filling your server's memory.因为,获取所有数据并填满服务器的内存是没有意义的。

In worst case, if what you have said is what you actually in need then you have no other way than retrieving all records.在最坏的情况下,如果您所说的正是您真正需要的,那么您除了检索所有记录之外别无他法。 You can probably implement paging to get data in batches rather than getting all of them at once.您可能可以实现分页以批量获取数据,而不是一次获取所有数据。

Pagination?分页?

Maybe this might be of use: https://www.postgresql.org/docs/8.3/static/queries-limit.html也许这可能有用: https : //www.postgresql.org/docs/8.3/static/queries-limit.html

Sorry if I have misunderstood.对不起,如果我误解了。

Trick :诡计 :

you can use modulus for chunk data您可以对块数据使用模数

eg :例如:

you want chunk data from 1000 record, chunk data 4 pieces using seq id you just modulus seq id data 4,您想要来自 1000 条记录的块数据,使用 seq id 的块数据 4 块,您只需对 seq id 数据 4 进行模数,

SELECT *
  FROM input_data_control b
 WHERE  b.bill_schedule_month = '201910' 
        AND mod(input_data_control_id, 4) = [1...4]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM