简体   繁体   中英

Performance wise; (single event listener) or (query) for firebase?

Is it better to retrieve a single entry using (addSingleEventListener) or (query) if I know the exact node path.

I have a "X" node that contains 10,000+ user ... each user has 60+ "Y" sub-nodes each containing a class of 3 "Z" nodes (10,000 X -> 60 Y -> 3 Z)

Now I continuously retrieve Z nodes using java code below .. but I'm facing slow performance & outOfMemory crashes on real android devices .. I was thinking maybe a "query.equalTo()" might be faster .. any ideas to improve performance

dbRootRef.child("X").child("Y").child("Z")
          .addListenerForSingleValueEvent(new ValueEventListener(){ ... });   

EDIT

Thank you all for your time, however I would like to clarify that:

  • I know that query and reading data through listeners are different processes but I need to retrieve data efficiently regardless of the name of the process.

  • My question is somehow general and what I was looking for was a general adapted way to retrieve data from database as it grows bigger.

When reading data from the Firebase Database, the majority of the time is going to go to the actual downloading of that data. Compared to that, the time it takes on the server to find that data is typically small.

This means that two approaches to retrieving the same data, are likely to give you similar performance. Given that, I'd go for the one that is most readable. Which, in the case that you know the exact path, is to use that exact path as you have in the code snippet you shared. That code snippet could be slightly shorter with:

dbRootRef.child("X/Y/Z")
      .addListenerForSingleValueEvent(new ValueEventListener(){ ... })

But this change won't have any noticeable performance difference either, since it's retrieving the same information again.


A bigger performance difference is whether you use addListenerForSingleValueEvent or addValueEventListener . If you use addListenerForSingleValueEvent on the same data multiple times, you are (likely, it depends on other factors) reading that data multiple times. If you then don't clean up the local references to that data carefully, you could easily end up with the same data in memory multiple times.

That's why it's recommended to use addValueEventListener if you have a prolonged need for the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM