Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fOk4q-00070s-NK for pgsql-general@arkaria.postgresql.org; Fri, 01 Jun 2018 13:26:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fOk4l-00089E-UP for pgsql-general@arkaria.postgresql.org; Fri, 01 Jun 2018 13:26:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fOk4l-000895-MC for pgsql-general@lists.postgresql.org; Fri, 01 Jun 2018 13:26:39 +0000 Received: from 23-25-19-49-static.hfc.comcastbusiness.net ([23.25.19.49] helo=jeeves.wrkhors.com) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fOk4d-0006V0-6D for pgsql-general@lists.postgresql.org; Fri, 01 Jun 2018 13:26:34 +0000 Received: from wrkhors.com (localhost [127.0.0.1]) by jeeves.wrkhors.com (Postfix) with ESMTP id 6202010EE1D; Fri, 1 Jun 2018 08:38:50 -0500 (CDT) Date: Fri, 1 Jun 2018 08:35:44 -0500 From: Steven Lembark To: pgsql-general@lists.postgresql.org Cc: lembark@wrkhors.com Subject: Re: reduce number of multiple values to be inserted Message-ID: <20180601083544.5be9d57d@wrkhors.com> In-Reply-To: References: Organization: Workhorse Computing MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 30 May 2018 08:10:05 +0800 tango ward wrote: > curr_pgsql.execute(''' > INSERT INTO student (created, modified, name, > address, age, > level ) > VALUES (current_timezone, current_timezone, > 'Scott', 'Malayan Village', 21, 2), > (current_timezone, current_timezone, > 'Ben', 'Malayan Village', 21, 2), > (current_timezone, current_timezone, > 'Scott', 'Malayan Village', 21, 2), > > (current_timezone, current_timezone, > 'Andrew', 'Malayan Village', 25, 2), > (current_timezone, current_timezone, > 'Larry', 'Malayan Village', 25, 2), > (current_timezone, current_timezone, > 'Adam', 'Malayan Village', 25, 2), > > (current_timezone, current_timezone, > 'Elisse', 'Malayan Village', 27, 2), > (current_timezone, current_timezone, > 'Xena', 'Malayan Village', 27, 2), > (current_timezone, current_timezone, > 'Karen', 'Malayan Village', 27, 2) > > I will repeat the same process for 13 villages so that will be 117 of > values. I would like to know if there's a way to reduce the script? > This has to be done strictly via script. Rather than try to cut+paste SQL it may be easier to put the data into a flat file and have some code spit the SQL out for you: #!/usr/bin/env perl ######################################################################## # housekeeping ######################################################################## use v5.22; use YAML::XS qw( Load ); ######################################################################## # package variables ######################################################################## my $data = do { # slurp the data from named files or stdin. local $/; Load }; my $prefix = <<'SQL'; /* * Input data for vx.y.z of student data schema. */ insert into student ( created, modified, name, address, age, level ) values ( SQL my $suffix = <<'SQL'; ); /* * end of input */ SQL ######################################################################## # output the SQL ######################################################################## say $prefix; while( my ( $place, $place_valz ) = each %$data ) { while( my ( $nums, $namz ) = each %$place_valz ) { for my $name ( @$namz ) { say <<"SQL"; ( current_timezone, current_timezone, '$name' , '$place' , '$nums' ) } SQL } } say $suffix; # this is not a module 0 __END__ =head1 NAME output_sql - dump insert ... values ... from YAML =head1 SYNOPSIS Inputs arrive via stdin, literal, or glob-ed paths: output_sql << /path/to/foobar.yaml; output_sql /path/to/foobar.yaml; output_sql /path/to/*.yaml; gzip -dc < /path/to/bigfile.yaml | output_sql; gzip -dc < /path/to/bigfile.yaml | output_sql | psql; Your data file could look like this if you want a single flat file for all of it: --- Malayan Village : 21, 2 : - Ben - Scott 25, 2 : - Anderew - Larry - Adam ... Another Village : ... Or your could break it into chunks using multiple documents within the YAML file (notice the extra '---'): --- Malayan Village : 21, 2 : - Ben - Scott 25, 2 : - Anderew - Larry - Adam ... --- Another Village : ... At which point $data, above, is an array and you get: for my $chunk ( @$data ) { while( my ( $place, $place_valz ) = each %$chunk ) { ... } } with the added flexibility of breaking the input data into multiple files if needed. -- Steven Lembark 1505 National Ave Workhorse Computing Rockford, IL 61103 lembark@wrkhors.com +1 888 359 3508