sql – I need to optimize the following pgsql select statement – its currently taking 34 avg seconds to execute

I have a view that shows all relations. I use the following select for the view: CREATE VIEW relationships AS SELECT DISTINCT relations.name, sch_tco.id AS table_id, relations.related_table AS related_table, scd_tco.id AS related_id, relations.foreign_column AS foreign_column_name, relations.local_column AS local_column_name, count(DISTINCT relations.name) AS relationships, count(DISTINCT relations.referenced_tables) AS foreign_keys_count, count(DISTINCT relations.referencing_tables) AS references_count, count(DISTINCT related_table) AS related_tables_count, count(DISTINCT … Read more