Skip to content

DBSIZE

The DBSIZE command returns the number of keys in the currently selected database. This command never fails, has a time complexity of O(1), and is a very fast operation.

Terminal window
DBSIZE

Parameter Description

The DBSIZE command takes no parameters.

The DBSIZE command returns the total number of keys in the currently selected Redis database. In Redis, there are 16 databases by default (numbered 0 to 15), and you can switch between them using the SELECT command.

Each database is an independent namespace and can contain keys with the same name without conflict. The DBSIZE command only returns the number of keys in the currently selected database.

In redisun, the DBSIZE command is implemented through the DBSizeCommand class and the dbsize method in the Redisun class.

Redisun redisun = Redisun.create(options -> {
options.setHost("localhost");
options.setPort(6379);
});
// Get the number of keys in the current database
long keyCount = redisun.dbsize();
System.out.println("Current database has " + keyCount + " keys");
  1. The DBSIZE command only counts keys in the currently selected database
  2. Different databases are independent namespaces
  3. The command has a time complexity of O(1) and executes very quickly