#order?

11 messages · Page 1 of 1 (latest)

plain moss
#

const mostRecentSong = await ctx.db.query("neteaseCloudMusicRecentSongs").first();
|
if thre is no addded index to this table, does the first always returns the latest inserted documents?

fickle bisonBOT
#

Hi @plain moss. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes. By default, Convex orders documents by _creationTime in ascending order. So .first() without any explicit ordering will return the oldest inserted document, not the most recent one.

To get the most recently inserted document, you should use .order("desc").first():

const mostRecentSong = await ctx.db
  .query("neteaseCloudMusicRecentSongs")
  .order("desc")
  .first();

This works because Convex organizes documents by insertion order by default, and .order("desc") starts scanning from the right (most recent) end of that shelf. [Ordering] [Query Performance]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@plain moss Kapa.ai is still learning and improving, please let me know how I did by reacting below.

plain moss
#

@fickle bison does this query scan the entire table or does this run in cosntatnt time

fickle bisonBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The query .order("desc").first() runs in constant time — it does not scan the entire table.

Since Convex stores documents in insertion order by default, .order("desc") simply starts scanning from the most recently inserted end of the table, and .first() stops as soon as it finds the very first document. Only one document needs to be examined. [Query Performance]

As the docs put it using the library analogy: the librarian starts on the right edge of the shelf and only needs to look at a single book to get the result.

This is in contrast to something like:

// BAD: may scan the entire table
const mostRecentSong = await ctx.db
  .query("neteaseCloudMusicRecentSongs")
  .filter((q) => q.eq(q.field("someField"), someValue))
  .first();

...where .filter() could force a full table scan to find the first matching document. [Filtering Performance]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@plain moss Kapa.ai is still learning and improving, please let me know how I did by reacting below.

plain moss
#

@fickle bison so the sort dont take O(n) to sort the documents in the table

fickle bisonBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Correct. There is no sorting happening at query time. Convex pre-sorts documents by _creationTime as they are inserted — it's the default storage order. Think of it like books placed on a shelf left-to-right as they arrive.

So when you call .order("desc").first(), Convex doesn't sort anything — it simply jumps to the end of an already-ordered structure and reads one document. That's why it's constant time, not O(n). [Query Performance]

The same principle applies to indexes: when you define an index, Convex maintains a pre-sorted data structure for that index. Queries using .withIndex() don't sort at query time either — they jump directly to the relevant range. [Indexes]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: