0

I'm writing a server that allows multiple users can modify a post.
So I created Permission table that contains user id, post id, and permission data.
And I just wanted to query this with only one value (I just thought that querying with one value is more efficient than querying with two values), so I googled and found this.
However, I also found that Cantor Pairing Function isn't unique,
so we can't use Cantor Pairing Function as a primary key.

But that only deals with Cantor way, not Elegant Pairing Function (by Matthew Szudzik)
How about Elegant pairing function?
Is it safe to use Elegant pairing key as primary key in database?
Or should I just give up and query that with two values?

Lee M.U.
  • 113
  • 1
  • 11

1 Answers1

1

Unless single ID field constraint is enforced by your storage, I believe that this is an example of famous

Premature optimization is the root of all evil. (D.Knuth).

Note that Cantor pairing function is not unique for real numbers but it is unique for integers and I don't think that your IDs are non-integer numbers. I think this is quite the same for the Elegant Pairing Function you reference because structurally it is based on the same idea. If you need a specific counter-example, here is one:

ElegantPair(1, 2) = 2^2 + 1 = 5 = 2.1^2 + 0.59 = ElegantPair(0.59, 2.1)

On the other hand the real problem is that you can't fit two 32-bit (or whatever size you use) int values into a single int value of the same size no matter what clever trick you use. The trick behind pairing functions is based on the fact that whole N is infinite and NxN has the same "size" as just N which is obviously not true for a fixed-size real (computer) world integers. Thus whatever mapping you use over fixed-sized ints, it will not be unique.

SergGr
  • 22,842
  • 2
  • 25
  • 49