Sobes.tech
Junior — Middle

Kas saaksite selgitada EXPLAIN käsu eesmärki PostgreSQL-is ja kuidas see aitab päringuid optimeerida?

sobes.tech AI

Vastus AI-lt

The EXPLAIN command in PostgreSQL is used to obtain the execution plan of an SQL query. It shows how the database plans to execute the query — which indexes will be used, how tables will be joined, how many rows are expected to be processed, etc.

This helps optimize queries by identifying bottlenecks, such as:

  • Lack of indexes, leading to full table scans.
  • Incorrect order of table joins.
  • Redundant sorting or aggregation operations.

Example of usage:

EXPLAIN SELECT * FROM users WHERE email = 'example@mail.com';

The result will show whether an index on the email field will be used or a sequential scan will be performed.

For more detailed analysis, you can use EXPLAIN ANALYZE, which executes the query and shows the actual time costs.