If you find yourself "rearranging" your "hash" many times over, you're threading into the realm of relational databases. That is the efficient solution. Many have offered advice.
You may also want to look into what "Logic Programming" means, as a gentle intro into the fuss that is relational databases. Good read.