CodeIgniter Active Record vs. Explicit

When dealing with frameworks, such as Code Igniter, there is always a question of what type of overhead it adds to application. One rainy day not too long ago we ran some really simple tests on the difference between active record queries, and explicit queries in CI, and decided it might be nice to share the results. There is always the question of “is it faster”, and while our results show explicit SQL queries to be faster, database execution time is almost always negligible when compared to PHP run time, or page load speeds.

The Setup

We wanted to keep this test pretty bare-bones, so we could really nail down the overhead of just the active record processing. We loaded up a fresh copy of Code Igniter version 1.7.3, and created a typical controller-model relationship. Next, we created a simple timer using PHP’s microtime() function, and wrapped our calls in a for-loop, so it would repeat 1000 times. On each loop it logged the time difference of just the query itself, and at the end it exported to a .csv file so we could chart it up.

GET Query Times

Average Times (s) Explicit Query: 0.000168 Active Record Query: 0.000309

INSERT Query Times

Average Times (s) Explicit Query: 0.000161 Active Record Query: 0.000259

UPDATE Query Times

Average Times (s) Explicit Query: 0.000172 Active Record Query: 0.000321

DELETE Query Times

Average Times (s) Explicit Query: 0.000172 Active Record Query: 0.000321

Conclusions

As you might see, and as you might expect, there is a “significant” overhead using CI’s active record; however, I put that in quotations because the time scale we are looking at is so darn small. When you are building applications that require 10e-5 precision on your database queries, well, you probably aren’t a general-purpose, curious developer.