query with joins improvement