Uses extra spaces to make MySQL queries more readable and inefficient to the point where they aren’t worth it?

Let's say I have a request

UPDATE foo.bar SET crop = 5 WHERE earth = 'brown'

I would write it like this:

UPDATE foo.bar SET crop = 5 WHERE soil = 'brown' 

I write queries in this format to make them more readable in my code. However, this is transferred between the web server and the database server, so I assume that the wasted space will take up some bandwidth and slow down the transfer a bit.

Assuming I'm writing much larger queries, those that take up 20+ lines, are they inefficient so as not to cost?

+6
source share
4 answers

You should always write your queries so that they are as readable as possible. If you are really worried about saving bytes sent to and from the server, you can run your queries through the database connector and set options to remove extraneous spaces. Perhaps this is already happening with your current code. Have you really looked at the SQL data that is sent over the network to see what it looks like?

+4
source

No, there is not a big difference in efficiency. Readability is a virtue that must triumph. If you are really interested in efficiency and often use queries that differ only in parameter values, you can consider using prepared statements for which the query execution plan is calculated only once (with a corresponding increase in efficiency).

+2
source

You must format the requests so that you and all members of your team understand them even 6 months after they are written.

Space does not affect the speed of the request.

0
source

IMO, this is premature optimization. Do you have indicators that measure tangible performance differences? The code must be written for reading. Let me just say that you would rather read:

 Update foo.bar Set crop = 5 Where soil = 'brown' uPdAte fOo.bAr sEt cRoP=5 wHErE sOil='brown' 

Readability Issues. It matters a lot more than minute differences in performance.

0
source

Source: https://habr.com/ru/post/887747/


All Articles