Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes, yes, yes.

0. Make the right kind of table. create table user_apps (user_id integer, app_id integer, primary key (user_id, app_id)); -- this does the validation for you, and (should) index the table on both user_id and (user_id,app_id).

1. Make the right kind of insert statement! insert into user_apps select 5, 6 union all select 5, 7 union all select 5, 8; -- etc

2. How do you do recommendations? That sounds like it'd be tough to do well, especially within a web request.



I didn't read the part about validations. That's silly.

Even if you have to do it in the application layer, why would you need more than one SELECT? Just get the installed apps from the DB in a single query and do a set difference operation to only get the apps not already installed.

But I agree that putting the constraint in the DB layer is the correct solution. With MySQL you can just do INSERT IGNORE to only add apps not yet marked as being installed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: