DeepakDahiyaD
Convex Community2y ago
2 replies
DeepakDahiya

Beyond Performance: Do Indexes Offer Advantages Over Filter Functions?

I've observed that a query like the following:

export const getUserByNameAndEmail = query({
  args: {
    email: v.string(),
    name: v.string(),
  },
  handler: async (ctx, { email, name }) => {
    return await ctx.db
      .query("users")
      .filter((q) =>
        q.and(q.eq(q.field("name"), name), q.eq(q.field("email"), email))
      )
      .unique();
  },
});

works perfectly fine without needing to create any indexes(index || withIndex).
Aside from the performance boost that indexes provide, are there any other potential issues we might encounter by relying solely on the filter function? Could there be situations where using an index is necessary to ensure the query functions correctly?
Was this page helpful?