Junior — Middle
Have you analyzed the execution plan of an SQL query?
sobes.tech AI
Answer from AI
Yes, analyzing the execution plan of an SQL query is an important step in optimization. The execution plan shows how the DBMS intends to execute the query: which indexes to use, in what order to join tables, what filtering and sorting operations to apply.
In Python, you can analyze the plan using, for example, the sqlite3 library with the EXPLAIN QUERY PLAN command or for PostgreSQL through EXPLAIN ANALYZE.
Example with SQLite:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('EXPLAIN QUERY PLAN SELECT * FROM users WHERE age > 30')
plan = cursor.fetchall()
for row in plan:
print(row)
By analyzing the output, you can understand which indexes are used and optimize the query or the database structure.