<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ADatalib_notes</id>
	<title>Development:Datalib notes - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ADatalib_notes"/>
	<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Datalib_notes&amp;action=history"/>
	<updated>2026-05-07T17:35:15Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Datalib_notes&amp;diff=11024&amp;oldid=prev</id>
		<title>Олег Давидович: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Datalib_notes&amp;diff=11024&amp;oldid=prev"/>
		<updated>2024-10-21T08:51:10Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 08:51, 21 октября 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Олег Давидович</name></author>
	</entry>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Datalib_notes&amp;diff=11023&amp;oldid=prev</id>
		<title>1&gt;Tsala: Datalib Notes moved to Development:Datalib notes</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Datalib_notes&amp;diff=11023&amp;oldid=prev"/>
		<updated>2007-03-18T12:05:46Z</updated>

		<summary type="html">&lt;p&gt;Datalib Notes moved to Development:Datalib notes&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== General Datalib usage Notes ==&lt;br /&gt;
&lt;br /&gt;
Couple of discussion threads about usage of datalib are [http://moodle.org/mod/forum/discuss.php?d=43456 here] and [http://moodle.org/mod/forum/discuss.php?d=38183 here].&lt;br /&gt;
&lt;br /&gt;
== Advanced Usage: get_recordset() ==&lt;br /&gt;
&lt;br /&gt;
There&amp;#039;s a new series of function calls in datalib that were introduced shortly after 1.5: get_recordset() and friends. It takes a bit of work to make good use of them, they are definitely useful.&lt;br /&gt;
&lt;br /&gt;
The function calls themselves take similar parameters to the get_records() family, and return an AdoDB recordset. In most situations, the right thing is to use get_records(), making sure that you are selecting a limited number of records (use LIMIT of course wink ), and that you are clearly fetching only the fields you need. That is enough for 99% of the cases. Stop reading now, use get_records(), and done.&lt;br /&gt;
&lt;br /&gt;
Easy!&lt;br /&gt;
&lt;br /&gt;
Now, there are some cases where we need to perform huge selects. And the downside of the get_records() machinery is that it allocates it all in RAM -- and if indeed records &amp;gt; than RAM, we are in trouble. This mostly affects batch processing in admin/cron.php (backups for instance) and the silly sync scripts we have in some auth and enrolment plugins.&lt;br /&gt;
&lt;br /&gt;
In those cases, where you have no option but to SELECT all the users, or all the courses, get_recordset() really shines. As you get an AdoDB recordset, you can iterate through it very fast, and with very low memory usage. I have prepared and profiled some samples and found that to keep that advantage you have to be very careful how you use it.&lt;br /&gt;
&lt;br /&gt;
I started testing with a large mdl_user table with 46K users, stored in PostgreSQL locally (socket connection). Here is a rundown of the code and rough timings...&lt;br /&gt;
&lt;br /&gt;
The traditional get_records() is fast (~14s), but it consumes 296MB of memory. It looks like:&lt;br /&gt;
&lt;br /&gt;
  $users = get_records(&amp;#039;user&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
So the next thing was to check that just triggering the SELECT was fast, and that fetching and re-shaping all that data into memory was an expensive step. get_recordset() in itself never fetches the data into PHP-space, so it takes only 4.8MB and 1.4s:&lt;br /&gt;
&lt;br /&gt;
  $users = get_recordset(&amp;#039;user&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
Next I tried using get_recordset() with FetchNextObj() which looked quite convenient, because it will yield each record in an object, similar to the objects that get_records() returns inside the array. The memory usage stayed the same (4.8M), but this is very slow (~65s):&lt;br /&gt;
&lt;br /&gt;
  $users = get_recordset(&amp;#039;user&amp;#039;);&lt;br /&gt;
  while($v = $users-&amp;gt;FetchNextObj()){&lt;br /&gt;
      // do something &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Internally, get_records() is using GetAssoc(), so I looked in there to se faster ways of getting at the data. Apparently, the trick is in reading the $rs-&amp;gt;fields array and using MoveNext(). So this loop still takes 4.8MB and walks the 46K user records in 4s:&lt;br /&gt;
&lt;br /&gt;
  $users = get_recordset(&amp;#039;user&amp;#039;);&lt;br /&gt;
  while(!$users-&amp;gt;EOF) {&lt;br /&gt;
      $user = $users-&amp;gt;fields;&lt;br /&gt;
      // do something&lt;br /&gt;
      $users-&amp;gt;MoveNext();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Naturally, as soon as you start doing useful things, this will take longer to exec. The core advantage, however, is that it can give you comparable performance to get_records() but with very low memory usage.&lt;br /&gt;
&lt;br /&gt;
There is an interesting [http://moodle.org/mod/forum/discuss.php?d=45794 discussion about get_recordset() in the General Developer Forum].&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>1&gt;Tsala</name></author>
	</entry>
</feed>