19

First of all, I should say that I think that this question does not belong on Meta.


StackOverflow URLs are inspirational to me, the way they are designed is very clever and they work really well but I can't seem to understand the logic being the IDs, here are some examples:

stackoverflow.com/questions/2121720/

If you change the ID to ID++ (2121721) one could think it would fetch the next question. Wrong, instead you're redirect to another question whose ID seems to be totally unrelated (2121212):

stackoverflow.com/questions/2121212/

Why does this happen? Is there some checksum algorithm on the IDs? Here is a sample trace:

2121720 -> exists
2121721 -> 2121212
2121213 -> exists
2121214 -> 2121155
2121156 -> exists
2121157 -> 2120884
2120885 -> exists
2120886 -> 2115014
2115015 -> 2114896 (what happened here?)
2114897 -> 2114799
2114800 -> 2114670 (what happened here?)
2114671 -> exists
2114672 -> 2110215
2110216 -> exists
2110217 -> 2106982
2106983 -> 2106955 (what happened here?)

The IDs seem to decrease. Can someone bring some light on this strange redirect behavior?

0

2 Answers 2

18

It looks to me like the IDs that redirect are actually IDs of answers - so when you put in 2121721, that ID actually belongs to an answer to the question with the ID 2121212. Notice how it redirects to one of the answers, not just to the question. The reason they decrease is that the answer was created after the question.

What this tells me is that the IDs are at least somewhat global. It may or may not mean everything is stored in the same table - though I wouldn't think so.

5
  • Oh! ID's of answers, that's smart! =)
    – Alix Axel
    Jan 23, 2010 at 2:30
  • 1
    Interestingly, the id of this question + 1 redirects to an answer that you have given to a completely different question! Freaky.. Jan 23, 2010 at 2:33
  • @Pongus Mung: Nice catch, freaky indeed.
    – Alix Axel
    Jan 23, 2010 at 2:45
  • Yeah, notice what happens when you click on "link" below the answer - it links to a separate id from the question!
    – BlueRaja
    Jan 23, 2010 at 3:33
  • I dunno, it might make sense to normalise out at least the text of questions and answers into the same table.
    – Justin
    Jan 23, 2010 at 12:00
35

Stack Overflow uses one table to hold both questions and answers. The ID is the primary key in the table called Posts. The ID is incremented each time a question or answer is posted.

So, yes, the ID and ID+1 for two posts would likely point to two entirely different questions.

You can read about the database schema in Understanding the Stack Overflow Database Schema Database schema documentation for the public data dump and SEDE.1

Posts Table

In StackOverflow, questions and answers are both considered posts. If a record has a null ParentId field, then it's a question. Otherwise, it's an answer, and to find the matching question, join the ParentId field up to Posts.Id.

  • Id - primary key, identity field from the original StackOverflow database.
  • Title - the title of the question. Answer titles will be null.
  • OwnerUserId - joins back to Users.Id. If OwnerUserId = -1, that's the community user, meaning it's a wiki question or answer.
  • AcceptedAnswerId - for questions, this points to the Post.Id of the officially accepted answer. This isn't necessarily the highest-voted answer, but the one the questioner accepted.

1Technically the schema is for the Stack Overflow Creative Commons Data Dump which is not a 100% mirror of the live database, but gives you a reasonable subset of the data.

3
  • I've added a link to the schema post here on MSE instead of the sqlserverpedia.com link. I'm not sure if that article held more info then avallable in the MSE post. If it does, do you happen to have the correct link to that sqlservepedia.com content?
    – rene
    Mar 30, 2015 at 12:54
  • You can go back and read the original contents here: web.archive.org/web/20100316141540/http://sqlserverpedia.com/… May 26, 2015 at 3:00
  • 2
    Stack Overflow uses one table to hold both questions and answers Considering that Stack Overflow's DB is certainly distributed (I would say many shards holding the data) and not a single DB server hit by all clients, how do they generate consecutive sequential IDs for questions and answers? Is the questions/answers table (posts table) uniquely stored on a single server and hit by all clients? But then, how does this machine withstand the heavy load when a huge amount of clients hits it at the same time? Thank you!
    – tonix
    Oct 12, 2020 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .