English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The fastest way to calculate the number of rows in a MySQL table?

Let's first look at an example of creating a table, adding records, and displaying them. The CREATE command is used to create a table.

mysql> CREATE table RowCountDemo
-> (
-> ID int,
-> Name varchar(100)
> );

Use the INSERT command to insert records.

mysql>INSERT into RowCountDemo values(1, 'Larry');
mysql>INSERT into RowCountDemo values(2, 'John');
mysql>INSERT into RowCountDemo values(3, 'Bela');
mysql>INSERT into RowCountDemo values(4, 'Jack');
mysql>INSERT into RowCountDemo values(5, 'Eric');
mysql>INSERT into RowCountDemo values(6, 'Rami');
mysql>INSERT into RowCountDemo values(7, 'Sam');
mysql>INSERT into RowCountDemo values(8, 'Maike');
mysql>INSERT into RowCountDemo values(9, 'Rocio');
mysql>INSERT into RowCountDemo values(10, 'Gavin');

Display records.

mysql>SELECT *from RowCountDemo;

The following is the output of the above query.

+------+-------+
| ID | Name |
+------+-------+
| 1    | Larry |
| 2    | John |
| 3    | Bela |
| 4    | Jack |
| 5    | Eric |
| 6    | Rami |
| 7    | Sam |
| 8    | Maike |
| 9    | Rocio |
| 10   | Gavin |
+------+-------+
10 rows in set (0.00 sec)

To quickly calculate the number of rows, we have the following two options-

Query1

mysql >SELECT count(*) from RowCountDemo;

The following is the output of the above query.

+----------+
| count(*) |
+----------+
| 10       |
+----------+
1 row in set (0.00 sec)

Query2

mysql>SELECT count(found_rows()) from RowCountDemo;

The following is the output of the above query.

+---------------------+
| count(found_rows()) |
+---------------------+
| 10                  |
+---------------------+
1 row in set (0.00 sec)