|
A for loop with multiple insert statements. Presumably you're going to be reading from the database far more than writing to it, so a for-loop, though less efficient than a single insert query, seems the most efficient way overall.
You could set up a table to store all an animal's ancestor categories, I guess, something like:
ancestors(category_id, ancestor_id)
But that's just trading memory for CPU, and considering each animal is probably only going to belong to a relatively small group of categories, it's probably not worth it. Go with a for-loop and multiple inserts, would be my advice.
|