Fastest way to count how many records I have in a MYSQL table

Asked

Viewed 5,894 times

0

As the title says I wanted to know the fastest way to count records from a table of thousands of records.

2 answers

1


I believe that the easiest way is using COUNT, I use it daily and never had problems and use on large bases.

The COUNT function (*) returns the number of records in a table:

SELECT COUNT(*) FROM table_name;

COUNT(DISTINCT column_name) returns the number of distinct values of the specified column:

SELECT COUNT(DISTINCT column_name) FROM table_name;
  • How can I place the amount of records that returns within a php variable?

  • @xdam98 ai is already another question, open it separately that will come many good answers.

  • even faster would be: SELECT COUNT(1) FROM table_name; because the bank will interpret the entire line as 1... using * the bank sees all values of the line

1

You can use the Count() function to do this.

SELECT COUNT(*) FROM tabela

and you can also do this using filters from your tables to bring only what interests you:

SELECT coluna1, coluna2, COUNT(*) FROM tabela
WHERE coluna1 = 'dado' OR coluna2 = 'dado'
GROUP BY coluna1, coluna2;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.