我有一个包含 100k 条记录但没有索引的表。我在用于左连接的列上创建了一个新索引。 我需要重新索引我的表吗?
索引的创建花费了几毫秒。所以我猜测查询不能使用这个索引(没有数据),直到我重新索引我的表(如果我有其他索引,我只会重新索引索引 - 我阅读了手册)。
新索引填充数据时找不到任何信息?这是自动完成的吗?什么时候?
最佳答案
CREATE INDEX
完成后,索引就可以使用了。之后无需运行 REINDEX
。
来自 REINDEX
documentation page :
REINDEX is similar to a drop and recreate of the index in that the index contents are rebuilt from scratch. However, the locking considerations are rather different. REINDEX locks out writes but not reads of the index's parent table.
这意味着 REINDEX
的行为类似于 DROP
之后的 CREATE
。
并且来自 CREATE INDEX
documentation page :
Creating an index can interfere with regular operation of a database. Normally PostgreSQL locks the table to be indexed against writes and performs the entire index build with a single scan of the table. Other transactions can still read the table, but if they try to insert, update, or delete rows in the table they will block until the index build is finished.
我认为这明确地解释了创建意味着索引化。
具体查询是否使用索引取决于许多不同的因素。如果您的查询不使用索引,则需要发布查询、表定义(例如作为 create table
语句)、您定义的索引和 explain (分析,详细)
您的查询。
关于PostgreSQL - 添加新索引时重新索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27619392/