简体   繁体   English

Postgres索引?

[英]Postgres Indexing?

I am a newbie in postgres. 我是postgres的新手。 I have a column named host (string varchar2) in a table which has around 20 million rows. 我在一个表中有一个名为host (string varchar2)的列,该表有大约2000万行。 How do I use indexing to optimize my search to find particular host. 如何使用索引来优化搜索以查找特定主机。 Also, this column will be updated daily do I need to write trigger indexing at particular interval? 此外,此列将每天更新我需要在特定时间间隔写入触发器索引吗? If yes, how do I do that? 如果是的话,我该怎么做? (For Records I am using Ruby and Rails 3) (对于我使用Ruby和Rails 3的记录)

Assuming you're doing exact matches, you should just be able to create the index and leave it: 假设您正在进行完全匹配,您应该只能创建索引并保留它:

CREATE INDEX host_index ON table_name (host)

The query optimizer should just use that automatically. 查询优化器应该自动使用它。

You may wish to specify other options such as the collation to use. 您可能希望指定其他选项,例如要使用的排序规则。

See the PostgreSQL docs for CREATE INDEX for more information. 有关更多信息,请参阅CREATE INDEXPostgreSQL文档

我建议使用BRIN Index因为它是从PostgreSQL 9.5引入的,而不是传统的btree索引。

For text search, it is recommended that you use GIN or GiST index types. 对于文本搜索,建议您使用GIN或GiST索引类型。

https://www.postgresql.org/docs/9.5/static/textsearch-indexes.html https://www.postgresql.org/docs/9.5/static/textsearch-indexes.html

Another possibility is that if you were only performing exact matching in the host column, ie, no inequality comparisons ( > , < ) and partial matching ( like , wildcard) involved, you may consider converting host to a hash integer to speed up the search significantly. 另一种可能性是,如果您只在host列中执行精确匹配,即不涉及不等式比较( >< )和部分匹配( like通配符),您可以考虑将host转换为哈希整数以加快搜索速度显著。

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

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