<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David López Dayer on Medium]]></title>
        <description><![CDATA[Stories by David López Dayer on Medium]]></description>
        <link>https://medium.com/@dayerman?source=rss-9cef24752f2a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NG95-q1LTWWouEljSEH0Lw.jpeg</url>
            <title>Stories by David López Dayer on Medium</title>
            <link>https://medium.com/@dayerman?source=rss-9cef24752f2a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:44:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dayerman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improve exponentially the queries of collections in Firestore with collectionGroup.]]></title>
            <link>https://medium.com/@dayerman/improve-exponentially-the-queries-of-collections-in-firestore-with-collectiongroup-1910ca0927e0?source=rss-9cef24752f2a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1910ca0927e0</guid>
            <category><![CDATA[firestore]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[collection-firestore]]></category>
            <category><![CDATA[growthtrainer]]></category>
            <category><![CDATA[efficiency]]></category>
            <dc:creator><![CDATA[David López Dayer]]></dc:creator>
            <pubDate>Fri, 22 Mar 2024 17:05:40 GMT</pubDate>
            <atom:updated>2024-03-23T05:01:52.794Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I needed to access user information stored in my Firestore database. The data is structured with a user collection and each user contains a nested history collection documenting their workout activities.</p><p>Surprisingly, querying the workout history of 128 users took an unexpectedly long 88 seconds. This left me puzzled, considering that definetely should be a way to improve this. The query I used looked like this:</p><pre>// Query the &#39;users&#39; collection to get all users<br>QuerySnapshot usersSnapshot =<br>  await FirebaseFirestore.instance.collection(&#39;users&#39;).get();<br><br>// Iterate through each user document<br>for (QueryDocumentSnapshot userDoc in usersSnapshot.docs) {<br>String userId = userDoc.id;<br>Map&lt;String, dynamic&gt; data = userDoc.data() as Map&lt;String, dynamic&gt;;<br>String userName = data.containsKey(&#39;displayName&#39;) ? data[&#39;displayName&#39;] : &#39;No name&#39;;<br><br>// Query the &#39;history&#39; subcollection for the current user<br>// !!! This is the part taking so long to complete, <br>// as it needs to make one request per user. !!!<br>QuerySnapshot historySnapshot = await FirebaseFirestore.instance<br>    .collection(&#39;users&#39;)<br>    .doc(userId)<br>    .collection(&#39;history&#39;)<br>    .get();<br><br>List&lt;WorkoutSession&gt; history = [];<br><br>// Iterate through each history document for the current user<br>for (QueryDocumentSnapshot historyDoc in historySnapshot.docs) {<br>  final data = historyDoc.data() as Map&lt;String, dynamic&gt;;<br>  WorkoutSession workoutSession = WorkoutSession.fromJson(data);<br>  history.add(workoutSession);<br>}</pre><p>The issue with the code was its reliance on individual requests for each user. However, I stumbled upon a solution: the ‘collectionGroup’ feature. This method enabled fetching all collections with a specific name in a single request. Utilizing two ‘collectionGroup’ queries, one for ‘users’ and another for ‘history,’ I seamlessly merged them based on the userId. The result? A remarkable reduction in data retrieval time to just 4 seconds! Here’s the updated code:</p><pre>List&lt;UserHistory&gt; usersAndHistory = [];<br><br>// Query the &#39;users&#39; collection to get all users<br>QuerySnapshot usersSnapshot =<br>  await FirebaseFirestore.instance.collection(&#39;users&#39;).get();<br><br>// Iterate through each user document<br>for (QueryDocumentSnapshot userDoc in usersSnapshot.docs) {<br>String userId = userDoc.id;<br>Map&lt;String, dynamic&gt; data = userDoc.data() as Map&lt;String, dynamic&gt;;<br>String userName = data.containsKey(&#39;displayName&#39;) ? data[&#39;displayName&#39;] : &#39;No name&#39;;<br><br>// Query the &#39;history&#39; subcollection for the current user<br>QuerySnapshot historySnapshot = await FirebaseFirestore.instance<br>    .collection(&#39;users&#39;)<br>    .doc(userId)<br>    .collection(&#39;history&#39;)<br>    .get();<br><br>List&lt;WorkoutSession&gt; history = [];</pre><p>When looking at how to optimize the queries in Firebase I was pointed to improve the data structure and index database, but nothing pointed me out to this method. I hope it can be useful to you.</p><p>If you want to check my app visit <a href="https://growthtrainer.fit/">https://growthtrainer.fit/</a></p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1910ca0927e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>