Indexes
At the back of a textbook, an alphabetical list tells you which page to open for a topic. A database index does the same job for a table: it helps the database go straight to the rows you want.
6 min read6 মিনিটে পড়া যায় · 8 sections8টি সেকশন · 7 sources7টি সোর্স · updated Nov 2026আপডেট: Nov 2026
A lookup with and without an indexইনডেক্স সহ ও ইনডেক্স ছাড়া একটি লুকআপ
Look up one student in a big table, with and without an index, then see what the index costs on writes.একটি বড় টেবিলে একজন স্টুডেন্টকে ইনডেক্স সহ ও ছাড়া খুঁজুন, তারপর দেখুন ইনডেক্সের রাইটে কী খরচ।
You’ll learnআপনি শিখবেন
- Without an index, the database reads every rowইনডেক্স ছাড়া ডেটাবেস প্রতিটি রো পড়ে
- An index is a B-tree next to the tableইনডেক্স টেবিলের পাশের একটি B-tree
- With an index it walks a few levelsইনডেক্স থাকলে কয়েকটি স্তর পার হয়
- Every index costs extra work on writesপ্রতিটি ইনডেক্স রাইটে বাড়তি কাজ করায়
- EXPLAIN shows which plan runsEXPLAIN দেখায় কোন প্ল্যান চলে
In plain words
An index is an extra structure that a database keeps next to a table, so that it can find rows by the value of a column. Without it, the database has to read the table row by row Source 1সোর্স 1. MySQL says the same: it starts at the first row and reads the whole table, and a bigger table means more work Source 5সোর্স 5. With an index, the database can find the right place in the data without looking at all of it Source 5সোর্স 5.
The PostgreSQL manual uses the same book comparison, and says that the database programmer must foresee which indexes will be useful Source 1সোর্স 1.
You create an index with a command such as the one below. You can add or remove an index at any time Source 1সোর্স 1.
CREATE INDEX orders_customer_idx ON orders (customer_id);সহজ English
An index is an extra structure that a database keeps next to a table. The database uses it to find rows by the value of a column. Without an index, the database reads the table row by row Source 1সোর্স 1. MySQL says the same. It starts at the first row and reads the whole table. A bigger table means more work Source 5সোর্স 5. With an index, the database can find the right place in the data without looking at all of it Source 5সোর্স 5.
The PostgreSQL manual uses the same book comparison. It says that the database programmer must foresee which indexes will be useful Source 1সোর্স 1.
You create an index with a command such as CREATE INDEX orders_customer_idx ON orders (customer_id);. You can add or remove an index at any time Source 1সোর্স 1.
বাংলা ব্যাখ্যা
ইনডেক্স (index) হলো একটি বাড়তি কাঠামো, যা ডেটাবেস একটি টেবিলের পাশে রাখে। এর সাহায্যে ডেটাবেস কোনো কলামের মান দিয়ে রো খুঁজে পায়। ইনডেক্স না থাকলে ডেটাবেসকে টেবিলটি রো ধরে ধরে পড়তে হয় Source 1সোর্স 1। MySQL-ও একই কথা বলে: সে প্রথম রো থেকে শুরু করে পুরো টেবিল পড়ে, আর টেবিল বড় হলে কাজ বেশি হয় Source 5সোর্স 5। ইনডেক্স থাকলে ডেটাবেস সব ডেটা না দেখেই ডেটার সঠিক জায়গা খুঁজে পায় Source 5সোর্স 5।
PostgreSQL ম্যানুয়াল একই বইয়ের তুলনা ব্যবহার করে। সেখানে বলা হয়েছে, কোন ইনডেক্স কাজে লাগবে তা ডেটাবেস প্রোগ্রামারকে আগে থেকে ভেবে নিতে হবে Source 1সোর্স 1।
আপনি CREATE INDEX orders_customer_idx ON orders (customer_id); এর মতো একটি কমান্ড দিয়ে ইনডেক্স তৈরি করেন। যেকোনো সময় ইনডেক্স যোগ করা বা মুছে ফেলা যায় Source 1সোর্স 1।
When you need it
You need an index when many queries look for a few rows in a big table, for example "all orders of one customer". An index can also help UPDATE and DELETE commands that have a search condition, and joins Source 1সোর্স 1.
Indexes matter less for small tables, and for reports that read most rows of a big table: reading in order is faster there Source 5সোর্স 5.
সহজ English
You need an index when many queries look for a few rows in a big table. An example is "all orders of one customer". An index can also help UPDATE and DELETE commands that have a search condition. It can help joins too Source 1সোর্স 1.
Indexes matter less for small tables. They also matter less for reports that read most rows of a big table. Reading in order is faster there Source 5সোর্স 5.
বাংলা ব্যাখ্যা
বড় টেবিলে অনেক কুয়েরি যখন অল্প কয়েকটি রো খোঁজে, তখন ইনডেক্স দরকার হয়। যেমন "একজন কাস্টমারের সব অর্ডার"। যেসব UPDATE ও DELETE কমান্ডে সার্চ কন্ডিশন আছে, সেগুলোতেও ইনডেক্স সাহায্য করতে পারে। জয়েনেও সাহায্য করতে পারে Source 1সোর্স 1।
ছোট টেবিলের জন্য ইনডেক্স কম গুরুত্বপূর্ণ। বড় টেবিলের বেশিরভাগ রো পড়ে এমন রিপোর্টের জন্যও তাই, কারণ সেখানে ক্রমানুসারে পড়া দ্রুত Source 5সোর্স 5।
How it works
Most MySQL indexes are stored in B-trees Source 5সোর্স 5. PostgreSQL also creates a B-tree by default Source 2সোর্স 2. With a search tree, the database may need to walk only a few levels Source 1সোর্স 1.
This table shows what some PostgreSQL index types handle Source 2সোর্স 2:
| Type | What it handles |
|---|---|
| B-tree | Equality and range queries on sortable data, such as <, =, >, BETWEEN, and IN |
| Hash | Simple equality only |
A B-tree can help with LIKE 'foo%' (a fixed start), but not with LIKE '%bar' Source 2সোর্স 2.
An index can have more than one column. A multicolumn B-tree index works best when the query limits the first (leftmost) columns Source 3সোর্স 3. In MySQL, an index on (col1, col2, col3) serves a search on col1, on col1, col2, and on all three Source 5সোর্স 5. PostgreSQL says to use multicolumn indexes sparingly Source 3সোর্স 3.
If a query needs only columns that are in an index, the database can take the values from the index tree. This is a covering index Source 5সোর্স 5.
What an index costs
An index is not free:
- Writes. The database must keep the index in step with the table, which adds work to every data change Source 1সোর্স 1.
- Space. An index takes space of its own. The PostgreSQL manual notes that a single-column index "saves space and time" compared with a wider one Source 3সোর্স 3.
- Build time. Building an index on a big table may be slow. During the build, PostgreSQL by default lets reads run but blocks writes Source 1সোর্স 1.
For this reason, the manual says to remove an index that is seldom or never used Source 1সোর্স 1.
সহজ English
Most MySQL indexes use B-trees Source 5সোর্স 5. PostgreSQL also creates a B-tree by default Source 2সোর্স 2. With a search tree, the database may need to walk only a few levels Source 1সোর্স 1.
Some PostgreSQL index types handle different queries Source 2সোর্স 2:
- A B-tree handles equality and range queries on sortable data. Examples are
<,=,>,BETWEEN, andIN. - A hash index handles simple equality only.
A B-tree can help with LIKE 'foo%' (a fixed start). It cannot help with LIKE '%bar' Source 2সোর্স 2.
An index can have more than one column. A multicolumn B-tree index works best when the query limits the first (leftmost) columns Source 3সোর্স 3. In MySQL, an index on (col1, col2, col3) serves a search on col1, on col1, col2, and on all three Source 5সোর্স 5. PostgreSQL says to use multicolumn indexes sparingly Source 3সোর্স 3.
Sometimes a query needs only columns that are in an index. Then the database can take the values from the index tree. This is a covering index Source 5সোর্স 5.
An index is not free. It has three costs:
- Writes. The database must keep the index in step with the table. This adds work to every data change Source 1সোর্স 1.
- Space. An index takes space of its own. The PostgreSQL manual notes that a single-column index "saves space and time" compared with a wider one Source 3সোর্স 3.
- Build time. Building an index on a big table may be slow. During the build, PostgreSQL by default lets reads run but blocks writes Source 1সোর্স 1.
For this reason, the manual says to remove an index that is seldom or never used Source 1সোর্স 1.
বাংলা ব্যাখ্যা
MySQL-এর বেশিরভাগ ইনডেক্স B-tree-তে রাখা হয় Source 5সোর্স 5। PostgreSQL-ও ডিফল্ট হিসেবে B-tree তৈরি করে Source 2সোর্স 2। সার্চ ট্রি থাকলে ডেটাবেসকে হয়তো মাত্র কয়েকটি স্তর পার হতে হয় Source 1সোর্স 1।
PostgreSQL-এর কিছু ইনডেক্স টাইপ বিভিন্ন কুয়েরি সামলায় Source 2সোর্স 2:
- B-tree সাজানো যায় এমন ডেটার ওপর ইকুয়ালিটি (equality) ও রেঞ্জ কুয়েরি সামলায়। যেমন
<,=,>,BETWEENওIN। - Hash ইনডেক্স শুধু সাধারণ ইকুয়ালিটি সামলায়।
B-tree LIKE 'foo%' (শুরুর অংশ নির্দিষ্ট) এ সাহায্য করতে পারে, কিন্তু LIKE '%bar' এ পারে না Source 2সোর্স 2।
একটি ইনডেক্সে একাধিক কলাম থাকতে পারে। কুয়েরি প্রথম (leftmost, অর্থাৎ একেবারে বাঁ দিকের) কলামগুলোকে সীমিত করলে মাল্টিকলাম B-tree ইনডেক্স সবচেয়ে ভালো কাজ করে Source 3সোর্স 3। MySQL-এ (col1, col2, col3) এর ইনডেক্স col1 দিয়ে, col1, col2 দিয়ে এবং তিনটি দিয়েই সার্চ সামলায় Source 5সোর্স 5। PostgreSQL বলে মাল্টিকলাম ইনডেক্স কম ব্যবহার করতে Source 3সোর্স 3।
কখনো একটি কুয়েরির দরকারি সব কলাম ইনডেক্সেই থাকে। তখন ডেটাবেস ইনডেক্স ট্রি থেকেই মান নিতে পারে। একে কভারিং ইনডেক্স (covering index) বলে Source 5সোর্স 5।
ইনডেক্স বিনামূল্যে পাওয়া যায় না। এর তিনটি খরচ আছে:
- রাইট (write)। ডেটাবেসকে ইনডেক্স আর টেবিল একসাথে ঠিক রাখতে হয়। এতে প্রতিটি ডেটা পরিবর্তনে কাজ বাড়ে Source 1সোর্স 1।
- জায়গা। ইনডেক্স নিজের জন্য জায়গা নেয়। PostgreSQL ম্যানুয়াল বলে, আরও চওড়া ইনডেক্সের তুলনায় সিঙ্গেল-কলাম ইনডেক্স "saves space and time" Source 3সোর্স 3।
- তৈরির সময়। বড় টেবিলে ইনডেক্স তৈরি ধীর হতে পারে। তৈরি চলার সময় PostgreSQL ডিফল্টভাবে রিড চলতে দেয়, কিন্তু রাইট আটকে দেয় Source 1সোর্স 1।
এই কারণে ম্যানুয়াল বলে, যে ইনডেক্স কদাচিৎ বা কখনোই ব্যবহার হয় না, তা মুছে ফেলতে Source 1সোর্স 1।
A real-life example
A learning platform has an enrollments table with 5 million rows. The page "My courses" runs SELECT * FROM enrollments WHERE student_id = 42. This is the tutorial's own example, and the numbers are not from a source.
Without an index on student_id, the database reads the table for each page view. With the index, it goes to the few rows of student 42, and the page gets faster. But every new enrollment now also updates the index, so a bulk import has more work to do. With ten more indexes added "just in case", every import pays for all of them.
সহজ English
A learning platform has an enrollments table with 5 million rows. The page "My courses" runs SELECT * FROM enrollments WHERE student_id = 42. This is the tutorial's own example. The numbers are not from a source.
Without an index on student_id, the database reads the table for each page view. With the index, it goes to the few rows of student 42. The page gets faster. But now every new enrollment also updates the index. So a bulk import has more work to do. Suppose you add ten more indexes "just in case". Then every import pays for all of them.
বাংলা ব্যাখ্যা
একটি লার্নিং প্ল্যাটফর্মের enrollments টেবিলে 5 million রো আছে। "My courses" পেজ SELECT * FROM enrollments WHERE student_id = 42 চালায়। এটি টিউটোরিয়ালের নিজস্ব উদাহরণ, আর সংখ্যাগুলো কোনো সোর্স থেকে নেওয়া নয়।
student_id-তে ইনডেক্স না থাকলে ডেটাবেস প্রতিটি পেজ ভিউয়ের জন্য টেবিল পড়ে। ইনডেক্স থাকলে ডেটাবেস স্টুডেন্ট 42-এর অল্প কয়েকটি রো-তে সোজা চলে যায়, আর পেজ দ্রুত হয়। কিন্তু এখন প্রতিটি নতুন এনরোলমেন্ট ইনডেক্সকেও আপডেট করে। তাই বাল্ক ইমপোর্টে কাজ বেশি হয়। ধরুন আপনি "যদি লাগে" ভেবে আরও দশটি ইনডেক্স যোগ করলেন। তখন প্রতিটি ইমপোর্টকে সবগুলোর খরচ দিতে হয়।
Common mistakes
- Indexing every column. Each index adds work on writes Source 1সোর্স 1 and takes space.
- Expecting a multicolumn index to help every query. A query that skips the leftmost column may get little help. PostgreSQL 18 and later can use a skip scan when the leftmost column has few distinct values Source 3সোর্স 3.
- Comparing columns of different types. In MySQL, a comparison between two string columns with different character sets (for example
utf8mb4andlatin1) rules out an index Source 5সোর্স 5. - Using a leading wildcard.
LIKE '%bar'cannot use a B-tree Source 2সোর্স 2. The database uses an index when it thinks that is more efficient than a full scan Source 1সোর্স 1.
সহজ English
- Indexing every column. Each index adds work on writes Source 1সোর্স 1. Each index also takes space.
- Expecting a multicolumn index to help every query. A query that skips the leftmost column may get little help. PostgreSQL 18 and later can use a skip scan when the leftmost column has few distinct values Source 3সোর্স 3.
- Comparing columns of different types. In MySQL, a comparison between two string columns with different character sets rules out an index. An example is
utf8mb4andlatin1Source 5সোর্স 5. - Using a leading wildcard.
LIKE '%bar'cannot use a B-tree Source 2সোর্স 2. The database uses an index when it thinks that is more efficient than a full scan Source 1সোর্স 1.
বাংলা ব্যাখ্যা
- প্রতিটি কলামে ইনডেক্স দেওয়া। প্রতিটি ইনডেক্স রাইটে কাজ বাড়ায় Source 1সোর্স 1, আর জায়গাও নেয়।
- ভাবা যে মাল্টিকলাম ইনডেক্স প্রতিটি কুয়েরিতে সাহায্য করবে। যে কুয়েরি লেফটমোস্ট কলাম বাদ দেয়, সে হয়তো খুব কম সাহায্য পায়। PostgreSQL 18 ও তার পরের ভার্সন লেফটমোস্ট কলামে আলাদা মান কম থাকলে স্কিপ স্ক্যান ব্যবহার করতে পারে Source 3সোর্স 3।
- ভিন্ন টাইপের কলাম তুলনা করা। MySQL-এ ভিন্ন ক্যারেক্টার সেটের দুটি স্ট্রিং কলাম তুলনা করলে ইনডেক্স ব্যবহার হয় না। যেমন
utf8mb4আরlatin1Source 5সোর্স 5। - শুরুতে ওয়াইল্ডকার্ড ব্যবহার করা।
LIKE '%bar'B-tree ব্যবহার করতে পারে না Source 2সোর্স 2। ডেটাবেস যখন মনে করে ইনডেক্স ফুল স্ক্যানের চেয়ে বেশি কার্যকর, তখন সে ইনডেক্স ব্যবহার করে Source 1সোর্স 1।
How to see if a query uses an index
Put EXPLAIN before the query to see the plan that the database chose Source 7সোর্স 7.
In PostgreSQL, the plan is a tree of nodes. The bottom nodes are scan nodes, and a table can be read by a sequential scan, an index scan, or a bitmap index scan Source 4সোর্স 4. A Seq Scan with a Filter checks the condition on each row it reads Source 4সোর্স 4. An Index Scan shows an Index Cond line Source 4সোর্স 4.
The cost numbers are estimates Source 4সোর্স 4. With EXPLAIN ANALYZE, PostgreSQL executes the statement, not only plans it Source 7সোর্স 7, then shows the true row counts and times next to the estimates Source 4সোর্স 4. The manual says that a key check is whether the estimated row counts are near the real ones Source 4সোর্স 4.
In MySQL, read two columns of the EXPLAIN result. key is the index that MySQL decided to use, and NULL means it found no index to use Source 6সোর্স 6. If possible_keys is NULL, no index is relevant Source 6সোর্স 6. The join type ALL is a full table scan, which is normally not good for the first table that is not const, and usually very bad for later tables Source 6সোর্স 6. To list the indexes of a table, use SHOW INDEX FROM tbl_name Source 6সোর্স 6.
সহজ English
Put EXPLAIN before the query. It shows the plan that the database chose Source 7সোর্স 7.
In PostgreSQL, the plan is a tree of nodes. The bottom nodes are scan nodes. A table can be read by a sequential scan, an index scan, or a bitmap index scan Source 4সোর্স 4. A Seq Scan with a Filter checks the condition on each row it reads Source 4সোর্স 4. An Index Scan shows an Index Cond line Source 4সোর্স 4.
The cost numbers are estimates Source 4সোর্স 4. With EXPLAIN ANALYZE, PostgreSQL executes the statement. It does not only plan it Source 7সোর্স 7. Then it shows the true row counts and times next to the estimates Source 4সোর্স 4. The manual says that a key check is whether the estimated row counts are near the real ones Source 4সোর্স 4.
In MySQL, read two columns of the EXPLAIN result. The column key is the index that MySQL decided to use. NULL means it found no index to use Source 6সোর্স 6. If possible_keys is NULL, no index is relevant Source 6সোর্স 6. The join type ALL is a full table scan. This is normally not good for the first table that is not const, and usually very bad for later tables Source 6সোর্স 6. To list the indexes of a table, use SHOW INDEX FROM tbl_name Source 6সোর্স 6.
বাংলা ব্যাখ্যা
কুয়েরির আগে EXPLAIN লিখুন। এটি ডেটাবেস যে প্ল্যান বেছে নিয়েছে তা দেখায় Source 7সোর্স 7।
PostgreSQL-এ প্ল্যান হলো নোডের একটি ট্রি। নিচের নোডগুলো স্ক্যান নোড। একটি টেবিল sequential scan, index scan বা bitmap index scan দিয়ে পড়া যায় Source 4সোর্স 4। Filter সহ একটি Seq Scan যে প্রতিটি রো পড়ে, তাতে কন্ডিশন যাচাই করে Source 4সোর্স 4। একটি Index Scan-এ Index Cond লাইন দেখা যায় Source 4সোর্স 4।
কস্টের সংখ্যাগুলো আন্দাজ Source 4সোর্স 4। EXPLAIN ANALYZE দিলে PostgreSQL স্টেটমেন্ট শুধু প্ল্যান করে না, চালায়ও Source 7সোর্স 7। তারপর আন্দাজের পাশে সত্যিকারের রো সংখ্যা ও সময় দেখায় Source 4সোর্স 4। ম্যানুয়াল বলে, একটি মূল পরীক্ষা হলো আন্দাজ করা রো সংখ্যা আসল সংখ্যার কাছাকাছি কি না Source 4সোর্স 4।
MySQL-এ EXPLAIN ফলাফলের দুটি কলাম পড়ুন। key হলো সেই ইনডেক্স, যেটি MySQL ব্যবহার করবে বলে ঠিক করেছে। NULL মানে ব্যবহার করার মতো কোনো ইনডেক্স সে পায়নি Source 6সোর্স 6। possible_keys NULL হলে কোনো ইনডেক্স প্রাসঙ্গিক নয় Source 6সোর্স 6। জয়েন টাইপ ALL মানে ফুল টেবিল স্ক্যান। প্রথম যে টেবিলটি const নয়, তার জন্য এটি সাধারণত ভালো নয়, আর পরের টেবিলগুলোর জন্য সাধারণত খুবই খারাপ Source 6সোর্স 6। একটি টেবিলের ইনডেক্সগুলো দেখতে SHOW INDEX FROM tbl_name ব্যবহার করুন Source 6সোর্স 6।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Test with a realistic amount of data. With 50 rows, every query is fast and a missing index stays hidden. Use a data set as large as production.
- Run
EXPLAINon the slow query. Look for a full scan on a big table, and forkey: NULLin MySQL. - Compare before and after. Check the plan and the time before and after you add the index.
- Measure the write side too. Time a bulk insert or update with and without the index.
- Be careful with
EXPLAIN ANALYZEonUPDATEorDELETE. It really runs the query, so use a copy of the data.
সহজ English
These test ideas are the tutorial's own advice.
- Test with a realistic amount of data. With 50 rows, every query is fast and a missing index stays hidden. Use a data set as large as production.
- Run
EXPLAINon the slow query. Look for a full scan on a big table. In MySQL, look forkey: NULL. - Compare before and after. Check the plan and the time before and after you add the index.
- Measure the write side too. Time a bulk insert or update with and without the index.
- Be careful with
EXPLAIN ANALYZEonUPDATEorDELETE. It really runs the query, so use a copy of the data.
বাংলা ব্যাখ্যা
এই টেস্টের ধারণাগুলো টিউটোরিয়ালের নিজস্ব পরামর্শ।
- বাস্তবসম্মত পরিমাণ ডেটা দিয়ে টেস্ট করুন। 50টি রো থাকলে প্রতিটি কুয়েরি দ্রুত হয়, আর না-থাকা ইনডেক্স লুকিয়ে থাকে। প্রোডাকশনের সমান বড় ডেটা সেট ব্যবহার করুন।
- ধীর কুয়েরিতে
EXPLAINচালান। বড় টেবিলে ফুল স্ক্যান খুঁজুন। MySQL-এkey: NULLখুঁজুন। - আগে ও পরে তুলনা করুন। ইনডেক্স যোগ করার আগে ও পরে প্ল্যান আর সময় দেখুন।
- রাইটের দিকটাও মাপুন। ইনডেক্স সহ ও ইনডেক্স ছাড়া একটি বাল্ক ইনসার্ট বা আপডেটের সময় মাপুন।
UPDATEবাDELETE-এEXPLAIN ANALYZEসাবধানে ব্যবহার করুন। এটি কুয়েরি সত্যিই চালায়, তাই ডেটার একটি কপি ব্যবহার করুন।
Key words
| Word | Meaning |
|---|---|
| Index | An extra structure that helps the database find rows by column value without reading every row Source 5সোর্স 5 |
| Full table scan | Reading the whole table, row by row Source 1সোর্স 1 |
| B-tree | The default index type of PostgreSQL, and the type of most MySQL indexes Source 2সোর্স 2 Source 5সোর্স 5 |
| Multicolumn index | One index on several columns, best when the query limits the leftmost ones Source 3সোর্স 3 |
| Covering index | An index that holds all the columns a query needs Source 5সোর্স 5 |
| EXPLAIN | A command that shows the plan of a query Source 4সোর্স 4 |