Speed up attrs(merge_container_attrs=True) calls

This change speeds up calls to attrs which merge container attributes by
performing a single sql query for each generation of parents instead of
performing a sql query per parent.