tags:

views:

97

answers:

7

Most sites that have some sort of user profile will either do something like this:

profile.php?u=123445

or else:

profile.php?u=jason.Davis

So I am curious, would it be slower to use a name to lookup a profile with php/mysql vs. using a number to look up a profile record?

+10  A: 

A string lookup would indeed probably be slower than a numeric one in probably every case. But the difference is so small it will never be noticed. Not until you have a really large number of users.

But check out SO: They do both.

http://stackoverflow.com/users/187606/pekka

The number for blazing fast database access.

The name for nice looks, and search engine visibility.

No conflicts with two names, because the ID is what is used to search the record.

I think that is the best of both worlds and the optimum.

Requires URL rewriting, though.

Pekka
Why is a number faster than a name in a db lookup? (Assuming both are indexed).
Esteban Araya
I am no database expert, but common sense tells me that even an index (which is the conversion of a part of the string to a record number) must always be slower than a direct reference to the record number itself. Insignificantly slower, but still slower. Correct me if I'm mistaken.
Pekka
@Esteban, a number would most likely to be stored as a fixed width integer (say 4 bytes) as opposed to a (for example) 32-byte string. The comparison would be faster since there's less data but, more importantly, each page of the index would store more records, leading to less disk I/O.
paxdiablo
this was exactly my theory, also I never realized that is why SO uses that method, thanks more making me see the light
jasondavis
Interestingly, you can actually put any name after the id: http://stackoverflow.com/users/187606/woodpekka
paxdiablo
:) :) --------------
Pekka
A: 

If the name is a key in the database, lookup times shouldn't be too different, and using a name would give you nicer looking URLs, so I would suggest going for using names.

Jeffrey Aylesworth
The name better not be a key in a database, or you will have a problem when another e.g. John Smith tries to open up an account.
Larry Watanabe
Unless of course, you're happy handing out user names like John.Smith.76352574763 :-)
paxdiablo
Wait, you mean most websites allow you to open two accounts with the same name?
Jeffrey Aylesworth
Username != name.
Sneakyness
+1  A: 

The name will be more readable and easier to debug, but names are not guaranteed to be unique so it will need to be augmented by a uniqifier (I believe this is actually a word, used in unification) to e.g. append digits to the end of the name.

Larry Watanabe
A: 

If you use a DB index, it shouldn't be noticeably slower. But what happens if you have two Jason Davises? UIDs are nice because you can guarantee uniqueness.

danben
+1  A: 

The main problem I see with using a name is not speed of lookup, but the fact that you can't have Jason Davis from Omaha, Nebraska and another Jason Davis from Perth, Western Australia.

By all means use the name in everything the world can see but, to ensure uniqueness, I would use an integer ID. This may well speed up the database queries as well but that would be a secondary concern for me. It's doubtful using a string would cause too much angst for your application.

paxdiablo
A: 

Both are wrong. You don't want to get profile.php given u=12345 as parameter. You want to get the profile of user 12345. So use /profile/12345.

Read something about REST, it's cool :)

Nicolás
A: 

Slower?

If you'd tested it, you'd have found out that there's not enough difference for it to be worth bothering about. If you're concerned about performance then this should be the last thing to worry you.

C.

symcbean