0

Currently I have a query in PostgreSQL that makes use of the ARRAY_AGG function in order to group together all distinct values for each column.

The query works fine however it can take up to 38 seconds to run which is not that efficient.

The query currently is like this:

WITH agged_data AS (
    SELECT birth_date, place_of_birth, first_name
    FROM user_info
)

SELECT (ARRAY_AGG(DISTINCT birth_date)), 
(ARRAY_AGG(DISTINCT place_of_birth)), 
(ARRAY_AGG(DISTINCT first_name)), 
FROM agged_data LIMIT 100

There are around >17,000 rows.

The query gets executed in Python backend (FastAPI) but I tested it out in PgAdmin as well. Originally I was running separate queries for each column (there are more columns in the table and the query I use, I just wrote the above as a MWE). But then thought it would put more strain on the db.

Is there an alternative to ARRAY_AGG for what I want to achieve?

7
  • Did you mean to put the LIMIT 100 inside the agged_data query? Commented Mar 31, 2023 at 11:55
  • No I tried that, but then it only got the distinct values of the first 100. Commented Mar 31, 2023 at 11:56
  • 2
    Without a query plan, nobody knows why the query is slow. Could you please share the DDL for all tables involved, including the indexes, and the result from EXPLAIN(ANALYZE, VERBOSE, BUFFERS) for this query? (all in plain text) Commented Mar 31, 2023 at 12:09
  • @mp252 Then what do you want to limit to 100? array_agg always returns a single row anyway. Commented Mar 31, 2023 at 12:16
  • 1
    @mp252 For that, you'd need three different subquery, e.g. using the ARRAY constructor: SELECT ARRAY(SELECT DISTINCT birth_date FROM user_info LIMIT 100) AS birth_dates, ARRAY(…) AS places_of_birth, …; Commented Mar 31, 2023 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.