zidZ
Convex Community3y ago
59 replies
zid

Question regarding indexes

Question regarding indexes

I think im a bit confused on the underpinnings on how some of this works.

If im fetching documents that needs to be sorted, im wondering whether if by placing an index on that field, a more efficient sorting algorithm can be implemented. Moreover, Im assuming given that the docs will essentially be sorted ahead of time, this will reduce table scan?

In practice, im trying to figure out:
note: I understand that convex has the default creation time property and thus fields like postDate may be unnecessary, but this is for example sake

Should i use an index in this situation?
// schema
.index("by_points", ["points"])

// app code
threads = await db
  .query("threads")
  .withIndex("points") // does this help?
  .order("desc", "points")
  .paginate(paginationOpts)
  .collect();


Is this proper application of an index? No eq, but rather a gte followed by the second half of the composite index thats not explicitely applied
// schema
.index("by_postDate_points", ["postDate", "points"])

// app code
threads = await db
.query("threads")
.withIndex("by_postDate_points", (q) =>
  // im assuming this is in fact an index
  q.gte("postDate", timeFrameStart)
)
.order("desc", "points")
.paginate(paginationOpts)
.collect();


im trying to see whether the appropriate index should be
.index("by_districtsId", ["districtsId"])
or
.index("by_districtsId_postDate", ["districtsId", "postDate"])

const threads = await db
.query("threads")
.withIndex("by_districtsId_postDate", (q) =>
  q.eq("districtsId", districtsId)
  .gte("postDate", timeFrameStart)
)
.order("desc", "points")
paginate(paginationOpts)
.collect();
Was this page helpful?