4

Is there any performance difference between db_select and EntityFieldQuery?

I would like to re-write portions of the site removing db_select in favour of using EntityFieldQuery.

I hope that i will able to replace few db_select calls with one EntityFieldQuery call, also.

  • 1
    You should also look at drupal.stackexchange.com/questions/12515/… . I would also prefer entity field query as I do not have to worry about how the data is stored and as far as performance goes I believe EFQ is comparatively a bit expensive as it fetches the whole entity item instead of certain fields. But I would not count it to me more than a few ms. – junedkazi Sep 26 '12 at 15:42
  • 1
    Mind that EFQ doesn't fetch the whole entity, at minimum, it fetches an array of the IDs. This means you'll need to load the entities separately, this means that entity_load can fetch your entities from the cache (see the reset parameter). – Bart Sep 26 '12 at 15:54
  • Thanks guys, i used EFQ in my later scripts, but this one is a bit older, solid bud a bit complex structure. I thought to upgrade script and migrate it to EFQ logic. – NenadP Sep 26 '12 at 17:25
7

There might be a slight difference in performance but I guess it depends on your implementation. As you said yourself: it might be possible to replace a couple of db_select queries with EntityFieldQuery, although I can't really think of a situation where you'd be able to do that, if your db_selects are solid.

If I'm not mistaken EntityFieldQuery actually uses db_select or at least a SelectQuery to fetch data. I also think EntityFieldQuery looks up information about the entity that needs to be queried (ie: base table), this is cached though.

My guess is EntityFieldQuery would be somewhat slower, but the only way to see if you're losing any performance is by benchmarking (timing) it.

All in all I wouldn't worry about the performance of EntityFieldQuery. The biggest advantage of EntityFieldQuery is that it adds another layer of abstraction, making your code more loosely coupled and more cohesive. It's also pluggable so somebody could change the backend without you having to change any of your code.

  • Reason why i am thinking to replace couple of db_select with EFQ is because i can achieve same result with less code (accompanied with db_selects are few nasty loops). Achieving with less code means that i could extend the script more easily and it could became more maintenance-friendly. Thanks for elaboration! I will se what is the cost of rewriting the code vs imporving currend db_query structures! – NenadP Sep 26 '12 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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