Easy Setter Injection in Zend Framework 2
Want to learn how to have initialized objects with minimal code and nearly no hands-on configuration in Zend Framework 2? Come learn about setter injection.
Here’s 3 simple ideas for improving Zend Framework 2 application performance without learning more code. Think Database
Today, I want to take you out left field a bit. I want to take you a little away from the everyday, potentially clichéd, advice you likely read when it comes to improving Zend Framework 2 application performance.
There’s likely nothing wrong with it; but I’d say you’ve already read it many times. Instead, I’ll show you 3 strategies you may not have thought of - specifically focused around the database. That way, when combined with the standard advice, you’ll be better able to improve performance of your Zend Framework application.
From experience, database skills are all to often overlooked. I’ve met and worked with a number of developers over the years, and taken over a number of code bases. Whilst very capable, with years of experience, commonly the database appeared to be an afterthought. Sometimes it was just a data dump.
Whilst PHP applications have been almost wedded to the hip of MySQL, since the language’s inception, developers don’t seem to have the database knowledge they should - whether that’s with DDL (Schemas) or DML (Queries).
Note: I simplified my definition of DDL & DML intentionally.
I’ve seen applications created which (re)implemented so much functionality already existing in the supporting database - functionality developed by a team of developers, with accompanying tests and support. Why? I’ve seen applications which do so much processing in PHP, which would be executed much faster in the SQL query or a stored procedure.
I’m not sure if the developers in question weren’t aware of the functionality, or perhaps were unmotivated enough to learn. Either way, they made a lot of work for themselves. So firstly, please stop reimplementing existing functionality. And don’t perform unecessary processing in PHP.
Not sure whether you can or not? Does that function exist? Spend time learning about your database with the vendor supplied documentation. The time will be well spent and you’ll do your career a favour by up-skilling. Links to the major database vendors are available at the end.
Unless you’re Facebook, Google, or a startup, this is likely a non-argument, or an excuse for laziness.
Note: If it’s your manager saying it, email me and I’ll help you break down the resistance. There’s a few good ways to show the benefits.
It’s more than likely, per/project at least, you’ll only use one database vendor. From experience, talk of migration to another vendor is likely smoke and mirrors; at least when the true cost of that process is understood.
So get in and start learning all about your vendor. Learn to do ever more, especially the advanced stuff. Draw out every last bit of functionality you can, cut down re-implementing functionality, and watch your app fly.
For some simple examples, try these:
Can you answer the following questions?
You don’t need to know all of them, but it’s handy if you do. They’re all features which can reduce your codebase, time and cost investment. Depending on your application, these will be more or less effective.
Note: The links above are taken from across the majority of the major database vendors. If it’s not your vendor of choice, the respective documentation will be not too far away.
This really goes hand in hand with points 1 & 2. The closer the logic is to the underlying data store, the faster it will be. Are you familiar with triggers and stored procedures? If not the Microsoft definition.aspx) is:
A stored procedure is a group of Transact-SQL statements compiled into a single execution plan.
These are written and stored within the database itself, often accounting for large performance increases. Why? One of the biggest bottlenecks is removed, network overhead.
Whilst networks have grown in speed exponentially over the decades, network overhead’s still one of the slowest areas. If you can eliminate it, it would be worth your while.
However, as MySQL’s own documentation points out:
this does increase the load on the database server because more of the work is done on the server side and less is done on the client (application) side
But it can be worth your while to invest time in approaching performance optimization this way. With stored procedures, queries don’t need to be sent to the server and processed before result sets are returned, as they’re stored, analysed and precompiled on the server. So you’ve removed some PHP code analysis and compilation, some network overhead and SQL analysis and compilation on the server.
Some people feel uncomfortable with stored procedures however. And some of the drawbacks are covered in this recent article on SitePoint - as well as a good introduction to them as well.
Two of the key criticisms I’ve heard, are a lack of knowledge about how to develop them and versioning them. To combat a lack of knowledge, there is abundant, excellent, documentation both online and off.
For versioning, there are two excellent tools, Doctrine Migrations and DBDeploy. Both of these allow for managing queries in a versionable manner. I’m an avid user of Doctrine Migrations and recommend it highly. I don’t advocate stored procedures in every case, but they’re worth considering if your needs benefit from them.
I’m concerned I may have veered into a rant today. I mean neither to do that, nor to belittle. My intent is to encourage you in learning more about databases and to show you how, with a bit more knowledge, you don’t have to write a sceric more code - in fact you’ll likely write less.
What’s your database of choice, and what your your performance tips and tricks? Share in the comments.
If you want to know more about your database vendor of choice, here’s the links to the latest documentation. Is yours missing?
Want to learn how to have initialized objects with minimal code and nearly no hands-on configuration in Zend Framework 2? Come learn about setter injection.
Often times object hydration isn’t a simple matter, and requires a more nuanced, more sophisticated solution than the out-of-the-box options, available in Zend Framework, provide. Today we showcase one such solution - the Aggregate Hydrator.
In Zend Framework 2, so much has been changed, updated and improved - with Forms receiving a lot of attention. In this post, I show you a simple, flexible and powerful approach to compose and use them everywhere in your applications.
In today’s post, we look at one of the simplest and most effective components of the Zend Framework that allows us to create extendable and extensible Zend Framework applications - Zend Application Resource Plugins (combined with the Strategy Pattern). If you want to ensure your apps can grow without heartache, read on.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus