When we write SQL queries, we add extra spaces for indentation to make it more readable. These spaces can quickly add up. I was wondering if these extra spaces can noticeably affect network latency and/or query parsing time in the DB.
Does the query string that is sent from the client/ORM to the database server include these spaces as well, or are they stripped/normalized by the client/ORM?
Would having a lot of space in the query string affect performance noticeably, therefore should be minimized within reason?
To illustrate, the following query consists of 577 characters total:
select * from (
select
u.id as userId,
u.name as userName,
sum(case when date(u.login_at) <= date('2020-12-31') then 1 else 0 end) as loginCount,
max(case when date(u.login_at) <= date('2020-12-31') then u.login_at else null end) as lastLogin,
from user u
join user_login ul on u.id = ul.user_id
where u.id = '12345'
group by u.name, u.id
) x
where loginCount > 0;
Whereas the same query stripped of unnecessary whitespace contains only 329 characters, a 43% reduction in query size.
select * from(select u.id userId,u.name userName,sum(case when date(u.login_at)<=date('2020-12-31')then 1 else 0 end)loginCount,max(case when date(u.login_at)<=date('2020-12-31')then u.login_at else null end)lastLogin,from user u join user_login ul on u.id=ul.user_id where u.id='12345' group by u.name,u.id)x where loginCount>0;
Note: I'm not endorsing writing queries like the second one. I just want to know whether there is a material difference in timing.
No. The additional white space is not going to noticeably affect performance. There might be a little impact in two places:
The cost of a select
query is rarely in the compile phase anyway. Reading the data and processing it is usually where (almost) all the time is spent.
Note: You could trivially create edge cases, such as select 1
followed by or preceded by a million spaces where the compilation would be noticeable. But you would have to be intentionally creating such a query string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With