SQL cells

A new SQL cell contains the SQL query editor and control panel. After the query is executed, query execution statistics (QUERY STATS) and query results panel (or the Error panel, if an error occurred during the query execution) are added to the cell.

SQL cell
SQL cell
SQL cell
SQL cell

When created, a new notebook contains one SQL cell.

Query text

The SQL query editor provides the following functionality:

  • SQL syntax highlighting.

  • Line numbering (can be enabled/disabled in the settings, see below). Clicking a line number selects the entire line.

  • Automatic closing of brackets and quotation marks as you type (controlled by the corresponding setting).

  • Automatic increasing/decreasing the editor based on the size of the SQL code. The editor can also be resized manually by dragging its bottom border. Double-clicking the resize handle at the bottom of the editor returns it to the size determined automatically depending on the size of the query text.

SQL query editor
SQL query editor
SQL query editor
SQL query editor

Beautify SQL

Use the beautify sql icon dark beautify sql icon light Beautify SQL icon to automatically make your query code more readable as follows:

  • use consistent indents and spaces to structure the code logically;

  • place major clauses (such as SELECT, FROM, WHERE, JOIN, etc.) on separate lines;

  • use uppercase for all SQL keywords (for example, SELECT, AS, FROM, GROUP BY, ORDER BY);

  • break long lists of columns to be selected into lines — one column per line.

NOTE

Beautify SQL applies formatting to the query text based on the results of EXPLAIN SYNTAX, so the ClickHouse server should be available for this functionality to work.

Use the Beautify SQL option
Use the Beautify SQL option
Use the Beautify SQL option
Use the Beautify SQL option
Example of applying Beautify SQL

 

The code of the query shown in the image above, after applying the Beautify SQL option, will look like the following.

Formatted query text
Formatted query text
Отформатированный текст запроса
Formatted query text

Query editor settings

For the query text editor in an SQL cell, you can configure the following parameters (to open the window with settings, click the settings icon dark settings icon light Settings icon in the upper right corner of the interface):

  • Font Size — font size (the setting does not affect text cells);

  • Word Wrap — enables/disables line wrapping (in text cells, line wrapping is always enabled, regardless of this setting);

  • Line Numbers — enables/disables line numbering (in text cells, row numbering can only be enabled for editing mode);

  • Auto-Close Brackets & Quotes — enables/disables automatic closing of brackets and quotation marks as you type.

Settings
Settings
Settings
Settings

Variables in queries

In the VARIABLES panel, you can add variables as key/value pairs. In a query, you can reference a variable by a key using the {{variable_name}} format — this entry will be automatically replaced with the variable’s value before submitting the query to be executed. Variables use case-insensitive matching (for example, {{MyVar}}, {{myvar}}, {{MYVAR}} all match the same variable).

Use a variable in a query
Use a variable in a query
Use a variable in a query
Use a variable in a query

The VARIABLES panel header displays a count of added variables on the right. If you add a variable with a duplicate name, the corresponding fields are highlighted in red, the variable count is not incremented, and the value of the duplicate variable is ignored.

Control panel

The SQL cell control panel, located above the query text editor, contains the following elements:

  • drag to order icon dark drag to order icon light Drag to reorder — drag and drop to move a cell relative to others within a notebook.

  • [n] — monotonically increasing cell creation identifier that does not change when the cell is moved or deleted.

  • settings icon dark settings icon light Query settings — display settings that allow specifying limits to prevent the execution of a potentially resource-intensive query. Available settings:

    • max_memory_usage — maximum amount of RAM for executing a query on a server;

    • max_execution_time — maximum query execution time;

    • max_threads — maximum number of threads that can be used simultaneously during query execution;

    • join_algorithm — algorithm to perform JOINs.

    These settings (except for the join algorithm) can also be specified for all queries at the notebook level. However, limits set within an SQL cell have a higher priority.

  • beautify sql icon dark beautify sql icon light Beautify SQL — apply automatic formatting to the query text.

  • Query execution status:

    query status grey icon dark query status grey icon light — query has not been executed yet;

    query status yellow icon dark query status yellow icon light — query is currently being executed;

    query status green icon dark query status green icon light — query has been executed successfully;

    query status red icon dark query status red icon light — query execution has been failed.

    For a query that has been executed (successfully or with an error), the time the query was last run is also shown next to the status indicator.

  • run icon Run — start query execution.

  • toggle query icon dark toggle query icon light Toggle query — collapse/expand the query code editor (if the editor is collapsed, the beginning of the query text is displayed within the control panel).

  • expand all queries icon dark expand all queries icon light Fullscreen — expand an SQL cell to full screen (or collapse it).

  • copy query icon dark copy query icon light Duplicate cell — copy a cell.

  • move up icon dark move up icon light Move up — move a cell one position above the previous one.

  • move down icon dark move down icon light Move down — move a cell one position below the next one.

  • add cell below icon dark add cell below icon light Add cell below — create a new cell of the same type below.

  • delete cell icon dark delete cell icon light Delete cell — delete a cell.

SQL cell control panel
SQL cell control panel
SQL cell control panel
SQL cell control panel

Query execution

