В Django API список транзакций стал отвечать заметно медленнее после роста таблицы до 8 миллионов строк. Проведи по механизму: почему индекс может не помочь и что ты первым проверишь в плане выполнения запроса через `EXPLAIN ANALYZE`.
Asked at
AI answer
First, I would reproduce the slow query with representative parameters and inspect the actual plan, for example in PostgreSQL:
EXPLAIN (ANALYZE, BUFFERS) ...;
ANALYZE actually executes the query, so this is important to keep in mind on a heavily loaded production database. In the plan, I would first compare the planner’s estimates with the actual row counts and time at each node. Then I would check:
- Whether a
Seq Scanis being used instead of an index, and how many rows it filters out. An index may not help if the condition is not very selective and the query returns most of the table; in that case, a sequential scan is cheaper. - Whether there is an
Index Scan/Bitmap Index Scan, how many pages are read, and what theBuffers(hit/read) values are. An index lookup can be slow because of the large number of table accesses needed to fetch rows. - Whether the index is being hindered by an expression or type cast in the condition (
WHERE), a mismatched column order in a composite index, or a sort or join that is not covered by the appropriate index. - How much the estimated
rowsdiffer from the actual row count, how many times a node is executed (loops), and whether there are expensiveSortorJoinoperations, or many repeated accesses caused by a correlated subquery.
If the estimates differ significantly from reality, I would check whether the statistics are up to date (ANALYZE). I would also review the query and its parameters, including pagination and fetching related objects in Django. Simply having an index is not enough: it needs to match the filter, sort order, and actual selectivity of the query.
Also asked at ylab
- Could you tell me about your overall role in this project? Were you fully responsible for it, or were you responsible for a specific part?
- Could you tell about a situation where you made a mistake at work?
- How can you find and analyze slow queries in PostgreSQL?
- 20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. 3. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false
Similar questions
- If the problem turns out to be on the database side and a SELECT query is taking a long time, what would you do? Which columns are indexes usually created on?
- If a service or method takes a long time to run, the problem could be with the Python script or the database. How would you figure out where the slowdown is occurring?
- How do you find and optimize a slow SQL query? What do you look at first in EXPLAIN ANALYZE?
- Have you used EXPLAIN?
- What is the purpose of the EXPLAIN ANALYZE command in a database management system?