php postgres从查询到理论上获取行 [英] php postgres from query to fetching rows in theory

查看:49
本文介绍了php postgres从查询到理论上获取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道到底发生了什么,因为php脚本运行查询直到数据库返回数据,而php脚本开始获取它们.

I would like to know what precisely is happening since php script runs the query to the moment when database returns data and php script starts to fetch them.

从理论上讲,我有一个具有巨大表的Postgre数据库,它谈论带有20多个列的10/20百万条记录,并且我有一个php脚本,该脚本要求将数据库中的所有行都放置在某个文件中

In theory, I have a Postgre database with huge table, talking about 10/20 million records with 20+ columns and I have a php script which is requesting all that rows from database to be placed in, lets say, some file.

我的知识是:

  • php脚本通过sql查询运行pg_query命令
  • 通过postgres php驱动程序/扩展名查询传递到数据库
  • 数据库完成工作并将结果返回给驱动程序
  • php postgres驱动程序将结果存储在服务器RAM内存中,并将资源ID返回给引用结果的php脚本
  • pg_fetch_row从RAM内存中读取记录

我正在尝试完成上面描述的工作的php代码的最佳优化.

I am trying to accomplish best possible optimization of php code which is doing described work above.

问题是:

  • 我的知识正确吗?如果缺少某些内容,或者我对某个步骤不正确,请纠正我.
  • 我如何知道为数据库结果分配了多少RAM内存?如果我的服务器内存不足,这可能很重要.
  • 我知道我可以获取单行,然后将其存储到文件中,并在有行的情况下重复执行这两个步骤,因此我可以最大限度地减少php脚本所需的内存,但是如何影响资源使用的内存呢?li>

谢谢.

推荐答案

PostgreSQL会(默认情况下)一次性返回所有行.这样做的好处是,可以在服务器端释放资源,但代价是客户端可能会有大量结果集.

PostgreSQL does (by default) return all rows in one go. This has the advantage of freeing resources on the server side at the cost of possibly large result sets at the client.

替代方法是使用游标,该游标一次可以返回单行.一些驱动程序直接支持此功能(也许 PDO ?),也可以使用 DECLARE FETCH

The alternative is to use a cursor which can return a single row at a time. Some drivers support this directly (maybe PDO?) or you can use DECLARE and FETCH

要注意的另一件事是PostgreSQL的 COPY 命令,可以直接转储表或查询文件(假设您不需要太多处理/格式化).检查您的datbase库是否提供直接访问.

The other thing to be aware of is PostgreSQL's COPY command which can dump a table or query to a file directly (assuming you don't need much processing/formatting). Check if your datbase library offers direct access.

这篇关于php postgres从查询到理论上获取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