If you want optimize it, you should get the SQL the query generates, and run it with EXPLAIN to see if you need to add some indexes.
I would say it's a messy query, that I have no idea what it does, so to make it readable I would probably make a database view out of it and make it look something like