views:

15

answers:

1

I have a table that stores messages from one user to another. messages(user_id,friend_id,message,created_date). My primary key is (friend_id,created_date). This prevents duplicate messages (AFAIK) because they will fail to insert.

Right now this is ok because my code generates about 20 of these queries at a time per user and I only have one user. But if there were hundreds or thousands of users would this create a bottleneck in my database with all the failed transactions? And if what kinds of things could I do to improve the situation?

EDIT: The boiled down question is should I use the primary key constraint,check outside of mysql, or use some other mysql functionality to keep duplicates out of the database?

+1  A: 

Should be fine as mysql will just do a primary key lookup internally and ignore the record (I'm assuming you're using INSERT IGNORE). If you were checking if they exist before inserting, mysql will still check again when you insert. This means if most inserts are going to succeed, then you're saving an extra check. If the vast majority of inserts were failing (not likely) then possibly the savings from not sending unnecessary data would outweigh the occasional repeated check.

bemace
Well number of possible failed transactions per user is not static. The number of messages retrieved is static. So if the user hits the application 2x and no new messages have been sent then all the messages that are attempted to be inserted will fail because they are already in the db. So I guess I'm confused about what you are saying the answer is. Do I keep it with a plain insert statement and let the primary key handle it or do I need to do something else?
controlfreak123
@controlfreak123 I'd generally say let the primary key take care of it, but maybe you should explain a little more about what you're doing? Why would people be reinserting messages at all?
bemace
@bernace I don't have control over the api that I use to retrieve the messages for each user. So if I request for a users messages before they receive additional messages there will be ones in the set that are already in the database. If i kept track of the timestamp of the most recent message inserted i could compare that to each new message before inserting it. But the primary key method seems simpler in that respect. Although relying on inserts to fail to achieve my objective seems hackish. Hence my question
controlfreak123
If you don't have a way to select only new entries, then I say go ahead with "INSERT IGNORE". Not really a better way to do it.
bemace
Ok, thats what I was looking for. The only thing now is that I need to find a new way of counting how many records were inserted because I'm using php and i was counting how many times the query returned true (without using IGNORE in the INSERT).
controlfreak123