gpt4 book ai didi

sql - Postgres SELECT * 来自大表

转载 作者:行者123 更新时间:2023-11-29 14:27:54 24 4
gpt4 key购买 nike

使用包含大记录的 Postgres 表,将近 700 万。我知道 SELECT * FROM table 足够大以适合内存,所以数据库连接在等待查询结果的长时间延迟后丢失(只能执行 SELECT * FROM table LIMIT n).

我需要按顺序处理每条记录,直到最后一条。这样做的方法是什么?

最佳答案

您可以获得 n 条记录的 block 。

select * from table where tableId>currentTableId and tableId<currentTableId*chunk_size

解释:

假设您有 100 条记录,并且您的内存一次包含 10 条记录。

您从您的应用程序(任何 ORM)查询执行 10 次

 int totalRecords = executeQuery('select count(*) from table');
int chunkSize = 10;
int chunks = totalRecords/chunkSize ;
while(chuks>0){
executeQuery('select * from table where tableId>currentTableId and tableId<currentTableId*chunkSize ');
chunk-=chunkSize;
}

这段代码非常抽象。这只是给你的提示。

关于sql - Postgres SELECT * 来自大表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com