Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:57:14+00:00 2026-06-04T18:57:14+00:00

I am trying to create a new table which is the aggregate-sum of 6

  • 0

I am trying to create a new table which is the aggregate-sum of 6 other tables with matching primary keys. This keeps stalling if I use more than 3 input tables:

 CREATE TABLE table_name AS SELECT table1.timestamp, table1.value + table2.value + table3.value + table4.value AS value FROM table1, table2, table3, table4 WHERE table1.timestamp=table2.timestamp AND table2.timestamp=table3.timestamp AND table3.timestamp=table4.timestamp;

Problem: The script works fairly fast (<5 seconds) when running for 2-3 tables but stalls otherwise. I have not tried running it longer than 5 minutes but this would be too slow for my purposes anyway.

Description of tables: Each table has an identical format of 6 columns (2 of which are relevant). The primary key is an integer “timestamp” and the “value” is a real number. Table sizes vary, but hover around 100k rows/entries for each table. The tables mostly have the same primary keys but some data points are missing in each table so it is crucial that those data points be omitted from the new table.

Is there something I am doing wrong and what should I do to make this run fast?

EDIT:

Ps: here is the actual output of a complete “EXPLAIN ANALYZE” query:

eldb=# EXPLAIN ANALYZE CREATE TABLE test_table AS SELECT count1.timestamp, count
1.year, count1.month, count1.day, count1.period, count1.the_value + count2.the_value + count
3.the_value + count4.the_value + count5.the_value + count6.the_value AS the_value FROM "table_name-1" AS count
1, "table_name-2" AS count2, "table_name-3" AS count3, "table_name-4" AS count4,
 "table_name-5" AS count5, "table_name-6" AS count6 WHERE count1.timestamp=count
2.timestamp AND count2.timestamp=count3.timestamp AND count3.timestamp=count4.ti
mestamp AND count4.timestamp=count5.timestamp AND count5.timestamp=count6.timest
amp AND count1.timestamp>2012020000 AND count2.timestamp>2012020000 AND count3.t
imestamp>2012020000 AND count4.timestamp>2012020000 and count5.timestamp>2012020
000 AND count6.timestamp>2012020000;
                                                                          QUERY
PLAN
--------------------------------------------------------------------------------
------------------------------------------------------------------------------
 Merge Join  (cost=20323.61..153806457715456.50 rows=5592655588099248 width=44)
(actual time=84.524..3310.692 rows=3410 loops=1)
   Merge Cond: (count1."timestamp" = count4."timestamp")
   ->  Nested Loop  (cost=10161.80..4417379579.26 rows=1057606343 width=40) (act
ual time=44.597..1616.585 rows=3410 loops=1)
         Join Filter: (count2."timestamp" = count1."timestamp")
         ->  Merge Join  (cost=10161.80..101480.96 rows=6070522 width=16) (actua
l time=43.648..48.950 rows=3410 loops=1)
               Merge Cond: (count2."timestamp" = count3."timestamp")
               ->  Sort  (cost=5080.90..5168.01 rows=34844 width=8) (actual time
=25.608..25.804 rows=3410 loops=1)
                     Sort Key: count2."timestamp"
                     Sort Method: quicksort  Memory: 256kB
                     ->  Seq Scan on "table_name-2" count2  (cost=0.00..1972.66
rows=34844 width=8) (actual time=0.064..23.297 rows=3410 loops=1)
                           Filter: ("timestamp" > 2012020000)
               ->  Materialize  (cost=5080.90..5255.12 rows=34844 width=8) (actu
al time=18.030..19.847 rows=3410 loops=1)
                     ->  Sort  (cost=5080.90..5168.01 rows=34844 width=8) (actua
l time=18.023..18.416 rows=3410 loops=1)
                           Sort Key: count3."timestamp"
                           Sort Method: quicksort  Memory: 256kB
                           ->  Seq Scan on "table_name-3" count3  (cost=0.00..19
72.66 rows=34844 width=8) (actual time=0.023..16.294 rows=3410 loops=1)
                                 Filter: ("timestamp" > 2012020000)
         ->  Materialize  (cost=0.00..2351.88 rows=34844 width=24) (actual time=
0.000..0.147 rows=3410 loops=3410)
               ->  Seq Scan on "table_name-1" count1  (cost=0.00..1972.66 rows=3
4844 width=24) (actual time=0.020..16.853 rows=3410 loops=1)
                     Filter: ("timestamp" > 2012020000)
   ->  Materialize  (cost=10161.80..4007228099.11 rows=1057606343 width=24) (act
ual time=39.917..1687.402 rows=3410 loops=1)
         ->  Nested Loop  (cost=10161.80..4004584083.26 rows=1057606343 width=24
) (actual time=39.915..1685.956 rows=3410 loops=1)
               Join Filter: (count4."timestamp" = count6."timestamp")
               ->  Merge Join  (cost=10161.80..101480.96 rows=6070522 width=16)
