SQL Query Formatting Tools Used At Percona
Percona engineers often need to analyze and review complex SQL database queries. Good formatting can make these SQL queries much easier to understand and work with. Without clear formatting, queries can become confusing and hard to debug.
Online query formatting services provide one set of solutions. Examples are Code Beautify, FreeFormatter, and sqlformat.org. However, many users are not comfortable sharing their queries with third-party services, especially if your SQL code contains confidential information.
This article examines alternatives to online tools for SQL query formatting tools that have been successfully used by Percona engineers. These solutions come in different types:
- Plug-ins to your code editor or IDEs.
- Database Management tools
- Terminal Tools
Let’s examine these options.
Code editor or IDE
Most modern code editors have functionality or plug-ins for formatting queries. We checked Visual Studio Code and Sublime because these popular code editors are available for Linux, Mac, and Windows.
Visual Studio Code knows how to format SQL queries, as well as available Extensions for working with databases, for example, “SQLTools – Database tools”. Sublime does not have SQL formatting as standard functionality, but it is easy to add it through Package Control. Several packages are available for Query formatting, and we used SqlBeautifier.
In this case, you need to copy the request to the code editor and save it to a file. It turned out that most of the Percona team use console tools.
Special database management and monitoring software also have functionality for query formatting.
MySQL Workbench enables a DBA or developer to visually, generate, and manage databases.
The Workbench allows you to format an SQL query into a new SQL tab and click the “Beautify/reformat the SQL script” button.
Percona Monitoring and Management (PMM)
Percona Monitoring and Management (PMM) is a free, best-of-breed, open-source database monitoring and management solution. PMM has the useful functionality of PMM Query Analytics, which allows you to immediately view requests in formatted form.
The most popular console tool for formatting queries in our team is sqlparse (sqlformat). The developers of the popular online service SQLFormat were very kind and provided the source code of their excellent tool (sqlparse). This simple and useful tool you can use on different platforms.
If you love Docker and do not want to install the tool on your system. Percona Support Engineer Agustín Gallego has packed the data script into a docker. Here is a simple instruction on how you can use it.
shell> docker pull guriandoro/sqlparse:0.3.1 shell> docker run --rm \ > --network=none \ > guriandoro/sqlparse:0.3.1 "SELECT several, columns from a_table as a join another_table as b where a.id = 1;" SELECT several, columns FROM a_table AS a JOIN another_table AS b WHERE a.id = 1;
Cool, right? 🙂
It’s very simple, install the sqlformat package.
# apt install sqlformat $ echo "SELECT several, columns from a_table as a join another_table as b where a.id = 1;" | sqlformat - -r -k upper SELECT several, columns FROM a_table AS a JOIN another_table AS b WHERE a.id = 1;
Install sqlparse using brew or another way to use the command sqlformat.
# brew install sqlparse # echo "SELECT several, columns from a_table as a join another_table as b where a.id = 1;" | sqlformat - -r -k upper SELECT several, columns FROM a_table AS a JOIN another_table AS b WHERE a.id = 1;
And if you love and use VIM, you can also install sqlformat and use it.
vmap <leader>sql :%!sqlformat - -r -k upper<CR>
We looked at Sqlparse and Sqlformat tools and their use on Linux, Mac, or Windows. We would like to note that it is very convenient for developers to use the Code Editor or IDE, especially if you become a regular user. Additionally, specialized database software such as MySQL Workbench or PMM can help with query formatting. These are SQL query formatting tools that the Percona team has utilized successfully. Share your experience with these or other tools here in the blog comments.
by Daniil Bazhenov via Percona Database Performance Blog