Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does excess whitespace in SQL queries affect performance?

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.

  1. 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?

  2. 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.

like image 493
DV82XL Avatar asked Sep 13 '25 14:09

DV82XL


1 Answers

No. The additional white space is not going to noticeably affect performance. There might be a little impact in two places:

  • The larger string might be passed through a network. So a really slow network (remember dialups?) might slow it down.
  • The tokenization phase of the compiler has to skip over white space. String processing is pretty fast these days. And just the tokenization phase is doing a lot of other work.

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.

like image 70
Gordon Linoff Avatar answered Sep 16 '25 04:09

Gordon Linoff