We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

"Lean" find results for better performance

Mongoose API has a method to remove those save, getters and setters from find() results called lean(). That improves the performance of find() when using large data. Does Phalcon have an equivalent method? Or a better way to retrieve a huge amount of rows from the database?

This is their doc: https://mongoosejs.com/docs/api.html#query_Query-lean

Edit: It seems this forum replaces the link and it doesn't anchor to the correct topic. Just search for "lean(" and it will get there.

edited Aug '16

Doesn't that still build the full object and then convert it to a data array?

Edit: I'll start making some benchmarks now...

It's possible. In ORM you can select columns which you want - then it's building partial class(Row). But don't know about ODM.

Just tested, using Gadb::find()->toArray() doesn't stop creating the object with save, getters and setters, etc... Execution went from 0.10 to 0.15 microseconds on a 31280 row database. Funny that I tried using $this->db->fetchAll("SELECT * FROM gadb") and that took 0.17. How can this be slower?

edited Aug '16

Oh then you mean just mysql ? Then try selecting columns which you only need, not all. I don't know how can be slowe, maybe just in one case it took 0.17. Phalcon will always return objects obviously - this is how ORM works.

Also toArray() methods is faster than iterating over each object. It you will iterate over them, do something for displaying or anything - it will be much slower than working on result from toArray method.

This is for an API. The idea is to get all airports from that database and send it as a JSON response. There will be no other interaction with the result, just a conversion to JSON. And that's the reason I wanted a method to skip the creation of those extra methods and return a plain object instead of the Phalcon object, just like mongoose.

And no, it wasn't only one case where it took 0.17. I'm really confused.

Then you need to use toArray() anyway. Gadb::find() will return resultset. toArray() is exactly what you need to do. Any further performance gains can be done by selecting columns which you only need(you really need to return all of columns ? i doubt it) or using cache(eveny mysql built-in result cache if you not using it yet, on any insert/update it just invalidate all keys.