The fastest way to check how many messages are in the stream with MySQL and PHP

I was wondering if this is faster when you try, for example, to check how many posts are in a certain thread on the forum. Should I...

(a) Go through each message in the database with a specific thread identifier and count how many rows

or

(b) Add one to a separate column in the thread database each time a thread is created, and then query for this single row

Thanks.

+3
source share
2 answers

? COUNT, ?

, , .

--This will provide counts for all threads
SELECT COUNT(threadID)
FROM Posts
GROUP BY threadID;


--This will provide count for one thread
SELECT COUNT(threadID)
FROM Posts
WHERE threadID=123
GROUP BY threadID;
+10

- .

(a) , , , (b).

+3

Source: https://habr.com/ru/post/1702173/


All Articles