t11s
t11s3mo ago

Is querying by indexed custom ID as fast as querying by Convex's internal _id?

In my Convex database, I have a couple tables that have a custom unique provider_id that comes from an external service I sync with. I frequently need to query by provider_id in both actions and frontend code. My question: If I create an index on provider_id, will queries using it (e.g. getting the unique document identified by provider_id) perform just as fast as queries using the internal _id? Or does Convex have special optimizations for _id that make it faster than any other indexed field?
6 Replies
Convex Bot
Convex Bot3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart3mo ago
Performance is identical 👍
t11s
t11sOP3mo ago
nice! would be nice if there was some way to define fields as id fields so sugar like ctx.db.get() and v.id could work
erquhart
erquhart3mo ago
Convex helpers has getOneOrThrow that works that way, you can import it from the convex-helpers package or just copy paste the function and get rid of the throwing behavior if you want to allow null returns: https://github.com/get-convex/convex-helpers/blob/edb15e450e79e244a0c3efbb7fdc8b66558764c0/packages/convex-helpers/server/relationships.ts#L16-L34
t11s
t11sOP3mo ago
but this doesn't let you search for a custom id field, just the internal _id field, which is not what i was asking for? and what would be the point of copy pasting this and removing the null return check, at that point just use await ctx.db.get(id)?

Did you find this page helpful?