John Davidson

php - Is Doctrine Really That Slow (30times slower than PDO)?


I've been using Doctrine with Symfony for a while now and annoyed by how slow it is. I started looking into it a bit more and can't believe the speed differences.

I simple sql query run directly in PDO would take about 5ms

SELECT * FROM events WHERE id=1

The same query in Doctrine with hydration into an Object takes around 150ms. That's 30times slower.

$em = Application::getDoctrineEntityManager();
$event = $em->find(Event::class, 1);

My event object isn't even very large and I've set all relationships to fetch="EXTRA_LAZY" so there shouldn't be any problem with any extra work or extra queries.

Am I missing something? Clearly an extra 145ms for writing data from the database result into an object is unacceptably long.

My Answer:

I understand your frustration with the performance difference between using Doctrine and PDO. However, it's important to note that Doctrine is an ORM (Object-Relational Mapping) tool that provides a higher level of abstraction and convenience compared to using raw SQL queries with PDO.

The trade-off for this convenience is often a slight decrease in performance, as Doctrine needs to perform additional operations such as entity mapping and hydration. Additionally, Doctrine offers features such as lazy loading, caching, and query building that can further impact performance.

That being said, there are ways to optimize the performance of Doctrine, such as using query caching, optimizing database queries, and reducing the number of database calls. It's also worth considering whether the convenience and features provided by Doctrine outweigh the performance trade-off for your specific use case.

Ultimately, the decision to use Doctrine or PDO should be based on your project requirements and priorities. If performance is a critical factor and you are comfortable writing raw SQL queries, using PDO may be a better option for you.

Rate this post

3 of 5 based on 3446 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us