N
N
Noob--Saibot2017-06-02 12:09:38
PHP
Noob--Saibot, 2017-06-02 12:09:38

What is the fastest way to get the number of records in a large mysql table??

A site with 200,000 pages began to noticeably slow down. I'm sure that the way to get the number of records like below is bad:

$result = mysql_query("SELECT COUNT(*) FROM cache");
$total_records = mysql_result($result,0,0);

Can you please tell me which one is easier?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Lero, 2017-06-02
@Corazonroto

Alternatively, one of: https://habrahabr.ru/post/126233/
They also pull through cursors: https://habrahabr.ru/post/30046/. I think mysql also has this feature.
But(!) if your number of records rarely changes, then perhaps it makes sense to simply cache the request before any event occurs.
Everything depends on the situation. Maybe one of the suggestions will work.

S
shagguboy, 2017-06-02
@shagguboy

all normal people consider this on triggers or do not consider it at all

K
Kirill Gorelov, 2017-06-06
@Kirill-Gorelov

I ran into the same situation two days ago.
Caching didn't work for me. I was told to use indexes.
True, it was already too late, I cleaned the database, now I'm waiting for 150 thousand records again, I'll try. But if you haven't cleared it, then try with indexes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question