tags:

views:

49

answers:

3

For example, I'm doing the next action:

SELECT COUNT(id) 
  FROM users 
 WHERE unique_name = 'Wiliam'

// if Wiliam don't exists then...

INSERT INTO users 
   SET unique_name = 'Wiliam'

The question is, I'm doing the SELECT COUNT(id) check every time I insert a new user, despite of using an unique key or not, so... if "unique_name" has an UNIQUE key it will be better for performance than using a normal key?

+2  A: 

What you mean is a UNIQUE CONSTRAINT on the column which will be updated. Reads will be faster, Inserts will be just a bit slower. It will still be faster than your code checking first and then inserting the value though. Just let mysql do its thing and return an error to you if the value is not unique.

Cahit
@cahitbox: Ok, you responded me in the first half of the message, I prefer do the check before because all queries are managed by a framework so I can know if the value I'm adding is unique, like a md5 hash created from a timestamp + ip + a britney spears song, so I can prevent doing the check. Therefor, I need to show a properly user-end error.Thanks
Wiliam
+1  A: 

You didn't say what this is for, which would help. If its part of an authentication system, then why doesn't your query include the user's password as well? If it's not, a unique indexed column used to store names isn't going to work very well in a real-world system unless you are OK with having just 1 and only Wiliam in your system. (Was that supposed to be William?)

And if that name field is really unique you do not need to use COUNT(ID) in your query. If 'unique_name' is truly unique you either get an id number returned from your query or you get nothing.

You'd want something like this: SELECT id FROM users WHERE unique_name = 'Wiliam'

No record return, no Wiliam.

An index (unique or non-unique -- I don't know what you're after here) on unique_name will improve the performance.

Your use of 'unique key' isn't very logical so I suspect you are getting confused about the nomenclature of keys, indexes, their relationships, and the purposes for them.

KEYS in a database are used to create and identify relationships between sets of data. This is what makes the 'relational' possible in a relational database.

Keys come in 2 flavors: Primary and foreign.

PRIMARY KEYS identify each row in a table. The value or values that comprise the key must be unique.

Primary keys can be made from a single column or made of several columns (in which case it is called a composite key) that together uniquely identifies the row. Again the important thing here is uniqueness.

I use MySql's auto-increment integer data type for my primary keys.

FOREIGN KEYS identify which rows in a table have a relationship with other rows in other tables. A foreign key of a record in one table is the primary key of the related record in the other table. A foreign key is not unique -- in many-to-many relationships there are by definition multiple records with the same foreign key. They should however be indexed.

INDEXES are used by the database as a sort of short-hand method to quickly look up values, as opposed to scanning the entire table or column for a match. Think of the index in the back of a book. Much easier to find something using a book's index than by flipping through the pages looking for it.

You may also want to index a non-key column for better performance when searching on that column. What column do you use frequently in a WHERE clause? Probably should index it then.

UNIQUE INDEX is an index where all the values in it must be distinct. A column with a unique index will not let you insert a duplicate value, because it would violate the unique constraint. Primary keys are unique indexes. But unique indexes do not have to be primary keys, or even a key.

Hope that helps.

[edited for brevity]

webdev
@webdev: It was a theoric question about mysql engine with a simple query example. The question isn't for any authentication system, it's for optimize a framework that manages automatically mysql databases.Thanks for your response.Wiliam is my name :)
Wiliam
A: 

Having a unique constraint is a good thing because it prevents insertion of duplicated entries in case your program is buggy (are you missing a "for update" clause in your select statement?) or in case someone inserts data not using your application.

You should, however, not depend on it in your application for normal operation. Lets assume unique_name is an input field a user can specify. Your application should check whether the name is unique. If it is, insert it. If it was not, tell the user.

It is a bad idea to just try the insert in all cases and see if it was successful: It will create errors in the database server logs that makes it more difficult to find real errors. And it will render your current transaction useless, which may be an issue depending on the situation

nhnb
@nhnb: That's the reason I need to do the checks before, to show the proper error. And the check is done automatically, so it must be there yes or yes. That's the reason I ask if UNIQUE key's are faster than NORMAL key's.
Wiliam