|All reviews||Solving The N+1 Problem In PHP||Latest reviews||Best sellers ranking|
Solving The N+1 Problem In PHP
Paul M. Jones
March 18, 2015
Week: Not ranked All time: 389
samshal.github.ioWhen it comes to Object Relational Mapping (ORM) or query optimization in a database system, the N + 1 problem, or as it is usually called SELECT (N + 1) problem, is a great challenge.
In particular when it involves database systems supported by PHP, such as MySQL, PostgreSQL, among others, you will hardly find enough material explaining what is the N + 1 problem and how to solve the issues with it that you may have in your database applications.
Paul M. Jones, addresses this problem in this book "Solving the N + 1 Problem in PHP". He explains the N + 1 problem very elegantly, using different examples and various real world cases to illustrate every concept that is discussed, and provides more than just one solution to the issue of an N + 1 problem occurring in an application, due to improper query formulation for instance.
Chapter one, which is the introduction to the book, contains a real world scenario of how the N + 1 problem might occur, and the various deficiencies that we may have in our applications due the problem.
One of these deficiencies is the problem of having a very slow code, which is obviously the first thing to be noticed about the N + 1 problem, as described in the book. He also gives other examples of the N + 1 problem in this chapter.
In chapter two, the author continues to build upon the foundation laid out in chapter one. Here he gives a more formal definition of the N + 1 problem, as well presents several examples and code snippets to illustrate the concepts discussed in this chapter.
In chapters three and four, the author explains how to diagnose and detect an N + 1 problem and the reasons why it may occur.
One of the most interesting things about chapter four is that the author gives us a better alternative to formulate queries. Instead of using CRUD interfaces (Create, Read, Update and Delete), he suggests using BREAD (Browse, Read, Edit, Add, Delete) instead. He presents very good reasons and also gives code examples as to how the BREAD alternative may be implemented for database queries.
Chapter five introduces different ways an N + 1 problem can be resolved. The author does this by solving real world examples. For instance, he explains how to handle a “One to Many Relationship”, a “One to One Relationship”, combining a “One to Many” relationship into a “One to One” relationship, and also how to convert multiple queries into a single query, all of which are examples of the N + 1 problem.
Chapters six and seven are my favorites because they were written to address problems of an application that was already launched and is in general use.
The author explains the concept of Query and Stitch, and code base refactoring as alternatives to rewriting a whole bunch of code just to remove the N + 1 problem.
The truth is, I have been finding both of these concepts a little bit too difficult to understand for a while, but Paul M. Jones really did a great job explaining it here.
The author answered some common questions in chapter eight concerning the ideas discussed in the book. The method with which the questions were asked and answered makes is really worthy to be used as a reference material for query design, optimization, and in general for case where the N + 1 problem needs to solved.
Recently I was reading about a recommendation request for a PHP package in the PHP Classes site that mentioned fast retrieval of information from a database that contains a table with about 48 gigabytes of data. This book is recommended for developers that face these kinds of challenges.
The N + 1 problem is not very well known among PHP developers or even among developers of database applications written in other languages. However, it is a problem that soon or later they will face, often without knowledge that this is a common problem with well studied solutions which are well explained in this book.
Therefore I recommend this book to all developers of database applications, written in PHP or not.
It is a concise book, that presents a well explained description of the SELECT (N + 1) problem and provides good solutions that involve writing well thought database queries and properly designing databases to avoid falling into the issues cause by the N + 1 problem.
No comments were submitted yet.