Fixes to DB triggers, and add indexes

Add missing intermediate table deletion to delete trigger.
Add missing WHERE clause to insert in trigger.
Add indexes.
D'oh, brain fart.