Project: YDB (App Definition and Development / Database)
File: query-plans-optimization.md

Before executing a query, it is essential to analyze its execution plan to detect and eliminate the reasons for possible excessive cluster resource consumption or abnormally high execution time. This article will discuss specific examples of query plan analysis. Let's consider the following query that searches for episodes by title: ``` sql SELECT seasonid, episodeid FROM episodes WHERE title = 'The Work Outing' ``` Schema of the `episodes` table: Let's build a plan for this query. You can do this via either UI or {{ ydb-short-name }} CLI: {% list tabs %} {{ ydb-short-name }} CLI You can build a query plan via {{ ydb-short-name }} using the following command: ``` ydb -p <profile_name> table query explain \ -q "SELECT seasonid, episodeid FROM episodes WHERE title = 'The Work Outing'" ``` Result: ``` Query Plan: ResultSet Limit (Limit: 1001) <UnionAll> Limit (Limit: 1001) Filter (Predicate: item.title == "The Work Outing") TableFullScan (ReadRanges: ["seriesid (-, +)","seasonid (-, +)","episodeid (-, +)"], ReadColumns: ["episodeid","season_id","title"], Table: episodes) Tables: ["episodes"] ``` Embedded UI You can also build a query plan via . You need to navigate to the database page, go to the `Query` section, type the query text, and click on `Explain`: Result: {% endlist %} Both plan representations contain the result being returned to the client at the root, table operations at the leaves, and data transformations at the intermediate nodes. It is important to pay attention to the node containing the table reading operation. In this case, it is a `TableFullScan` for the `episodes` table. Full table scans consume time and resources proportional to the size of the table, so it is advisable to avoid them whenever possible in tables that tend to grow over time or are simply large. One typical approach to avoid full scans is using a . In this case, it makes sense to add a secondary index for the column `title` using the following query: ``` sql ALTER TABLE episodes ADD INDEX title_index GLOBAL ON (title) ``` Please note that this example uses . Building an index in {{ ydb-short-name }} is an asynchronous operation. Even if the index creation query is successful, it is advisable to wait for some time because the index may not be ready for use immediately. You can manage asynchronous operations through the . Let's build the query plan using the secondary index `title_index`. Secondary indexes to be used need to be explicitly specified in the `VIEW` clause. {% list tabs %} {{ ydb-short-name }} CLI Command: ``` ydb -p <profile_name> table query explain \ -q "SELECT seasonid, episodeid FROM episodes VIEW title_index WHERE title = 'The Work Outing'" ``` Result: ``` Query Plan: ResultSet Limit (Limit: 1001) <UnionAll> Limit (Limit: 1001) Filter (Predicate: Exist(item.title)) TablePointLookup (ReadRange: ["title (The Work Outing)","seriesid (-, +)","seasonid (-, +)","episodeid (-, +)"], ReadLimit: 1001, ReadColumns: ["episodeid","seasonid","title"], Table: episodes/titleindex/indexImplTable) Tables: ["episodes/title_index/indexImplTable"] ``` Embedded UI Result: {% endlist %} The secondary index allowed the query to be executed without fully scanning the main table. Instead of a `TableFullScan,` we received a `TablePointLookup`reading the index table by key. We no longer need to read the main table because all necessary columns are contained in the index table.