I'm 99% sure that the right index usage will occur if you use the view. But be prepared for it to be inexplicably slow! `EXPLAIN QUERY PLAN` is your friend.

For deleting empty environments:

```
  DELETE FROM environments WHERE
  id != ? AND
  id NOT IN (
    SELECT DISTINCT env FROM events_view
  )
```

should do the trick no? "Delete environments that aren't in the set of used environments".

This should be fairly cheap: it's an easy index walk and then an easy table scan.