Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

So I'm trying to find out what is faster db_query, db_select, or EntityFieldQuery. Currently I'm using EntityFieldQuery. I'm grabbing about 1600 node entries.

I realize this can be taxing on the system so I just want to figure out which is the best option to grab 1600 nodes. Shaving off seconds or even milliseconds would matter a lot with the application I'm building.

Thanks in advance for your answers.

share|improve this question
    
Have you profiled it? –  MPD 2 days ago
    
Not sure what you mean. Could you elaborate a little? –  geocalleo 2 days ago
    
What you should use depends on what you're trying to do exactly? If you can give more detail we might be able to help. If you are running a lot of queries, db_query() is the fastest. However, if you are loading entities with it (entity_load) than it probably doesn't matter, because entity_load will be slow when loading 1600+ entities. –  donutdan4114 2 days ago

2 Answers 2

up vote 4 down vote accepted

To answer your question in short, db_query is the fastest! Here are some reasons, facts and figure compiled from different questions, sources:

A simple googling of this question, come up with following results:

For simple queries, db_query() is 22% faster than db_select()
For simple queries, db_query() is 124% faster than EFQ
For queries with two joins, db_query() is 29% faster than db_select()

and this

db_query():

Total Incl. Wall Time (microsec):   796 microsecs
Total Incl. CPU (microsecs):    0 microsecs
Total Incl. MemUse (bytes): 123,352 bytes
Total Incl. PeakMemUse (bytes): 124,248 bytes
Number of Function Calls:   38

db_select()

Total Incl. Wall Time (microsec):   1,118 microsecs
Total Incl. CPU (microsecs):    0 microsecs
Total Incl. MemUse (bytes): 425,216 bytes
Total Incl. PeakMemUse (bytes): 436,392 bytes
Number of Function Calls:   88

If you notice above db_select makes more function calls and utilize more memory than db_query.

  1. See here for reasons on why to use db_select
  2. See here for reasons on why to use EntityFieldQuery over db_select
  3. See here for performance comparison of db_query and db_select

I guess the choice should be solely based on your requirements. EntityFieldQuery might be slower but, offers many advantages such as simple syntax, field storage is pluggable, loose coupling and many more.

share|improve this answer
    
This is exactly what I was looking for. Thanks a bunch Raf. –  geocalleo 2 days ago
    
@geocalleo glad it was helpful! don't forget to mark your question as resolved. –  Raf 2 days ago

That's a very bad idea there, for 1600 nodes, don't go around the APIs and use EntityFieldQuery . You are optimizing the wrong thing.

share|improve this answer
    
Hi Chx, could you elaborate. So bottom line, 1600 nodes need to be pulled. I'm already using EntityFieldQuery so just trying to understand what the bad idea would be. What I have found is that EntityFieldQuery is limiting in some areas. So far it's nothing that is affecting me. Anyway, looking forward to hearing your thoughts. –  geocalleo 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.