Speeding up mySQL Queries

Started by
16 comments, last by graveyard filla 18 years, 2 months ago
Quote:Original post by graveyard filla
In the case of PK's - some tables have fields which "naturally" fit as primary keys... for example, in a "user" table (that stores users login names and passwords), the user_name field would make a good fit for a primary key, since obviously you want to force users to create unique usernames.


Good example, but I'd add one corollary. A primary key should have two features:

1. It uniquely identifies the record. (This is required.)
2. It is static. (This is recommended.)

Your example works best if the user_name field never changes. If the user can change his name, the database will have to perform cascading updates in order to maintain data integrity; so in that case, it would make sense to have a separate auto-incrementing primary key, and a unique index on the user_name field.
Post Extant Graphical MUD
Advertisement
Yeah, you're right.... Like I said, every situation has different variables.....in a web app I did for work I used the username as the PK - because no one ever changed their username (In fact, this wasn't even possible by the software.) It doesn't even seem like something that would be too common in any software, anyway...... have you ever changed your username for anything?
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
Yeah, you're right.... Like I said, every situation has different variables.....in a web app I did for work I used the username as the PK - because no one ever changed their username (In fact, this wasn't even possible by the software.) It doesn't even seem like something that would be too common in any software, anyway...... have you ever changed your username for anything?


Most web-based forums I've seen allow user names to be changed. The users might not have permission to do it themselves, but admins can. In your app, since it's not possible, it shouldn't be an issue. I just mentioned it as something to consider during database design.
Post Extant Graphical MUD
Having looked at the code for a number of web forums, most of them use a numeric ID as the primary key for their users table. They keep the usernames unique by doing a search through the table when creating a new user or changing the username and not allowing it if the search comes up with any existing rows.
Quote:Original post by graveyard filla
Yeah, you're right.... Like I said, every situation has different variables.....in a web app I did for work I used the username as the PK - because no one ever changed their username (In fact, this wasn't even possible by the software.) It doesn't even seem like something that would be too common in any software, anyway...... have you ever changed your username for anything?


It would've been more efficient storage wise to use a surrogate key even if the user name never changed. An int would give you 2.1B unique numbers in only 4 bytes of space. All of your foreign keys would also only be 4 bytes. A varchar would vary, but anyone with a user name >4 chars long would be less efficient spacewise than the int.

As far as performance, joining using the int datatype is more efficient performance wise than either char or varchar (at least on mssql). Also, b/c of the reason above, more data can possibly fit in your data pages that will again make a join more efficient timewise. An argument could be made that since you have the username in the other tables you won't have to join the user table to them at all, but then what about the other tables that you may need to join together such as roles, prefs, posts, whatever.

Very rarely in practice have I seen a natural PK workout better than a surrogate one in storage efficiency, speed, and maintainability.

You're right..... however, in my case, it was done simply because it was easier and cleaner to do. The app was for a start up company with light load and a small database. It simplified queries, etc..... Unfortunately my boss was a cheap bastard so these are the kind of things I had to do [grin].
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
Unfortunately my boss was a cheap bastard so these are the kind of things I had to do [grin].


Understood. I tell my current boss either we can do it right now or do it right later. No matter what we do it will eventually have to get done right. Only takes a couple examples before managers start to realize a little extra time up front can save so much time in the back end. This is even more true on the database side of things b/c once you have a structure built with live data and code built on top of it, it's so hard to change in a graceful manner.
I know what you mean... making big changes to a live production system sucks, no matter what... and when you have to start changing schemas and writing "transition" code, it really gets ugly...
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement