Database Reference
In-Depth Information
Counting rows
While in some cases, it might be fine to simply keep requesting pages until no more re-
cords are encountered, it's often nice to give the user a sense of how many records they're
dealing with up front. Happily, we can use the
COUNT
function to do just this:
SELECT COUNT(1) FROM "user_status_updates"
WHERE "username" = 'alice';
Instead of requesting a list of columns or a wildcard to get all columns, we select
COUNT(1)
to get a total count of rows matching the
WHERE
condition:
We could have also used
COUNT(*)
, which behaves in the same way as
COUNT(1)
.
There are, however, no other valid ways to use the
COUNT
function in CQL.
Note
While the
COUNT
function is convenient, it's of dubious utility in production applications.
Under the hood, performing a
COUNT
query requires the same work for Cassandra that ac-
tually returning the rows would; by using
COUNT
, you're mostly saving network band-
width. If you need to efficiently track the number of rows stored in a given table or parti-
tion, you're better off using a counter cache. We'll introduce a good way to do this in