How to manually update the statistics data of tables in PostgreSQL

我们两清 提交于 2019-12-11 13:53:03

问题


The ANALYZE statement can be used in PostgreSQL to collect the statistics data of tables. However, I do not want to actually insert these data into tables, I just need to evaluate the cost of some queries, is there anyway to manually specify the statistics data of tables in PostgreSQL without actually putting data into it?


回答1:


I think you are muddling ANALYZE with EXPLAIN ANALYZE. There are different things.

If you want query costs and timing without applying the changes, the only real option you have is to begin a transaction, execute the query under EXPLAIN ANALYZE, and then ROLLBACK.

This still executes the query, meaning that:

  • CPU time and I/O are consumed
  • Locks are still taken and held for the duration
  • New rows are actually written to the tables and indexes, but are never marked visible. They are cleaned up in the next VACUUM.



回答2:


You can already EXPLAIN ANALYSE a query even with no inserted data, it will help you to get a feeling of the execution plan.

But there's no such thing as real data :) What you can do, as a workaround, is BEGINning a transaction, INSERT some data, EXPLAIN ANALYSE your query, then ROLLBACK your transaction.

Example :

mydatabase=# BEGIN;
BEGIN
mydatabase=# INSERT INTO auth_message (user_id, message) VALUES (1, 'foobar');
INSERT 0 1
mydatabase=# EXPLAIN ANALYSE SELECT count(*) FROM auth_message;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=24.50..24.51 rows=1 width=0) (actual time=0.011..0.011 rows=1 loops=1)
   ->  Seq Scan on auth_message  (cost=0.00..21.60 rows=1160 width=0) (actual time=0.007..0.008 rows=1 loops=1)
 Total runtime: 0.042 ms
(3 lignes)

mydatabase=# ROLLBACK;
ROLLBACK
mydatabase=# EXPLAIN ANALYSE SELECT count(*) FROM auth_message;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=24.50..24.51 rows=1 width=0) (actual time=0.011..0.011 rows=1 loops=1)
   ->  Seq Scan on auth_message  (cost=0.00..21.60 rows=1160 width=0) (actual time=0.009..0.009 rows=0 loops=1)
 Total runtime: 0.043 ms
(3 lignes)

The 1st EXPLAIN ANALYSE shows that there was some "temporary" data (rows=1)

This is not strictly a "mock", but at least, PostgreSQL plan execution (and various optimizations it could do) should be, IMHO, best than with no data (disclaimer : purely intuitive)



来源:https://stackoverflow.com/questions/21013352/how-to-manually-update-the-statistics-data-of-tables-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!