MariaDB BENCHMARK() Function

In MariaDB, BENCHMARK() is a built-in function that executes a given expression a specified number of times.

The MariaDB BENCHMARK() function is primarily used in the command line tool to report query execution times.

MariaDB BENCHMARK() Syntax

Here is the syntax of the MariaDB BENCHMARK() function:

BENCHMARK(count, expr)

Parameters

count

Optional. specified number of times.

expr

Optional. The expression to execute.

If you do not provide the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'BENCHMARK'.

Return value

The MariaDB BENCHMARK() function alwayss return 0.

If the first parameter is given a negative count or NULL, the BENCHMARK() function will return NULL.

MariaDB BENCHMARK() Examples

Example 1

The following example shows how to use the BENCHMARK() function execute the MD5('aaa') function 10000000 times.

SELECT BENCHMARK(10000000, MD5('aaa'));
+---------------------------------+
| BENCHMARK(10000000, MD5('aaa')) |
+---------------------------------+
|                               0 |
+---------------------------------+
1 row in set (1.502 sec)

The function returns 0, but you can get the execution time from the output: 1 row in set (1.502 sec).

Example 2

The following example shows how to use the BENCHMARK() function execute the SLEEP(1) function 10 times.

SELECT BENCHMARK(10, SLEEP(1));

Output:

+-------------------------+
| BENCHMARK(10, SLEEP(1)) |
+-------------------------+
|                       0 |
+-------------------------+
1 row in set (10.111 sec)

In this example, the SLEEP(1) function will execute temporarily for 1 second.

Example 3 - Negative or NULL

If the first parameter is given a negative count or NULL, the BENCHMARK() function will return NULL.

SELECT
  BENCHMARK(-10, SLEEP(1)),
  BENCHMARK(NULL, SLEEP(1));

Output:

+--------------------------+---------------------------+
| BENCHMARK(-10, SLEEP(1)) | BENCHMARK(NULL, SLEEP(1)) |
+--------------------------+---------------------------+
|                     NULL |                      NULL |
+--------------------------+---------------------------+
1 row in set, 1 warning (0.000 sec)

Conclusion

In MariaDB, BENCHMARK() is a built-in function that executes a given expression a specified number of times.