How is the best way to do a ‘running total’ system like the tags search on stackoverflow? If I click on ‘php’ for example, its show the total amount of items on ‘each’ other tags, and its very fast. How can I do this in php with mysql?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a query that “looks” like that
This query makes the plausible assumption that the Posts (Questions) on SO are stored in two tables;
SO_Posts, containing one record per Post, and holding info such as a PostId (Primary Key), the question itself, the date, the title etc.
and
Post_Tags which associates a given Post (by its Post_Id) with a Tag (or more likely a TagId since tags ought to be normalized, but that’s a detail). For a given Post, there are as many records in Post_Tags as there are different tags attached to the post.
Note: in effect the structure of the SO Posts database is more complicated, with various tables for storing comments, replies etc. but with regards to the Post-to-Tag relationship, this two-table layout (or more likely a 3 tables layout allowing to have a tagId in the Post_Tags rather than the tag itself) captures the essence of how it is possible, easy and fast (provided the right indexes) to show these filtered agregate counts.
The idea is to find all PostIDs associated with the targeted tag (here ‘PHP’) (looked-up in “T1”) and then to aggregate all the Posts (in “T2”), by Tag.
Note that the main table SO_Posts is not necessary here, but it would likely be part of the query, for example to allow adding extra criteria such as say the Post status (not being closed…).