add query and optimize