← Back to PostgreSQL Course | Chapter 9: Indexes & Performance | Lesson 1 of 7

CREATE INDEX से Index बनाना

एक index एक extra sorted structure है जो PostgreSQL को पूरी table scan किए बिना rows ढूंढने देता है।

In this page:

  1. CREATE INDEX
Syntax
sql
CREATE INDEX index_name ON table_name (column);
CREATE UNIQUE INDEX index_name ON table_name (column1, column2);

CREATE INDEX

CREATE INDEX name ON table (column) default से एक B-tree index बनाता है। UNIQUE indexes uniqueness enforce करते हैं, multicolumn indexes कई columns cover करते हैं, और CREATE INDEX CONCURRENTLY writes block किए बिना बनाता है।

Indexes reads तेज़ करते हैं लेकिन writes में cost add करते हैं और disk space इस्तेमाल करते हैं। Primary keys और unique constraints automatically indexes बनाते हैं।

Note: busy production tables पर CREATE INDEX CONCURRENTLY इस्तेमाल करें।

उदाहरण: CREATE INDEX

sql
CREATE TABLE emails (id INTEGER PRIMARY KEY, address TEXT, domain TEXT);
INSERT INTO emails VALUES (1,'[email protected]','x.com'),(2,'[email protected]','y.org'),(3,'[email protected]','x.com');
CREATE INDEX idx_emails_domain ON emails (domain);
CREATE UNIQUE INDEX idx_emails_address ON emails (address);
-- Queries look the same; the planner decides whether the index helps
SELECT address FROM emails WHERE domain = 'x.com' ORDER BY id;
SELECT COUNT(*) AS total FROM emails;

-- Output:
-- address
-- [email protected]
-- [email protected]
-- total
-- 3
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. #}
आम गलतियां
  1. हर column index करना
  2. foreign key columns भूल जाना
  3. यह कभी check न करना कि index इस्तेमाल होता है या नहीं
चैप्टर सारांश
  • CREATE INDEX name ON table (column) syntax इस्तेमाल होता है
  • UNIQUE uniqueness enforce करता है
  • CONCURRENTLY writes block होने से बचाता है
  • Indexes writes और space cost करते हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.