简体   繁体   English

Postgresql,每次更新索引或结构后手动使用analyze $table_name 来更新查询计划器? 真的吗?

[英]Postgresql, manually use analyze $table_name after each update both on indexes or on structure to update query planner? Really?

I have been trying queries and indexes for two days to find the fastest solution and only today I discover thanks to this answer that using the " analyze $table_name; " command the EXPLAIN and then the query changes completely after adding or removing indexes.我一直在试图查询和索引了两天,找到最快的解决方案,只有今天我发现由于这个答案,在基于“ analyze $table_name; ”命令EXPLAIN ,然后将查询添加或删除索引后完全改变。

My work consists of this:我的工作包括:

  1. use Potgresql 13使用 Potgresql 13
  2. use a database that is as similar as possible to the production ones, both in terms of data type and quantity使用在数据类型和数量方面与生产数据库尽可能相似的数据库
  3. write a query that will be generated and used by the backend language编写一个将由后端语言生成和使用的查询
  4. try various indices (I am also learning Postgresql in the meantime)尝试各种索引(同时我也在学习 Postgresql)
  5. add one index and try the EXPLAIN添加一个索引并尝试 EXPLAIN
  6. I still see the Sequential Scan despite the new index尽管有新索引,但我仍然看到Sequential Scan
  7. so since the index is not used I proceed to understand better and find another solution所以由于没有使用索引,我继续更好地理解并找到另一个解决方案
  8. EXCEPT ONLY TODAY I realized that it was enough to use " analyze $table_name; " to update the Postgresql query planning除了今天,我意识到使用“ analyze $table_name; ”来更新 Postgresql 查询计划就足够了

QUESTIONS问题

  1. Is this something I always have to do?这是我总是必须做的事情吗?

  2. Can I set it automatically so that after each modification on the indexes or on the structure under development it is automatically analyzed?我可以自动设置它以便在每次修改索引或正在开发的结构后自动分析吗?

  3. Is this analysis performed automatically in production?此分析是否在生产中自动执行?

Indexes do not have statistics, they rely on the statistics of the same column(s) from the underlying table.索引没有统计信息,它们依赖于基础表中相同列的统计信息。 The exception is functional/expressional indexes, which do have their own statistics since they invent new columns not in the underlying table.例外是功能/表达式索引,它们确实有自己的统计信息,因为它们创建了不在基础表中的新列。

Unless the index you created was an expresional index (or your stats were already out of date in the first place), there is no reason to analyze the table after the index creation.除非您创建的索引是一个表达式索引(或者您的统计数据首先已经过时),否则没有理由在创建索引后分析表。

There is no mechanism to automatically do an analyze after the creation of an expressional index.没有在创建表达式索引后自动进行分析的机制。 Even if there were, you would probably want to do it manually anyway, as you would like to ensure it has completed before you proceed to testing.即使有,您也可能希望手动完成,因为您希望在继续测试之前确保它已完成。

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

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