Query mutation with high writes?
Am i doing something wrong, this is just a query but show in red( writes) usage?
// Get all completed sessions for the current user
export const getAllCompletedSessions = query({
args: {},
handler: async ctx => {
const userId = await getCurrentUserOrThrow(ctx);
// Get all completed sessions for this user, ordered by newest first
const sessions = await ctx.db
.query('quizSessions')
.withIndex('by_user_quiz', q => q.eq('userId', userId._id))
.filter(q => q.eq(q.field('isComplete'), true))
.order('desc')
.collect();
return sessions;
},
5 Replies
Im just starting out. Im a total noob.
But your index is called "by_user_quiz" are you sure that it is an index over the userId field? Because if not I think the index will not help you speeding up your query..
Also I read that index can have 2 fields. Maybe also add "isComplete" to your index and it will decrease the amount of rows beeing read and you won't need filter. So instead of ["author", "title"]) use
["userId","isComplete"]
(Source: https://docs.convex.dev/database/reading-data/indexes/indexes-and-query-perf)
If the problem still persists since you marked this as bug, how big is your "quizsessions" table and how often do you call that function? Note it will also rerun on every realtimeUpdate , just to get an idea of where the BWusage can come from.
Also I think in logs you can see the executed functions and then when you click on it on the right side it will tell you what ressources it used. Maybe you can share that too, to realy find out if its overly expensive and realy is a bug.
you might be onto something .. yea my index is definitely wrong its too broad
}).index('by_user_quiz', ['userId', 'quizId', 'isComplete'])
Make sure to read through the best practices guide at some point - somewhat pertinent to this are:
- Avoid
.filter on database queries
- Check for redundant indexesBest Practices | Convex Developer Hub
Essential best practices for building scalable Convex applications including database queries, function organization, validation, and security.
Thanks, @erquhart ! Iād read that before, but the fact it showed up as a write issue on a query really threw me off š
Appreciate the support!
Ooohhh I see what you mean - looks the same in my dashboard as well, I believe that's a bug. All query bandwidth is showing as writes. We'll look into this š