Blog

2007 10 posts (4)

Ordering: Ascending Descending

1. PHP 5 tips: DateTime class

2007-10-07 11:56:01 by Martynas Jusevičius

PHP 5.2 introduced new DateTime and DateTimeZone classes, which obsolete the datetime functions. They offer a convenient way to store dates and time as objects, instead of strings or integers. Classes also have a better time zone support as well as support for dates outside of the Unix epoch.
But the API, while useful, is rather over-simplified and under-documentented. Here is a good introduction to the API with lots of examples: Looking at PHP5’s DateTime and DateTimeZone by Laughing Meme.

Add a comment Comments (2)

2. DIY Framework v0.3 released

2007-10-12 11:08:01 by Martynas Jusevičius

We are pleased to finally announce the release of the new version 0.3 of our DIY Framework. As promised, it includes new features and solves several issues:

To demonstrate features of the framework, we also release version 0.1 of DIY Blog, an open-source blog/content management engine, on which this website is running, too.

For easier distribution, we have moved our code to SourceForge.

So here come the links:

DIY Framework 0.3

DIY Blog 0.1

Happy hacking! Please provide feedback and spread the word :)

Add a comment Comments (256)

3. Eager fetching and SELECT N+1 problem

2007-10-22 23:33:57 by Martynas Jusevičius

After starting using an ORM and enjoying the benefits it brings, you might soon run into so-called SELECT N+1 problem if you don't use it properly, probably even without noticing it.

SELECT N+1 occurs when you retrieve a collection of objects from database via ORM, and then iterate it accessing the object properties of collection members. For example (we will use Propel):

$posts = PostPeer::doSelect(new Criteria());

foreach ($posts as $post)
{
	$author = $post->getAuthor();
	print $author->getName(); // do something with $author
}

Here we have Post and Author as classes, and each Post has it's own Author (this of course has to be specified as a foreign key in the database schema).
Now, when you access the author of every post in a loop, Propel loads it from the database with a separate SELECT query. And if there were N posts, you get a total of N such queries, plus 1 to load the posts in the first place — thus SELECT N+1.

While such code would work, it is far from optimal, since there is no need to SELECT from database in a loop to get authors of every post. Frequent database queries is one of the biggest hits on backend performance, and SELECT N+1 is causing it but often overlooked. If you are not sure if you have run into this problem, it is easiest to check your database query logs for excessive amounts of alike-looking SELECTs.

The problem is solved using eager fetching, which means that main and related objects are loaded in one go. In other words, when posts are loaded, author for each of them has to be instantiated at the same time, and not later when it is accessed.
The solution is implemented using JOINs and SELECTing data from related tables into one dataset. Processing it into objects (or “populating” objects, in Propel terms) needs little more memory and logic, but that is negligible compared to the overall performance gain. With Propel, it is achieved simply by specifying a different table-specific peer method for loading posts:

$posts = PostPeer::doSelectJoinAuthor(new Criteria());

Add a comment Comments (33)

4. Ohloh, the open source network

2007-10-30 16:19:50 by Martynas Jusevičius

What a great concept has been developed by the guys at Ohloh. It is an open source network that connects people through the software they create and use. If you like developing software and are into social networking as well, this sounds just perfect.
At Ohloh you can find and browse various open-source projects and see who developed them, how they evolved through time, and how much they are estimated to be worth. Ohloh even connects to the CVS/SVN repositories and figures out what languages have been used and who authored what code, line by line! As well as dozens of different kinds of statistics.

We registered our projects as well. They don't look very impressive yet though :)

DIY Framework at Ohloh

DIY Blog at Ohloh

Add a comment

Ordering: Ascending Descending