(actual time=38.689..44.309 rows=3410 loops=1)
                     Merge Cond: (count4."timestamp" = count5."timestamp")
                     ->  Sort  (cost=5080.90..5168.01 rows=34844 width=8) (actua
l time=18.960..19.156 rows=3410 loops=1)
                           Sort Key: count4."timestamp"
                           Sort Method: quicksort  Memory: 256kB
                           ->  Seq Scan on "table_name-4" count4  (cost=0.00..19
72.66 rows=34844 width=8) (actual time=0.059..17.271 rows=3410 loops=1)
                                 Filter: ("timestamp" > 2012020000)
                     ->  Materialize  (cost=5080.90..5255.12 rows=34844 width=8)
 (actual time=19.717..21.826 rows=3410 loops=1)
                           ->  Sort  (cost=5080.90..5168.01 rows=34844 width=8)
(actual time=19.708..20.266 rows=3410 loops=1)
                                 Sort Key: count5."timestamp"
                                 Sort Method: quicksort  Memory: 256kB
                                 ->  Seq Scan on "table_name-5" count5  (cost=0.
00..1972.66 rows=34844 width=8) (actual time=0.034..18.001 rows=3410 loops=1)
                                       Filter: ("timestamp" > 2012020000)
               ->  Materialize  (cost=0.00..2283.88 rows=34844 width=8) (actual
time=0.000..0.148 rows=3410 loops=3410)
                     ->  Seq Scan on "table_name-6" count6  (cost=0.00..1972.66
rows=34844 width=8) (actual time=0.036..17.785 rows=3410 loops=1)
                           Filter: ("timestamp" > 2012020000)
 Total runtime: 3330.933 ms
(40 rows)

And here is the table structure (same for all tables):

CREATE TABLE "table_name-6"
(
"timestamp" integer NOT NULL,
year integer NOT NULL,
month integer NOT NULL,
day integer NOT NULL,
period integer NOT NULL,
the_value real,
CONSTRAINT "table_name-6_pkey" PRIMARY KEY ("timestamp" )
)

