本文介绍了ORM查询结果:数组VS结果句柄包裹在Iterator接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这里有一个为亲的:

Okay, here's one for the pro's:

有关一两年了,我一直都在我自己的PHP ORM / ActiveRecord的实现,我命名的

For a couple of years now, i've been working on my own PHP ORM/ActiveRecord implementation that i named Pork.dbObject.

它是松耦合基于使自己的站点在5分钟内轨的电影,我们都看到了几年前。你可以做这样的事情:

It's loosly based on the 'make your own site with rails in 5 minutes' movie we all saw a couple of years ago. You can do things like:

$clients = dbObject::Search("Client", array("ID > 500"));

$client = new Client(218); // fetch row with id 218 from client table

$projects = $client->Find('Project');

这将获取一行或多行从数据库中,包在DBOBJECT,并在一个阵列中返回它们,或返回假的没有任何结果。

This will fetch one or more rows from the database, wrap them in a dbObject and return them in one array, or return false of there are no results.

这一切都已经可以正常使用数十个站点和后端的,但现在我的同事用它来创建一个巨大的LOGPARSER这里开始的内存使用量的问题。

All of this has been working perfectly in dozens of sites and backends, but now my colleague is using it to create a huge logparser and here starts the memory usage problems..

他上运行的查询可以返回了20.000行,甚至更多,这是ofcourse不是一件非常好的事情来包装成一个对象包装全部一次,并返回作为一个单一的阵列。

The queries he runs can return over 20.000 rows, maybe even more, which is ofcourse not a very good thing to wrap into an object wrapper all at once and return as a single array.

显而易见的解决办法是返回对象实现迭代接口而不是阵列。从结果应该不会立即提取所有的记录,但只保存结果资源生成的数据库查询,并使用mysql_fetch_ *在内部,当你遍历对象,如果它是一个数组。

The obvious solution would be to return an object that implements the Iterator interface instead of an array. It shouldn't instantly fetch all the records from the resultset, but just hold the result resource for the generated database query and use mysql_fetch_* internally when you traverse the object as if it was an array.

现在我们得到我的真正的问题:我可以,没有任何问题,只是这样做?是数据库能够处理多个打开的结果集,混匀,并保存在内存中一段时间​​?

Now we get to my real question: Can I, without any problems just do this? Are databases able to handle multiple open resultsets, and mix them and keep them in memory for a while?

例如,取20个对象,循环他们,让每种20取5人,至极反过来他们也取3人。这会创建一个循环,其中许多不同的结果手柄将被保存在存储器中。

For example, fetch 20 objects, loop them, let each of these 20 fetch 5 others, wich in their turn also fetch 3 others. This would create a loop where a number of different result handles will be kept in memory.

我知道我不能序列化这些对象之一,但我将能够实现这一点没有PHP5中的任何问题,或将数据库接口给我的问题?

I know i can't serialize one of these objects, but will i be able to implement this without any problems in PHP5, or will database interfaces give me problems?

推荐答案

这取决于你使用,你的数据库配置的数据库。

It depends on which database you're using and your database configuration.

对于MySQL,你需要确保你使用的缓冲的查询。在PDO将其设置是这样的:

For MySQL you need to make sure you use buffered queries. In PDO you set it like this:

$myPdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

这意味着,所有的数据将被发送到客户端(不一样取这一切在PHP)。

This means that all the data will be sent to the client (not the same as fetching it all in PHP).

其他(可能更差)的替代方法是当你发现一个查询中运行一个结果集仍然打开,打开一个新的数据库连接。

The other (probably worse) alternative is to open a new database connection whenever you detect a query being run with a result set still open.

正常 的mysql_query() 使用缓冲查询,以便将与多个结果集。

The normal mysql_query() uses a buffered query, so that will work with multiple result sets.

这篇关于ORM查询结果:数组VS结果句柄包裹在Iterator接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 13:29