Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the 10 million questions counters on stackoverflow are made?

Stackoverflow is celebrating 10 Million Questions. Congratulations!

Providing us this link: https://stackoverflow.com/10m

There are 3 counters displayed, and the number of each counter is increasing fast and not static.

However, I don't see any AJAX requests for displaying the recent result of each counter.

I got such a counter in our forum as well, but to get the number of recent posts, I do an AJAX request every 3 seconds, running the query SELECT MAX(id) AS total_posts FROM forumposts to display.

I know, this is not the best solution we have, and it will be not correct anymore if a post is deleted. Using the SELECT COUNT(id) command is too slow, since we have also more than 10 million posts.

So, how Stackoverflow is displaying the increase of their counters without any requests? It seems to be a nicer solution for me and I'd like to use it for our forum as well then.

like image 506
lickmycode Avatar asked Aug 31 '15 12:08

lickmycode


People also ask

How do I ask a question on Stack Overflow?

1. Click the "Ask Question" button. Navigate to the Stack Overflow homepage in your browser at stackoverflow.com. In the upper right hand corner of the page, you should see the Ask Question button, which you should click to continue.

Is Stack Overflow worth the money?

Naturally, Stack Overflow has been priceless in terms of usefulness to me while I have learned to code and debug in a trial-by-fire manner. Several of my questions were basic questions when I started out.

How to improve your Stack Overflow question asking skills?

Often these will be constructive, and by paying attention, you can learn how to improve your Stack Overflow question asking skills for next time. Leave your browser open to your post, and respond to questions by editing your post to provide more, or more precise, information. Accept and implement possible solutions.

How to accept and implement possible solutions to a question?

Accept and implement possible solutions. To accept an answer that you consider satisfactory, you can click the green tick-mark under the score of the answer. This will indicate that the question is finished, and will give the user who answered points as a reward for contributing.


1 Answers

It's using websockets. You can see it from the network tab. wss://qa.sockets.stackexchange.com/ From the request, it does look like it gets a total count everytime. enter image description here

SQL count being slow A poor man's way of improving performance would be to track statistics yourself by creating a table thats keeps track of the row count. You'd be burdened with making sure that you update that table every time you delete or add new records. Before you do that, do some research on indexes and the following questions

  • mysql COUNT() num rows too slow
  • SQL Server Count is slow
like image 52
Juan Mendes Avatar answered Oct 29 '22 12:10

Juan Mendes