简体   繁体   English

数据行较大时不使用PostgreSQL索引

[英]PostgreSQL Index not used when data rows are large

Hi I'm curious about why index doesn't work when data rows are large even 100. 嗨,我很好奇为什么数据行很大甚至100行时索引也不起作用。

Here's select for 10 data: 这里选择10个数据:

mydb> explain select * from data where user_id=1;
+-----------------------------------------------------------------------------------+
| QUERY PLAN                                                                        |
|-----------------------------------------------------------------------------------|
| Index Scan using ix_data_user_id on data  (cost=0.14..8.15 rows=1 width=2043) |
| Index Cond: (user_id = 1)                                                         |
+-----------------------------------------------------------------------------------+
EXPLAIN

Here's select for 100 data: 选择100个数据:

mydb> explain select * from data where user_id=1;
+------------------------------------------------------------+
| QUERY PLAN                                                 |
|------------------------------------------------------------|
| Seq Scan on data  (cost=0.00..44.67 rows=1414 width=945) |
| Filter: (user_id = 1)                                      |
+------------------------------------------------------------+
EXPLAIN

How can index work when data rows are 100? 当数据行为100时,索引如何工作?

100 is not a large amount of data. 100不是大量数据。 Think 10,000 or 100,000 rows for a respectable amount. 将10,000或100,000行视为可观的金额。

To put it simply, records in a table are stored on data pages. 简单地说,表中的记录存储在数据页上。 A data page typically has about 8k bytes (it depends on the database and on settings). 数据页通常约有8k字节(取决于数据库和设置)。 A major purpose of indexes is to reduce the number of data pages that need to be read. 索引的主要目的是减少需要读取的数据页的数量。

If all the records in a table fit on one page, there is no need to reduce the number pages being read. 如果表中的所有记录都适合一页,则无需减少正在读取的页数。 The one page will be read. 将读取一页。 Hence, the index may not be particularly useful. 因此,索引可能不是特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM