Faster Firestore via Data Aggregation

  Рет қаралды 61,458

Fireship

Fireship

Күн бұрын

Пікірлер: 68
@davidheisnam743
@davidheisnam743 6 жыл бұрын
So if there are 1000 comments and a new comment is added, the cloud function is going to query all 1000 comments. That is 'disturbing' for me. I wish they had a built-in collection size property.
@zuzelstein
@zuzelstein 5 жыл бұрын
This should be top comment. Absolutely not production ready app. This is not cost effective to query all comments each time.
@RobertKing
@RobertKing 5 жыл бұрын
I suggest putting a "limit 100" on your query. that way it's production ready. Then in the user interface, if you get back 100, just show "100+". If you're getting more than 100 comments, you have other issues such as spam detection you need to take care of.
@b5a5m5
@b5a5m5 5 жыл бұрын
I may be mistaken for I haven't gone in depth in the docs but, is the "querySnapshot" not a reference to the documents that match the query from the indexes? Meaning the query isn't actually incurring a charge for reading the documents until forEach is executed?
@ScrubsFrance
@ScrubsFrance 5 жыл бұрын
BAM5 size contains the number of documents returned firebase.google.com/docs/reference/android/com/google/firebase/firestore/QuerySnapshot.html#size() But it contains all the documents even before the foreach :( Have to limit the number of documents expected directly in the query request ;)
@otheraw5659
@otheraw5659 5 жыл бұрын
You can have the trick in the firebase channel. I forgot which videos, but I think it is one of the newest video about pricing. Basically you can just use math, save savedCommentsCount + 1 or something like that, u don't have to re-read all the doc in the collection
@basdfgwe
@basdfgwe 6 жыл бұрын
Instead of reading all documents each time can you simply have a cloud function that increments a counter, and decrements based on a deletion.
@im.thatoneguy
@im.thatoneguy 5 жыл бұрын
There is a danger of a race condition. If you have a user add and then a user delete smultaneously you could have a condition where they both start from the same document count and attempt to modify at once. Say we start at 100 posts -1 Delete. 99 posts. Now the Add function is also running in the background and it read the 100 posts before updating and your post count is now "101" instead of 100. It's still possible for the number to be off with parallel operators but it'll never be more off than the current number of active functions. If you have 9 people all try to delete simultaneously you might go from 100 -1 (9x times) and end up at 99 instead of 91 comments. Now when someone adds a document even if it was 99, it'll still recount and end back up at 92.
@basdfgwe
@basdfgwe 5 жыл бұрын
@@im.thatoneguy hi Gavin, what if you did it with a transaction ?
@eric-id6bk
@eric-id6bk 3 жыл бұрын
@@basdfgwe This is a bit late, however, this wouldn't work. Transactions are not cross-user. You'd be best using a queue for the actions (increment, decrement) which invokes a cloud function and performs that action on that document. That way you'd only be writing to that content one at a time.
@dionricky
@dionricky 3 жыл бұрын
For anyone looking for answer to this, maybe you can just increment/decrement the counter based on the action without even reading all the comments doc. But for certain interval, update the counter to match the size of comments in the firestore. This way is more "cheap" but requires you to set up another service for scheduling the counter "cache" refresh job. Edit: race condition on post-comment case is totally fine. But if you need something more reliable, for ticket reservation for example, probably use SQL instead of NoSQL db.
@basdfgwe
@basdfgwe 3 жыл бұрын
@@dionricky hi Don, I didn't think there was a way of getting a count like what we have in SQL.
@drantunes
@drantunes 6 жыл бұрын
Use a limit(5) query in comments collection seems more simple, or no?
@saadabbasi2063
@saadabbasi2063 5 жыл бұрын
Hey Diago, Good approach, but correct me if i am wrong. Till this point that "we need to isolate reviews/comments in to a separate model" we are on the same channel, right? If we query by limit on othher document then n of population queries will be made to reviews/comments collection (here n is number of posts) and reffing in mongodb is not thhat performant as Joins in SQL. Is that still a good approach, if you could share your thoughts
@tntg5
@tntg5 4 жыл бұрын
I have a similar feature but I don't nest comments under the parent collection. Instead, I have a separate collection at the root that is called comments, and have a property called parentId. That is because a comment can be attached to a post or event or some other object. Then when I call a post, I also make another call to comments with the post id, order desc, and limit to 5. But if I needed to display the number of comments it would be different
@johanchouquet2872
@johanchouquet2872 5 жыл бұрын
Thanks a lot Jeff! Could you prepare a video where you have to do Transaction in Cloud Functions for aggregation of a counter for example in order to prevent contention on multiple accesses on data ? It's a really common use case, and there's few docs about this on the web!
@RafaelTorquato
@RafaelTorquato 2 жыл бұрын
This solution works well with 1s limit on the updates of a document?
@krisnaldoHD
@krisnaldoHD 6 жыл бұрын
Is this not as insufficient as reading all of the comments and then counting the number of objects? Or is it different when you're doing this on the server side?
@artursponchiado7265
@artursponchiado7265 6 жыл бұрын
Good video! But I was thinking, does the firestore don't have a limit of 1 transaction/update per second? Even if I wrap all that code in a transaction, it will be limited by this 1sec update limit, right?
@KosratDAhmad-ti4qy
@KosratDAhmad-ti4qy 6 жыл бұрын
event.params doesn't exist from DocumentSnapshot??
@whatthefunction9140
@whatthefunction9140 3 жыл бұрын
If I have 1 million documents and want to delete 500k Oldest is that one delete or 500k deletes?
@pandarzzz
@pandarzzz 6 жыл бұрын
Thank you for sharing this informative video! 🐋🖐🏾 I find this video very useful. I hope you are well.
@RageBasterd
@RageBasterd 6 жыл бұрын
excellent content as always my feed is full of firebase today, with all this dev summit and waht not. to bad most of it is for native mobile
@Fireship
@Fireship 6 жыл бұрын
Thank you! Bunch of great content right now, hard to keep up.
@niyongaboeric
@niyongaboeric 6 жыл бұрын
Special Angular Men Thank you.
@Fireship
@Fireship 6 жыл бұрын
Thank you!
@TimeoutMegagameplays
@TimeoutMegagameplays 5 жыл бұрын
1:48 What would be ideal for something that would get as many reads as writes?
@POSITIVO509
@POSITIVO509 6 жыл бұрын
Amazing
@jajasaria
@jajasaria 5 жыл бұрын
i know mysql but why nosql look so hard to me. hope someone could help me.
@or.o.s.t8190
@or.o.s.t8190 3 жыл бұрын
How does he write in batches (not letter by letter)? I assume it's done in editing but could there be an extension for vscode/some other magic in play?
@crowdozer3592
@crowdozer3592 3 жыл бұрын
I'm not 100% sure how he's doing it, maybe editing, but I also vaguely recall there being an extension that does something similar to this for demonstrative purposes. I remember a coworker saying it'd be funny to troll someone by pretending to be doing something super complex super fast with it
@crowdozer3592
@crowdozer3592 3 жыл бұрын
Given the mouse movements I'm more inclined to think it is actually an extension
@zacturner7894
@zacturner7894 6 жыл бұрын
Is this code still valid? I get a log error on the server that 'commentId' and 'postId' are trying to call on undefined ie ( 'event.params'). Any ideas?
@Fireship
@Fireship 6 жыл бұрын
For the most part yes, but there were some changes in v1.0 that is causing your error firebase.google.com/docs/functions/beta-v1-diff
@zacturner7894
@zacturner7894 6 жыл бұрын
@@Fireship Thanks mate, got it sorted.
@JeffrinJ
@JeffrinJ 6 жыл бұрын
What Operation system are you using?
@erperejildo
@erperejildo 5 жыл бұрын
but where is that index.js located?
@boon_keng
@boon_keng 5 жыл бұрын
Firestore support createdAt ordering?
@primeraposicion
@primeraposicion 6 жыл бұрын
Thank you { while (true) { n * 'so' } } much!! :D
@mozartzerna7024
@mozartzerna7024 6 жыл бұрын
i have this error Cannot read property 'commentId' of undefined ? need help?
@ngrund1
@ngrund1 6 жыл бұрын
You are using a newer version of Firebase SDK for Cloud Functions than he is in this video. Check out the changes needed: firebase.google.com/docs/functions/beta-v1-diff. Here is how I changed the code he provided to get this working: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.aggregateComments = functions.firestore .document('posts/{postId}/comments/{commentId}') .onWrite((change, context) => { const commentId = context.params.commentId; const postId = context.params.postId; Pretty much the same from there on out.
@murthy900
@murthy900 6 жыл бұрын
how to add new subcollection from angular ?
@Nek208
@Nek208 5 жыл бұрын
How would you solve relationship? Example: see userName or userPhoto on every comment?
@nickstylezmedia
@nickstylezmedia 5 жыл бұрын
kzbin.info/www/bejne/oJ6ZZ4eJobunmcU :)
@saffanalvy
@saffanalvy 2 жыл бұрын
Hi. Can you please make this process in Flutter for Android section. I'll highly appreciate it. Thanks.
@niyongaboeric
@niyongaboeric 6 жыл бұрын
Hi Jeff, I may you advise me the level of typescript I must have to start developing SPA. Thanks
@harpymaslow
@harpymaslow 6 жыл бұрын
You rock. Gotta take the pro stuff. Don't want to pay every month though. Is the 40% offer still up for the one-time purchase ? Thx man
@Fireship
@Fireship 6 жыл бұрын
Thank you! The one-time membership is not currently discounted, but the monthly/quarterly is. You can cancel anytime. But with the lifetime you also get a free t-shirt :)
@harpymaslow
@harpymaslow 6 жыл бұрын
Angular Firebase Ah the t-shirt. See I need the lifetime plan ^^ The discount was only during Google I/O ?
@Fireship
@Fireship 6 жыл бұрын
Yes, but I will likely discount it again in the future. Not definite timeline tho.
@harpymaslow
@harpymaslow 6 жыл бұрын
Angular Firebase Ok. I'll wait for it then. Great job on the videos. You're helping a LOT
@game-o-zone8170
@game-o-zone8170 6 жыл бұрын
Everything you teach is too good, only thing is you have to slow down a bit, and explain it in depth. rest everything seems gr8 :)
@ashb9254
@ashb9254 6 жыл бұрын
he thinks he is teaching to experts
@ImperiumLibertas
@ImperiumLibertas 6 жыл бұрын
Most of these videos are directed towards those who are "experts." If you want to learn angular, he has a beginners tutorial series.
@sulaiman2606
@sulaiman2606 6 жыл бұрын
Great work😍 Can you please provide us with the structure of this application
@Fireship
@Fireship 6 жыл бұрын
Thank you! Follow the link in the description to get the full code on github.
@hirenpatelcr
@hirenpatelcr 6 жыл бұрын
Super Fast.... cant understand.
@yeilmusic
@yeilmusic 4 жыл бұрын
FAVOURITES, likes ? how to fetch which video like user.
I tried 5 Firebase alternatives
10:31
Fireship
Рет қаралды 789 М.
Understanding Firestore queries - Firebase v9
13:21
Atomic Code
Рет қаралды 6 М.
У ГОРДЕЯ ПОЖАР в ОФИСЕ!
01:01
Дима Гордей
Рет қаралды 7 МЛН
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,3 МЛН
Violet Beauregarde Doll🫐
00:58
PIRANKA
Рет қаралды 52 МЛН
~/.dotfiles in 100 Seconds
13:54
Fireship
Рет қаралды 417 М.
How do Cloud Functions work? | Get to know Cloud Firestore #11
18:21
How do Transactions Work? | Get to know Cloud Firestore #8
16:10
80% of programmers are NOT happy… why?
4:43
Fireship
Рет қаралды 1,2 МЛН
7 Database Paradigms
9:53
Fireship
Рет қаралды 1,6 МЛН
Security Rules! 🔑 | Get to know Cloud Firestore #6
22:39
Firebase
Рет қаралды 272 М.
How Do I Paginate My Data? | Get to know Cloud Firestore #7
10:16
100 Firebase Tips, Tricks, and Screw-ups
24:31
Fireship
Рет қаралды 187 М.