views:

29

answers:

1

For example:

There are 10,000 items in a list and the user needs to be able to select a portion of these based on whatever criteria, and then put them into a group. So as the user, I filter the list of these 10,000 items based on whatever, hit select all and then click "Group".

I'm trying to come up with the most efficient method of linking these all together.

If I have item_table and group_table and then group_has_item, I'd effectively have to INSERT 1,000 rows at one time into the group_has_item table. Is there an efficient way to do this? Or is there a better schema? The only requirement is that these items have to be able to be tied to multiple groups at once.

+2  A: 

If I have item_table and group_table and then group_has_item, I'd effectively have to INSERT 1,000 rows at one time into the group_has_item table. Is there an efficient way to do this?

Yes - what you describe is the proper way to associate the records.
It's called a many-to-many relationship, and uses a table for holding these relationships. Some call the table linking the two entities map, lookup, xref... I've forgotten some of the synonyms.

OMG Ponies
Just to add to that, performance is not an issue here if you select your data types and indexes appropriately.
NullUserException
Yeah, I know that I described a standard a many to many relationship - but I'm wondering if there's a better way since I'm trying to establish these relationships with huge numbers of things at random.
Chris
Performance isn't an issue with issuing 1000 insert statements at once?
Chris
@Chris: many-to-many is the best solution if you want to perform any sort of relational integrity, reporting... Honestly, how often do you see 1,000 inserts happening?
OMG Ponies
Agreed - you can work on the performance issues, but this is the right way to represent the relationships.
Scott Saunders
@OMG Ponies: If I enable this functionality, a lot more often than I'd like. But I did just think of a better way to do it. When they create the group, I'll only have it store the filter conditions plus additions or exceptions. That should reduce the db work quite a bit.
Chris
@Chris: Yes, but what about when you want to find group/item associations for only two groups? I can't stress how much you risk hamstringing your application by storing denormalized data. Ultimately, it's your app and your decision...
OMG Ponies