My compiled SQL has a lot of spaces and new lines, how can I get rid of it?

This is known as "whitespace control".

Use a minus sign (-, e.g. {{- ... -}}, {%- ... %}, {#- ... -#}) at the start or end of a block to strip whitespace before or after the block (more docs here). Check out the tutorial on using Jinja for an example.

Take caution: it's easy to fall down a rabbit hole when it comes to whitespace control!