Query का Optimization
Optimising का मतलब है measure करना, sensibly index करना, सिर्फ ज़रूरी चीज़ fetch करना, और planner को अच्छी statistics देना।
In this page:
Query Optimization
सबसे slow queries पर EXPLAIN ANALYZE से शुरू करें। सिर्फ ज़रूरी columns select करें, जल्दी filter करें, WHERE में indexed columns पर functions से बचें, OFFSET paging को keyset paging से replace करें, writes batch करें, और ANALYZE से statistics fresh रखें।
Covering indexes (INCLUDE) और partial indexes consider करें। हर change के बाद फिर measure करें।
Note:
एक column पर एक function, जैसे lower(col), col पर एक plain index इस्तेमाल होने से रोकता है।
उदाहरण: Query optimization
-- Slow: function on the column defeats the plain index
shop=# SELECT id FROM users WHERE lower(email) = '[email protected]';
-- Fast: an expression index matches the query
shop=# CREATE INDEX idx_users_lower_email ON users (lower(email));
-- Fast: keyset pagination instead of a huge OFFSET
shop=# SELECT id, title FROM posts WHERE id > 12000 ORDER BY id LIMIT 20;
-- Covering index so PostgreSQL can answer from the index alone
shop=# CREATE INDEX idx_orders_cust ON orders (customer_id) INCLUDE (total);
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- बिना measuring के optimize करना
- SELECT * इस्तेमाल करना
- indexed columns को functions में wrap करना
चैप्टर सारांश
- EXPLAIN ANALYZE से measure करें
- सिर्फ ज़रूरी columns select करें
- indexed columns पर functions से बचें
- ANALYZE से statistics refresh करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: