Skip to main content
Don't link to outdated Postgres versions
Source Link
user1822
user1822

The issue you are running into is because you are creating nested views which can cause postgress to lose track of indexes and not use them properly. This is something that I have noticed before when you get multiple layers deep the optimizer will no longer be using the indexes. A good way to check this would be do an explain on each view.

What you can do is use a CTE(Common Table Expression) in order to do all the calculations in a single view. This will buy you two things.

  1. The optimizer will be able to hit all indexes
  2. You will be able to limit the data you are looking at to the last 30 days so the database will have less items to work at which will help performance.

https://www.postgresql.org/docs/9.1/queries-with.htmlhttps://www.postgresql.org/docs/current/queries-with.html

The issue you are running into is because you are creating nested views which can cause postgress to lose track of indexes and not use them properly. This is something that I have noticed before when you get multiple layers deep the optimizer will no longer be using the indexes. A good way to check this would be do an explain on each view.

What you can do is use a CTE(Common Table Expression) in order to do all the calculations in a single view. This will buy you two things.

  1. The optimizer will be able to hit all indexes
  2. You will be able to limit the data you are looking at to the last 30 days so the database will have less items to work at which will help performance.

https://www.postgresql.org/docs/9.1/queries-with.html

The issue you are running into is because you are creating nested views which can cause postgress to lose track of indexes and not use them properly. This is something that I have noticed before when you get multiple layers deep the optimizer will no longer be using the indexes. A good way to check this would be do an explain on each view.

What you can do is use a CTE(Common Table Expression) in order to do all the calculations in a single view. This will buy you two things.

  1. The optimizer will be able to hit all indexes
  2. You will be able to limit the data you are looking at to the last 30 days so the database will have less items to work at which will help performance.

https://www.postgresql.org/docs/current/queries-with.html

Source Link
Joe W
  • 1.1k
  • 9
  • 20

The issue you are running into is because you are creating nested views which can cause postgress to lose track of indexes and not use them properly. This is something that I have noticed before when you get multiple layers deep the optimizer will no longer be using the indexes. A good way to check this would be do an explain on each view.

What you can do is use a CTE(Common Table Expression) in order to do all the calculations in a single view. This will buy you two things.

  1. The optimizer will be able to hit all indexes
  2. You will be able to limit the data you are looking at to the last 30 days so the database will have less items to work at which will help performance.

https://www.postgresql.org/docs/9.1/queries-with.html