CodeIgniter Forums

Full Version: Active record class VS simple query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]saidai jagan[/eluser]
Hai,
Any body tell which is fast.
Using active record class
$this->db->get('my table');
OR
Using query method ,
$this->db->query('select * from table');

El Forum

[eluser]n0xie[/eluser]
Obviously doing a query directly is faster since the Active Record class first has to 'build' the query.

El Forum

[eluser]saidai jagan[/eluser]
Yeah thanx man. That was i wanted. Smile

El Forum

[eluser]Zeeshan Rasool[/eluser]
informative !

El Forum

[eluser]BrianDHall[/eluser]
$this->db->get('my_table') is faster, because it is less typing and its hard to screw up the syntax.

There is of course a big difference in execution time/resources and development speed Smile

Always the word of caution, but it is likely the difference in speed is on the order of fractions of a millisecond - avoid using query strings directly just because they are 'faster', for the same reason you use CI even though just using a pure php spaghetti-coded page would be 'faster'.

Put if you have profiled your application and determined that AR functions are a significant constraining factor to application performance and a pseudo-test has been done to confirm you aren't wasting your time, then you should in fact drop built-in database objects entirely where possible and rely on direct system-specific optimized query calls - it might buy you more than a handful of clockcycles that ->query() vs ->get() could ever buy you. Still seems like a bit of a local optimization, but YMMV.

El Forum

[eluser]saidai jagan[/eluser]
OH. FYI i checked both (get() and query()) methods in the output profiler system.It shows get() is much better than query() :-S .Any information ???

El Forum

[eluser]BrianDHall[/eluser]
[quote author="saidai jagan" date="1257259099"]OH. FYI i checked both (get() and query()) methods in the output profiler system.It shows get() is much better than query() :-S .Any information ???[/quote]

I ran some tests on my live server and tested three things:

1- ORM approach (create an object and use get).
2- db->query()
3- db->get()

I found get to be the fastest, query to be .002-.005 seconds slower, and the orm method to be .007 slower.

So the difference in the slowest and the fastest require about 35 queries to cause even a quarter of a second of difference in performance.

Seems kind of a minor difference, but dunno why get() actually runs ever so slightly faster.

For fun I tested $i++ vs ++$i, just cause, and it takes 100,000 uses to score .005 seconds of difference in performance. For those keeping track, that is 5,000,000 increments to be a quarter of a second of difference Smile

El Forum

[eluser]saidai jagan[/eluser]
Yeah i also tested in the live server.
The query() method running at 0.0465 secs.
but the get() runs at 0.0355 secs.
But i thought query() is faster now i get to kno get() is much much better for me.
Is this a valid point?

El Forum

[eluser]BrianDHall[/eluser]
[quote author="saidai jagan" date="1257415933"]Yeah i also tested in the live server.
The query() method running at 0.0465 secs.
but the get() runs at 0.0355 secs.
But i thought query() is faster now i get to kno get() is much much better for me.
Is this a valid point?[/quote]

Well, that's 0.011 seconds difference - if you run the query 10 times that is 1/10th of 1 second. Are you running an application where such a load actually matters, or is most of your servers capacity unused anyway? Is lag time running queries noticably slowing your applications response time?

If it is a constraint of the system, it matters - otherwise, it doesn't.

The principle of avoiding premature optimization is to code for maximum development speed, then only when performance becomes an issue do you go back and seek to optimize only if it makes any sense.

El Forum

[eluser]saidai jagan[/eluser]
Thanks fa u r reply !! Smile.
so shall i use query() rather than get()?.
Pages: 1 2