This is a tough one because your manuscript will never be "perfect." After all, writing is subjective. However, preparation makes all the difference. I'm guilty of jumping the gun and later realizing I had a much better first line or query blurb at my disposal. Here are some steps (not necessarily in order) everyone should go through before sending that first query:
1) Unless you're writing non-fiction, your manuscript needs to be complete
2) Take a break from the manuscript (as many times as you need to) so you can come back with fresh eyes and a new perspective
3) Critique partners! It's so hard to find people you jive with (yes, I used "jive." I'm old, get over it), but it's immeasurably helpful. I didn't have one I worked well with until my fourth book, and it has made a world of difference in helping me recognize my writing weaknesses and work on fixing them. Usually you both help each other out on your books
4) Beta readers! Different from critique partners, beta readers don't have to be writers themselves (though they can be). Beta readers should see your work when you think it's close to being ready to get feedback on what is and isn't working. Don't get too many people's opinions though (too many cooks and all that). I like to have 2-3, personally.
5) Read your book aloud. This helps you find places where the flow feels off, and forces you to slow down so you don't overlook small mistakes. I recommend Ransom Riggs' Ms. Peregrine's series is perfect if you want an example of this. My husband and I read that series together, and he has a lot of what we call "tasty sentences" because they feel so nice to say!
6) Do a line edit. Look at each sentence individually and see if it's as good as it can be. Don't edit out your voice, but (most of the time) purple prose ought to be minimalized
7) Some people like to hire a professional editor before querying. That's a personal decision to make, but don't think it's a necessity! I know plenty of people who've successfully landed and agent and later been published without paying a dime out of pocket for an editor.
This isn't a comprehensive list, but it's a good place to start! Make sure you're ready before you send out queries because unless you significantly overhaul the book, you only get once chance to query an agent (and sometimes an entire agency!) with each project.
One last thought: if you get to the point where you're changing things without necessarily improving them, you're probably ready to query ;)
Good luck with the preparations!