4

I have a Firebase with a child named lines. lines contains about 500k records, each with eight key/value pairs.

When my app loads, it tries to grab the last 128 records in lines using the following:

fb = new Firebase("https://myapp.firebaseio.com/lines");
fb.limitToLast(128).once("value", function(snapshot) {
  // do something
});

I'm finding that my app's initial load time is getting slower and slower. The only thing that's changing is how many records there are under lines. Should I expect limitToLast() to take increasingly longer as the number of records under lines goes up? If so, I can try and cull the records over time, but I was under the impression that the limitToLast() query would only grab what is specified.

mix
  • 6,943
  • 15
  • 61
  • 90

1 Answers1

6

Should I expect limitToLast() to take increasing longer as the number of records under lines goes up?

Yes. Keep reading for why that is.

If so, I can try and cull the records over time

That is indeed the recommended solution.

but I was under the impression that the limitToLast() query would only grab what is specified.

It will only send the children matching your query to the client. But to know which children to send it needs to consider all of them. So while limitToLast() can be used to reduce bandwidth, they don't reduce "disk IO" on the Firebase servers.

I put "disk IO" in quotes there, because there are many factors involved here. But in general: giving the database more nodes to consider, will make it slower. Or the opposite: if you give the database less work to do, it will be done with that work faster.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • >"But to know which children to send it needs to consider all of them." Frank, are you sure about that? I cannot see it into the docs. If it is true then wondering about limitToFirst+startAt? ..I think firebase should share more on what is the expect time/space/.. complexity of all types of index queries.. – bodrin May 09 '16 at 19:11
  • I agree if there was more documentation of the performance of the different types of queries. But I am also sure about the statement you're asking about: considering more data takes more time. – Frank van Puffelen May 09 '16 at 19:19
  • Frank, I agree with "considering more data takes more time", but it is not the same as "But to know which children to send it needs to consider all of them.", right? I would expect that range queries are efficient using an index in general and efficient means that you do not need to traverse all the elements - it should not be like O(N). – bodrin May 10 '16 at 07:53