46

Facebook's user id's go up to 2^32 .. which by my count it 4294967296.

mySQL's unsigned int's range is 0 to 4294967295 (which is 1 short - or my math is wrong) and its unsigned big int's range is 0 to 18446744073709551615

int = 4 bytes, bigint = 8 bytes

OR

Do I store it as a string?

varchar(10) = ? bytes

How will it effect efficiency, I heard that mysql handle's numbers far better than strings (performance wise). So what do you guys recommend

Costa
  • 4,851
  • 33
  • 30
Mark
  • 5,423
  • 11
  • 47
  • 62

8 Answers8

86

Because Facebook assigns the IDs, and not you, you must use BIGINTs.

Facebook does not assign the IDs sequentially, and I suspect they have some regime for assigning numbers.

I recently fixed exactly this bug, so it is a real problem.

I would make it UNSIGNED, simply because that is what it is.

I would not use a string. That makes comparisons painful and your indexes clunkier than they need to be.

Phil Wallach
  • 3,318
  • 20
  • 19
  • 14
    From here: http://wiki.developers.facebook.com/index.php/User_ID "If you're storing it in a MySQL database, use the BIGINT unsigned datatype." – Karl B Jan 31 '10 at 19:08
  • 2
    link above not available? i cannot access it – Loonb Dec 10 '12 at 11:19
  • Archived version of above link (2008): https://web.archive.org/web/20080830095724/http://wiki.developers.facebook.com/index.php/User_ID – Costa Jan 08 '14 at 21:51
4

You can't use INT any more. Last night I had two user ids that maxed out INT(10).

4

If you are reading this in 2015 when facebook has upgraded their API to 2.0 version. They have added a note in their documentation stating that their ids would be changed and would have an app scope. So maybe there is huge possibility later in the future that they might change all the ids to Alpha numeric.

https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids So I would suggest to keep the type to varchar and avoid any future migration pains

Vivek Gounder
  • 376
  • 3
  • 9
  • 2
    "maybe"? "possibility"? Why "huge", btw? This answer is random speculating ramblings. IMAO it's unlikely they would do such an idiotic move (not that they haven't done any so far, I give you that): they will likely just give different user ids to each app. – o0'. Sep 04 '15 at 10:33
4

I use a bigint to store the facebook id, because that's what it is.

but internally for the primary and foreign keys of the tables, i use a smallint, because it is smaller. But also because if the bigint should ever have to become a string (to find users by username instead of id), i can easily change it.

so i have a table that looks like this:

profile
- profile_key smallint primary key
- profile_name varchar
- fb_profile_id bigint

and one that looks like this

something_else
- profile_key smallint primary key
- something_else_key smallint primary key
- something_else_name varchar

and my queries for a singe page could be something like this:

select profile_key, profile_name
from profile
where fb_profile_id = ?

now i take the profile_key and use it in the next query

select something_else_key, something_else_name
from something_else
where profile_key = ?

the profile table almost always gets queried for almost any request anyway, so i don't consider it an extra step.

And ofcourse it is also quite ease to cache the first query for some extra performance.

Elmer
  • 9,147
  • 2
  • 48
  • 38
3

Store them as strings.

The Facebook Graph API returns ids as strings, so if you want comparisons to work without having to cast, you should use strings. IMO this trumps other considerations.

dleavitt
  • 1,386
  • 13
  • 14
3

Your math is a little wrong... remember that the largest number you can store in N bytes is 2^(N) - 1... not 2^(N). There are 2^N possible numbers, however the largest number you can store is 1 less that.

If Facebook uses an unsigned big int, then you should use that. They probably don't assign them sequentially.

Yes, you could get away with a varchar... however it would be slower (but probably not as much as you are thinking).

DigitalZebra
  • 39,494
  • 39
  • 114
  • 146
-5

I would just stick with INT. It's easy, it's small, it works and you can always change the column to a larger size in the future if you need to.

FYI:

VARCHAR(n) ==> variable, up to n + 1 bytes
CHAR(n) ==> fixed, n bytes

nickf
  • 537,072
  • 198
  • 649
  • 721
-6

Unless you expect more than 60% of the world's population to sign up, int should do?

Tarydon
  • 5,097
  • 23
  • 24
  • 1
    Maybe I wasn't clear... I am making a facebook app.. so the fact that facebooks id's are 64bit is very relvant... an id I just got from an account was... '100000719084526' so I guess my math was wrong on 2^32. I guess bigint it is – Mark Jan 31 '10 at 15:22
  • 2
    looks like 60% of the world's population will eventually sign up for facebook. – Matisse VerDuyn Oct 09 '12 at 19:06