
ORM won't kill any kittens
Last Friday of August I presented a session on the monthly meetup of my local user group Mexicali Open Source
The session name was "ORM won't kill any kittens". It was a few recomentations about how to use an ORM Doctrine in this particular case, since the demo was coded on Symfony2.
This was the topic list:
- The Symfony WebProfiler
- Write the DQL statements
- Lazy Associations
- Avoid Object Hydration and use Array Hydration instead
- Avoid fetching all of the properties
- Avoid loading entities use a reference instead
- Batch Processing
- Write DQL for Updates
- The N+1 Problem
The final point "The N+1 Problem" was the main reason for this session, since a few sessions back another member of the comunity was presenting a demo about a web application and looking at the quantity of executed queries, I mentioned this seems to be the N+1 Problem but what is this.
This issue appear when you need to load the children of an entity-relation. The problem is one query is executed for the parent record and one query of each child record.
As you may expect instead of one single query a lot of queries are executed and that is something you should try to avoid.
Along with the presentation I did a demo of how to solve the "N+1 Problem" writing DQL statements and adding some code on the Controller.
This is the number of the pre-loaded records and the relation between the entities on the demo:
250 Articles
10 Categories (Many to One)
100 Tags (Many to Many)
Without writing any DQL/Query and using default methods provided by Doctrine to load the list of Article entities from the DB.
$articles = $entityManager->getRepository('ORMDemoBundle:Article')->findAll();
Twig template
{% for article in articles %}
{{ article.title }}
{{ article.category.name }}
{% for tag in article.tags %}
{{ tag.name }} {%- if loop.last == false -%}, {%- endif -%}
{% endfor %}
{% endfor %}
Default behaviour: 511 DB Queries execution time 165.19 ms, 261 removing fetch="EXTRA_LAZY" and setting "EAGER" on Article association mapping Tag.
I will show the steps I used to approach this problem and reduce the number of executed queries to only 2.
Categories (Many to One)
The first improvement is create a DQL/Query and use LEFT JOIN on the relation between Article and Category.
public function findByArticleCategory(){
$queryBuilder = $this->getQueryBuilder();
$query = $queryBuilder->select('a.id, a.title, a.body, c.name as category_name')
->from('ORMDemoBundle:Article','a')
->leftJoin('a.category', 'c')
->getQuery();
return $query->getArrayResult();
}
Runnable SQL generated from Doctrine.
SELECT a0_.id AS id0, a0_.title AS title1,
a0_.body AS body2, c1_.name AS name3
FROM article a0_ LEFT JOIN category c1_ ON a0_.category_id = c1_.id;
As simple as this looks it remove the need to execute 10 DB queries when executing the SQL statement.
//Obtain the list of Articles
$articles = $entityManager->getRepository('ORMDemoBundle:Article')
->findByArticleCategory();
Tags (Many to Many)
As you can imagine the following improvement is also related to creating a DQQuery, but first extract all of the Article's ids obtained on the previous query result, the array_column function was used for that purpose.
// Extract the values from the id column on the articles array
$articleKeys = array_column($articles, 'id');
This array of Article ids it's be pass as an argument to the next DQL/Query in this case using the relation between Tags & Articles to implement a LEFT JOIN on Articles and then filter the id column with the passed id values.
public function findTagsByArticle($articleKeys){
$queryBuilder = $this->getQueryBuilder();
$query = $queryBuilder->select('article.id as article_id,
tag.id as tag_id, tag.name as tag_name')
->from('ORMDemoBundle:Tag','tag')
->leftJoin('tag.articles', 'article', 'ON')
->where('article.id IN (:articleKeys)')
->setParameter('articleIds', $articleKeys)
->getQuery();
return $query->getArrayResult();
}
Runnable SQL generated from Doctrine
SELECT a0_.id AS id0, t1_.id AS id1, t1_.name AS name2
FROM tag t1_
LEFT JOIN articles_tags a2_ ON t1_.id = a2_.tag_id
LEFT JOIN article a0_ ON a0_.id = a2_.article_id
WHERE a0_.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114,
115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226,
227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250);
Executing that will return an array of the matching relation between Tags & Articles.
$articles_tags = $entityManager->getRepository('ORMDemoBundle:Article')
->findTagsByArticle($articleKeys);
Next use foreach to iterate the result and create a new array and concatenate all of the matching Tags by Article id.
$tags = [];
foreach ($articles_tags as $article_tag){
$article_id = $article_tag['article_id'];
if (array_key_exists($article_id, $tags)) {
$tags[$article_id] = $tags[$article_id] . ', ' . $article_tag['tag_name'];
}
else {
$tags[$article_id] = $article_tag['tag_name'];
}
}
And finally the two arrays $articles & $tags are passed from the controller to the Twig file and use the for loop to display the results
{% for article in articles %}
{{ article.title }}
{{ article.category_name }}
{{ tags[article.id] }}
{% endfor %}
Optimized behaviour: 2 DB Queries execution time 6.23 ms.
You can see the slides I used on the presentation here:
http://jmolivas.com/slides/mxlos/orm-wont-kill-any-kittens/
You can also take a look at the code used for the demo on this github repository:
https://github.com/jmolivas/orm.dev