PDO结果迭代
我正在对 Oracle 服务器使用 PDO,并将查询结果拉入可迭代的集合类中。我这样做是因为许多结果集都非常大,如果我只使用 fetchAll()
,就会使用太多内存。我现在想要实现分页,但我不确定如何在不将集合迭代到所需记录并丢弃之前的所有内容的情况下执行此操作。这看起来效率非常低,而且整体设计很糟糕。有没有什么方法可以做我想做的事情,而不浪费地检索和丢弃不需要的记录?限制查询是唯一的方法吗?
I am using PDO against an Oracle server and pulling query results into a collection class that is iterable. I do this because many result sets are extremely large and would use too much memory if I just used fetchAll()
. I'm now wanting to implement pagination, but I'm not sure how to do this without iterating the collection to the desired record and discarding everything before it. This seems very inefficient and an overall bad design. Is there any way to do what I want to do without wastefully retrieving and discarding unwanted records? Is limiting the query the only way?
是的,您应该在此之前在 SQL 查询阶段实现分页。否则,没有什么比迭代和丢弃更好的了(这真的很可怕)。
Yes, you should implement pagination before that, at the SQL query stage. Otherwise there's nothing better than iterating and discarding (which is really horrible).