Here is my snipping code of React Native which I am using for my mobile application. I can’t wait 30sec for just 50k data. Imagine the time it will take to load in 500k of data or more
useEffect(() => {
const fetchData = () => {
setLoading(true);
firestore().collection('FData')
.orderBy('counter', 'desc') //order using firestore counter
.limit(50) //limiting value
.onSnapshot(snapshot => {
var items = [];
snapshot.forEach((doc)=> {
items.push({ id: doc.id, ...doc.data() });
});
setLoading(false);
setList(items);
})
};
fetchData();
}, []);
2
Answers
You can try to sort them in the app.
But I think your main problem – you trying to get 50k items by ONE request. It’s to much. 10 item it’s ok. More than 100 – it’s not ok.
Reading 50k documents, it’s way too much data to read in a single go. Bear in mind that, that the query performance in Firestore depends on the number of documents you request and not on the number of documents you search.
So it doesn’t really matter if you search for 10 documents in a collection of 100 documents or in a collection that contains 100 MIL documents, the response time will always be the same. So to be able to read data fast, I recommend you load it in smaller chunks. This practice is called pagination and it can be really simple implemented using query cursors if take a look at the official documentation.
Besides that, since everything in Firestore it’s about the number of reads you perform, reading 50k documents at once, can be a bit expensive.