Note: the actual table names and values were renamed. Also, this output was for a small fraction of the actual table size.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:57:15+00:00Added an answer on June 4, 2026 at 6:57 pm
    DROP SCHEMA tmp CASCADE;
    CREATE SCHEMA tmp ;
    
    set search_path='tmp';
    
    SET random_page_cost=1;
    
    CREATE TABLE table_name1
            ( ztimestamp integer NOT NULL
            , year integer NOT NULL
            , month integer NOT NULL
            , day integer NOT NULL
            , period integer NOT NULL
            , the_value real
            , CONSTRAINT table_name1_pkey PRIMARY KEY (ztimestamp )
            ) ;
    
    CREATE TABLE table_name2
            ( ztimestamp integer NOT NULL
            , year integer NOT NULL
            , month integer NOT NULL
            , day integer NOT NULL
            , period integer NOT NULL
            , the_value real
            , CONSTRAINT table_name2_pkey PRIMARY KEY (ztimestamp )
            ) ;
    
    
    ... similar for 3,4,5,6 ...
    
    
    INSERT INTO table_name1(ztimestamp,year,month,day,period,the_value)
    SELECT generate_series(1,2000), 0,0,0,0, 1.0;
    INSERT INTO table_name2 SELECT * FROM table_name1;
    INSERT INTO table_name3 SELECT * FROM table_name1;
    INSERT INTO table_name4 SELECT * FROM table_name1;
    INSERT INTO table_name5 SELECT * FROM table_name1;
    INSERT INTO table_name6 SELECT * FROM table_name1;
    
    EXPLAIN ANALYZE
    CREATE TABLE test_table AS
    SELECT c1.ztimestamp, c1.year, c1.month, c1.day, c1.period
            , c1.the_value + c2.the_value + c3.the_value + c4.the_value
            + c5.the_value + c6.the_value AS the_value
    FROM table_name1 AS c1
            , table_name2 AS c2
            , table_name3 AS c3
            , table_name4 AS c4
            , table_name5 AS c5
            , table_name6 AS c6
    WHERE c1.ztimestamp=c2.ztimestamp
    AND c2.ztimestamp=c3.ztimestamp
    AND c3.ztimestamp=c4.ztimestamp
    AND c4.ztimestamp=c5.ztimestamp
    AND c5.ztimestamp=c6.ztimestamp
        ;
    

    Result && plan:INSERT 0 2000

    INSERT 0 2000
    INSERT 0 2000
    INSERT 0 2000
    INSERT 0 2000
    INSERT 0 2000
    INSERT 0 2000
                                                                                  QUERY PLAN                                                                               
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Merge Join  (cost=0.00..475.93 rows=1963 width=44) (actual time=0.066..11.840 rows=2000 loops=1)
       Merge Cond: (c1.ztimestamp = c6.ztimestamp)
       ->  Merge Join  (cost=0.00..371.26 rows=1963 width=56) (actual time=0.052..8.706 rows=2000 loops=1)
             Merge Cond: (c1.ztimestamp = c5.ztimestamp)
             ->  Merge Join  (cost=0.00..291.12 rows=1963 width=48) (actual time=0.042..6.752 rows=2000 loops=1)
                   Merge Cond: (c1.ztimestamp = c4.ztimestamp)
                   ->  Merge Join  (cost=0.00..210.98 rows=1963 width=40) (actual time=0.033..4.751 rows=2000 loops=1)
                         Merge Cond: (c1.ztimestamp = c3.ztimestamp)
                         ->  Merge Join  (cost=0.00..130.84 rows=1963 width=32) (actual time=0.022..2.903 rows=2000 loops=1)
                               Merge Cond: (c1.ztimestamp = c2.ztimestamp)
                               ->  Index Scan using table_name1_pkey on table_name1 c1  (cost=0.00..50.70 rows=1963 width=24) (actual time=0.009..0.609 rows=2000 loops=1)
                               ->  Index Scan using table_name2_pkey on table_name2 c2  (cost=0.00..50.70 rows=1963 width=8) (actual time=0.010..0.756 rows=2000 loops=1)
                         ->  Index Scan using table_name3_pkey on table_name3 c3  (cost=0.00..50.70 rows=1963 width=8) (actual time=0.010..0.718 rows=2000 loops=1)
                   ->  Index Scan using table_name4_pkey on table_name4 c4  (cost=0.00..50.70 rows=1963 width=8) (actual time=0.009..0.758 rows=2000 loops=1)
             ->  Index Scan using table_name5_pkey on table_name5 c5  (cost=0.00..50.70 rows=1963 width=8) (actual time=0.010..0.696 rows=2000 loops=1)
       ->  Index Scan using table_name6_pkey on table_name6 c6  (cost=0.00..50.70 rows=1963 width=8) (actual time=0.008..1.044 rows=2000 loops=1)
     Total runtime: 70.201 ms
    (17 rows)
    

    UPDATE: Most people prefer the JOIN-syntax to the where … syntax:

    EXPLAIN ANALYZE
    CREATE TABLE test_table AS
    SELECT c1.ztimestamp, c1.year, c1.month, c1.day, c1.period
            , c1.the_value + c2.the_value + c3.the_value + c4.the_value
            + c5.the_value + c6.the_value AS the_value
    FROM table_name1 AS c1
    JOIN table_name2 AS c2 ON c1.ztimestamp=c2.ztimestamp
    JOIN table_name3 AS c3 ON c2.ztimestamp=c3.ztimestamp
    JOIN table_name4 AS c4 ON c3.ztimestamp=c4.ztimestamp
    JOIN table_name5 AS c5 ON c4.ztimestamp=c5.ztimestamp
    JOIN table_name6 AS c6 ON c5.ztimestamp=c6.ztimestamp
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute a sql query which will create a new table
I am trying to create a new table in a database which has a
I'm trying to correctly write TSQL to create a new table containing the following
Im trying to create new file on D: drive with c/c++ I found this
I'm trying to create a trigger for my table which automatically adds a published
With this version of an iPhone app, I'm trying to create a new core
I am trying to create a flowdocument with a Table of data which I
I am trying to create a table in which the border will appear like
I am currently trying to create a new order (which will be shown below)
I'm trying to create new aggregate function in PostgreSQL to use instead of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.