jamwt
jamwt•9mo ago

count stuff... 🧵

count stuff... 🧵
4 Replies
jamwt
jamwtOP•9mo ago
if you want the number of records in the whole table, some databases maintain that for you so it is O(1) innodb/mysql doesn't, but convex actually does the api is not stabilized, but you can use it if you want the number of records that match a predicate (let say, an index), you have to walk the index between (start, end) and discover how many there are there almost never some sort of data structure that's keeping a count of the "density" in any given interval so if you want this done in sublinear time for some count you're going to be requesting over and over again (like, how many Xs vs. Ys there are), it's usually necessarily to denormalize and just track this during mutations
Matt Luo
Matt Luo•9mo ago
Hi @jamwt, are you referring to ctx.db.query(tableName).count()? I found that method from this message: https://discord.com/channels/1019350475847499849/1019350478817079338/1249133062076498032 Will this count() eventually be stabilized, or is it a hidden feature that is not supposed to be used in production anytime soon? I ask in order to see if I should invest effort in the short term building my own counter pattern.
jamwt
jamwtOP•9mo ago
I dunno, probably a good question for @sujayakar and the deep database folks. I think there are reasons to reserve the right to remove it at some point, but we just haven't gotten around to final decisions on that yet
sujayakar
sujayakar•9mo ago
I don't think we'd stabilize it as is. we've been chatting about a more powerful counting index that lets the developer specify a field name to group by before counting. so, for example, in addition to counting the number of rows in a messages table, building a counting index on author would support efficiently counting the number of rows with a given author. so, my recommendation would be to use the counter pattern for a production app, since that's guaranteed to be stable. then, down the road, we may add a new index type that supports this pattern more efficiently. or, it may just be a component 🙂

Did you find this page helpful?