How to get Count of each Column value of table?

What is a good way to optimize a count query in Postgresql?

  • I have a table in postgresql that contains an array which is updated constantly. In my application i need to get the number of rows for which a specific parameter is not present in that array column.  My query looks like this:     select count(id) from table where not (ARRAY[‘parameter value’] <@ table.array_column) But when increasing the amount of rows and the amount of executions of that query (several times per second, possibly hundreds or thousands) the performance decreses a lot, it seems to me that the counting in postgresql might have a linear order of execution (I’m not completely sure of this). Basically my question is: Is there an existing pattern I’m not aware of that applies to this situation? what would be the best approach for this? Any suggestion you could give me would be really appreciated.

  • Answer:

    After using the Explain command as suggested by Steve Lacy i found out that given the current structure of my databse it will always be sequential so im going to have a cache with the most recent count to speed it up a little and execute the query only when necesary

Jeffry Gutierrez at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.