I currently run a cron job every 20 sec but its using bandwidth a lot how can i reduce the bandwidth
My cron job currently runs every 20 sec. It scrapes data from a site and inserts into the db. The db has 300 rows and 12 columns which is updated every 20 sec. My ideal situation is to be able to run the cron every 10 sec or even lower but seeing that i am doing something clearly wrong how can i optimise this behavior. I liked using convex because of the real time changes that can be seen in the UI when i change the db data after cron job updates it. Any help is appreciated.
36 Replies
Are you using indexes for any ctx.db.query calls?
im sorry im new to convex could you explain more what you mean by indexes
this is my get functions currently
Got it
ctx.db.query()
will always read the entire table unless you use an index. Filters only apply after everything is read in. Indexes are the way to limit the number of records being read, and therefore the read bandwidth.You can read up on indexes here: https://docs.convex.dev/database/indexes/
Indexes | Convex Developer Hub
Indexes are a data structure that allow you to speed up your
ok thank you will read up on this
is there a way to optimise write as well since im updating 300 rows every 20 sec
which looks somewhat like this
Not really, except ensuring you're only patching what's necessary. Honestly not sure how much of a diff that makes. If you want the data updated every 20 seconds, have to make the writes.
Use indexes for all queries and then see where that puts your bandwidth
i wonder how indexes would help when i want to get all the data
for specific row using indexes makes sense but how about in the querry to fetch all data
Yeah if you need to get all stocks for that query, no change needed. Just make sure it isn't being called more than once per cron.
the json is small
why is read more expensive than write
a single write takes about 68kb and read takes ~10mb
@erquhart
that usually means you're missing an index...
hmmm the thing is im writing 304 rows to a table
and reading the same thing
so was wondering how index would help in reading all the rows im not filtering anything
only 1 query and 1 mutation
gotcha
what does your "Functions breakdown by project" section look like in usage? when you break down by bandwidth?
it's this page -- this is what it looks like for my team
that's function calls -- change the dropdown to bandwidth
sure
cool. can you paste the body of that mutateStock function?
b/c yeah, that's doing a ton of reads
yeah! first query, no index
that's your culprit
so if you ever have this again, you can use this function detail view in order to zero in on what function is the offender
and then if bandwidth is the issue... it's almost always a missing index
we're working on tooling and lints and stuff to help point these issues out more easily
they happen far more often that they should, we're not happy about that! we haven't done a good enough job here
so could you explain what the ideal fix could be done here
cause i have exceded my convex plan
and im in lockdown
this:
until this month
needs to change to
.withIndex
ok i ll look into using index while filtering
let me ping @Indy , I think there's something we can do here when it's a temporary surge that goes away of bandwidth
thank you so much that would be a great help
https://discord.com/channels/1019350475847499849/1318662869830598706/1318662869830598706. <- @uzamaki21
there's a little thread I created with our ai tool
sure ill take a look thank you
Hi @uzamaki21 usually if you have a spike on one day our system automatically forgives it, but it looks like you went over for a few days. Send us an email at support@convex.dev and we'll see if we can figure something out.
sure