Before inserting a record that depends on another...
For example, I have 2 tables: groups & users__groups,
the 2nd one depends on the 1st (users__groups.`group_id` on groups.`id`).
The question is, which approach to take to keep the users__groups table clean:
1) When user joins a group, issue a conditional insert that checks whether the group the user wants to join exists. (This approach is to prevent from forgers to insert duplicate data.)
2) Inserts are with no condition (= faster) but then arises the need to delete junk records (inserted by forgers) from time to time. (Most people are legit users, so I don't think there would be too much junk records.)
And regardless of which approach taken: have a unique index in the users__groups table (in my case it spans 2 columns: `user_id` & `group_id`), this approach narrows possible (by forgers) junk records to one per combination of user & group, as the `group_id` (that can be forged) is not checked.
It also speeds up SELECTs & JOINs.
But, slows inserts (users join groups very frequently).
The BIG question is: performance-wise, is it better to have ALL my INSERTs with conditions to check that they are legit OR from time to time issue DELETEs to delete junk records?
Thanks.
Last edited by Cheburgena; 01-10-2009 at 04:09 AM.
|