Postgres - non-linear relationship between row count and query speed











up vote
0
down vote

favorite












Why isn't there a direct (linear) relationship between the number of rows being processed and the time taken?



Example - I'm moving rows from one table to another. If I move a million rows it takes about 20 seconds, if I move 10 million rows it doesn't take 200 seconds (about 4 minutes) it takes closer to 20 minutes, and if I move 20 million rows it takes about 2 hours.



Background - I'm merging daily partitions into larger monthly partitions by running the following queries as a single transaction.....



ALTER TABLE table_a DETACH PARTITION table_a_201811; 
ALTER TABLE table_a DETACH PARTITION table_a_20181104;
WITH moved_rows AS
(
DELETE FROM table_a_20181104
RETURNING *
)
INSERT INTO table_a_201811
SELECT * FROM moved_rows;
ALTER TABLE table_a ATTACH PARTITION table_a_201811 FOR VALUES FROM ('2018-11-01') TO ('2018-11-05');
DROP TABLE table_a_20181104;


Experimentation indicates that the ALTER TABLE commands for detaching/attaching partitions take only a few seconds (seemingly independently of table size) whilst the middle statement that actual does the transfer takes the bulk of the time.



I had though that if it takes x seconds to move a millions rows it would take 2x seconds to move 2 million rows, and 10x seconds to move 10 million rows. This doesn't seem to be the case. Why not? - and is there a way of improving the performance?



I'm using version 10.5, and the process has exclusive access to the database (no other connections, and no locks showing in pg_locks.










share|improve this question


























    up vote
    0
    down vote

    favorite












    Why isn't there a direct (linear) relationship between the number of rows being processed and the time taken?



    Example - I'm moving rows from one table to another. If I move a million rows it takes about 20 seconds, if I move 10 million rows it doesn't take 200 seconds (about 4 minutes) it takes closer to 20 minutes, and if I move 20 million rows it takes about 2 hours.



    Background - I'm merging daily partitions into larger monthly partitions by running the following queries as a single transaction.....



    ALTER TABLE table_a DETACH PARTITION table_a_201811; 
    ALTER TABLE table_a DETACH PARTITION table_a_20181104;
    WITH moved_rows AS
    (
    DELETE FROM table_a_20181104
    RETURNING *
    )
    INSERT INTO table_a_201811
    SELECT * FROM moved_rows;
    ALTER TABLE table_a ATTACH PARTITION table_a_201811 FOR VALUES FROM ('2018-11-01') TO ('2018-11-05');
    DROP TABLE table_a_20181104;


    Experimentation indicates that the ALTER TABLE commands for detaching/attaching partitions take only a few seconds (seemingly independently of table size) whilst the middle statement that actual does the transfer takes the bulk of the time.



    I had though that if it takes x seconds to move a millions rows it would take 2x seconds to move 2 million rows, and 10x seconds to move 10 million rows. This doesn't seem to be the case. Why not? - and is there a way of improving the performance?



    I'm using version 10.5, and the process has exclusive access to the database (no other connections, and no locks showing in pg_locks.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Why isn't there a direct (linear) relationship between the number of rows being processed and the time taken?



      Example - I'm moving rows from one table to another. If I move a million rows it takes about 20 seconds, if I move 10 million rows it doesn't take 200 seconds (about 4 minutes) it takes closer to 20 minutes, and if I move 20 million rows it takes about 2 hours.



      Background - I'm merging daily partitions into larger monthly partitions by running the following queries as a single transaction.....



      ALTER TABLE table_a DETACH PARTITION table_a_201811; 
      ALTER TABLE table_a DETACH PARTITION table_a_20181104;
      WITH moved_rows AS
      (
      DELETE FROM table_a_20181104
      RETURNING *
      )
      INSERT INTO table_a_201811
      SELECT * FROM moved_rows;
      ALTER TABLE table_a ATTACH PARTITION table_a_201811 FOR VALUES FROM ('2018-11-01') TO ('2018-11-05');
      DROP TABLE table_a_20181104;


      Experimentation indicates that the ALTER TABLE commands for detaching/attaching partitions take only a few seconds (seemingly independently of table size) whilst the middle statement that actual does the transfer takes the bulk of the time.



      I had though that if it takes x seconds to move a millions rows it would take 2x seconds to move 2 million rows, and 10x seconds to move 10 million rows. This doesn't seem to be the case. Why not? - and is there a way of improving the performance?



      I'm using version 10.5, and the process has exclusive access to the database (no other connections, and no locks showing in pg_locks.










      share|improve this question













      Why isn't there a direct (linear) relationship between the number of rows being processed and the time taken?



      Example - I'm moving rows from one table to another. If I move a million rows it takes about 20 seconds, if I move 10 million rows it doesn't take 200 seconds (about 4 minutes) it takes closer to 20 minutes, and if I move 20 million rows it takes about 2 hours.



      Background - I'm merging daily partitions into larger monthly partitions by running the following queries as a single transaction.....



      ALTER TABLE table_a DETACH PARTITION table_a_201811; 
      ALTER TABLE table_a DETACH PARTITION table_a_20181104;
      WITH moved_rows AS
      (
      DELETE FROM table_a_20181104
      RETURNING *
      )
      INSERT INTO table_a_201811
      SELECT * FROM moved_rows;
      ALTER TABLE table_a ATTACH PARTITION table_a_201811 FOR VALUES FROM ('2018-11-01') TO ('2018-11-05');
      DROP TABLE table_a_20181104;


      Experimentation indicates that the ALTER TABLE commands for detaching/attaching partitions take only a few seconds (seemingly independently of table size) whilst the middle statement that actual does the transfer takes the bulk of the time.



      I had though that if it takes x seconds to move a millions rows it would take 2x seconds to move 2 million rows, and 10x seconds to move 10 million rows. This doesn't seem to be the case. Why not? - and is there a way of improving the performance?



      I'm using version 10.5, and the process has exclusive access to the database (no other connections, and no locks showing in pg_locks.







      postgresql database-performance






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 18:17









      Hemel

      1948




      1948





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195451%2fpostgres-non-linear-relationship-between-row-count-and-query-speed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195451%2fpostgres-non-linear-relationship-between-row-count-and-query-speed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini