How disable postgresql “Cache” optimization?

后端 未结 3 1897
暖寄归人
暖寄归人 2021-01-01 12:12

I\'m trying to optimize my function.
The thing is when you run the query once you get one result.
Run the query a second time or third time the process time is muc

3条回答
  •  猫巷女王i
    2021-01-01 13:09

    PostgreSQL doesn't have a "cache" optimisation, in the sense of a query result cache.

    It does cache table blocks that were recently read in shared_buffers, but for most installs that only has a small effect. The main cache is the operating system's disk read cache. For more information see:

    See and clear Postgres caches/buffers?

    It sounds to me like you have a system with a reasonable amount of RAM and a fast CPU but a terribly slow disk. So queries that only hit the OS's disk cache are very fast, but queries that go to disk take a couple of seconds to read the data in. So caching effects are very strong.

    You should explain (buffers, analyze, verbose) SELECT ... your queries. Try with a couple of different input values until you get a slow one. Compare plans.

    If the plans are the same, that's probably it.

    If the plans are different, you're probably hitting a situation where the query planner is making bad choices based on variations in the table statistics. Increasing the statistics targets for the columns of interest may help (see the manual). If you get different plans and are stuck / want help, feel free to post a new question on dba.stackexchange.com with details.

提交回复
热议问题