To run a query, do one of the following:

  • Click run icon Run in the control panel of the corresponding SQL cell. While a query is running, the Run button visually changes to Stop — clicking it cancels the query execution (KILL QUERY is sent).

  • Click an SQL cell to select it (a selected cell is highlighted in green) and press CTRL+ENTER (or CMD+ENTER).

Run a query
Run a query
Run a query
Run a query

In the ADQM Notebook interface, multiple SQL cells can execute their queries simultaneously when running individually. The run all icon command executes queries in cells sequentially from top to bottom waiting for each query to complete before running the next one.

Run all queries in a notebook
Run all queries in a notebook
Run all queries in a notebook
Run all queries in a notebook

Using the Max Rows per Query parameter in ADQM Notebook’s settings, you can set the maximum number of rows in query results for a cell. When the limit is reached, a query is interrupted (KILL QUERY is sent), and the results are marked as truncated.

Query results panel

After executing a query, the results are displayed in the results panel, which contains the Table and Chart tabs, where you can view the resulting data as a grid or chart, respectively. The results panel also contains:

  • general information on the query results (number of received data rows and execution time);

  • Search results…​ field, which allows you to filter data in the result set in real time by any column — the panel will show only the values ​​that match the specified filter;

  • download icon dark download icon light Download icon, which you can use to save the query results in one of the supported formats (CSV, TSV, JSON, JSONLines, Parquet).

    CSV, TSV, JSON, and JSONLines are generated on the client side from cached results. Parquet re-runs the entire query on the ADQMDB server because it requires server-side format generation. This means:

    • A query may return different results if data changed.

    • Additional server load is possible.

The results panel can be collapsed/expanded with the icon toggle query icon dark toggle query icon light in the upper left corner.

Query results panel
Query results panel
Query results panel
Query results panel

To clear the query results of all SQL cells in a notebook, open the Clear context menu in the notebook control panel and select Clear Results.

Clear results of queries
Clear results of queries
Clear results of queries
Clear results of queries

Error panel

If an error occurs during query execution, a corresponding message is displayed in the Error panel (instead of the results panel).

Query execution error
Query execution error
Query execution error
Query execution error

Results table

After executing a query, an SQL cell displays the resulting data set on the Table tab as an interactive data grid, which supports the following features for easy viewing of the received data:

  • Displaying column names and data types in headers.

  • Sorting data by clicking any column header.

  • Data search — filtering values in the result set by any column in real time using the Search results…​ field.

  • Smart formatting:

    • numeric columns are aligned to the right, text columns are aligned to the left;

    • URLs are automatically detected and converted into clickable links;

    • NULL values are visually different from others;

    • JSON data is formatted;

    • arrays and JSON objects can be collapsed/expanded (the default behavior is controlled by the Expand JSON/Arrays by default option in the interface settings — this option affects the display of the results of newly executed queries; already displayed results will not be collapsed/expanded when the option is switched);

    • summary information on columns as labels in headers: the proportion of NULL values ​​in a column (if 0% — a green label, if more than 0% — a red label), the number of unique values ​​in a column, the range of values ​​in numeric and DateTime columns.

Query results table
Query results table
Query results table
Query results table
NOTE

The maximum number of cells (rows * columns) per result set is 100000 to prevent browser crashes.

Query results chart

You can also view the numerical results of your queries as charts. To do this, open the Chart tab in the results panel and configure the following parameters to build a chart:

  • TYPE — chart type (line icon dark line icon light Line, area icon dark area icon light Area, or bar icon dark bar icon light Bar);

  • X-AXIS — a column whose values will be used for labels on the X axis;

  • Y-AXIS — one or more columns whose values will be used for labels on the Y axis.

On both axes, a chart is built between the minimum and maximum values ​​from the query results.

Query results as charts
Query results as charts
Query results as charts
Query results as charts

A chart displays a maximum of 2000 points due to downsampling. If downsampling is applied, the note downsampled to N of M points is shown, while the results table still contains all rows.

Query execution statistics

If query logging is enabled in ADQM (the query_log parameter is activated), an SQL cell displays the QUERY STATS panel with the following information about the query execution:

  • Duration — query execution time;

  • Read — total number of rows read from all tables and table functions involved in a query;

  • Scanned — total number of bytes read from all tables and table functions involved in a query;

  • Memory — memory used by a query;

  • Threads — peak number of threads used simultaneously during query execution;

  • Node Breakdown — information about query execution on each host (when a query is executed in an ADQM cluster);

  • Details — detailed information including: query kind (SELECT, INSERT, etc.), number of rows written (for write queries), bytes written, number of rows in query results, size of query results in bytes, and names of databases/tables/columns accessed by the query.

QUERY STATS panel
QUERY STATS panel
QUERY STATS panel
QUERY STATS panel

Statistics are displayed automatically after executing a query if the Show Query Statistics option is enabled in the ADQM Notebook interface settings. If this option is disabled (by default), click Show Query Stats in the panel to view statistics.

Fetch query statistics
Fetch query statistics
Fetch query statistics
Fetch query statistics

To select a cluster for receiving query statistics, use the Cluster for Statistics setting. If no clusters are defined in the ADQM configuration, this setting is fixed at Single node.

Settings for query statistics
Settings for query statistics
Settings for query statistics
Settings for query statistics
Found a mistake? Seleсt text and press Ctrl+Enter to report it