dbTalk Databases Forums  

Difference with and without bind variables

comp.databases.postgresql comp.databases.postgresql


Discuss Difference with and without bind variables in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mladen Gogala
 
Posts: n/a

Default Difference with and without bind variables - 08-08-2010 , 06:13 PM






I was confused by the role of the bind variables for Postgres
applications so I created 2 scripts, doing the same thing, with or
without bind variables. The difference is staggering. Here are the
scripts, first with binds:

mgogala@ubuntu:/tmp$ cat test_bind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =>
0});
my $INS = "insert into TAB1 values(?,?)";
my $sth = $dbh->prepare($INS);
foreach my $i (1 .. 100000)
{
$sth->execute(($i,"Postgres"));
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

And without binds:

mgogala@ubuntu:/tmp$ cat test_nobind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =>
0});
my $INS = "insert into TAB1 values(";
foreach my $i (1 .. 100000)
{
my $SQL=$INS.$i.",'Postgres')";
my $sth=$dbh->prepare($SQL);
$sth->execute();
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}


The difference in speed is enormous:

mgogala@ubuntu:/tmp$ time ./test_nobind

real 0m59.129s
user 0m17.933s
sys 0m8.965s
mgogala@ubuntu:/tmp$ time ./test_bind

real 0m20.358s
user 0m5.768s
sys 0m2.640s
mgogala@ubuntu:/tmp$


Inserts with bind variables are 2.5 times faster than without bind
variables. Anybody care to comment?
--
http://mgogala.byethost5.com

Reply With Quote
  #2  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-08-2010 , 06:40 PM






On Sun, 08 Aug 2010 23:13:55 +0000, Mladen Gogala wrote:

Quote:
Inserts with bind variables are 2.5 times faster than without bind
variables. Anybody care to comment?
The table looks like this:
scott=# \d tab1
Table "public.tab1"
Column | Type | Modifiers
--------+-----------------------+-----------
col1 | bigint | not null
col2 | character varying(16) |
Indexes:
"tab1_pkey" PRIMARY KEY, btree (col1)


Postgres version is 8.4.4 on my laptop (Compaq nc6400).

--
http://mgogala.byethost5.com

Reply With Quote
  #3  
Old   
Bob Badour
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-08-2010 , 07:19 PM



Mladen Gogala wrote:

Quote:
I was confused by the role of the bind variables for Postgres
applications so I created 2 scripts, doing the same thing, with or
without bind variables. The difference is staggering. Here are the
scripts, first with binds:

mgogala@ubuntu:/tmp$ cat test_bind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =
0});
my $INS = "insert into TAB1 values(?,?)";
my $sth = $dbh->prepare($INS);
foreach my $i (1 .. 100000)
{
$sth->execute(($i,"Postgres"));
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

And without binds:

mgogala@ubuntu:/tmp$ cat test_nobind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =
0});
my $INS = "insert into TAB1 values(";
foreach my $i (1 .. 100000)
{
my $SQL=$INS.$i.",'Postgres')";
my $sth=$dbh->prepare($SQL);
$sth->execute();
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

The difference in speed is enormous:

mgogala@ubuntu:/tmp$ time ./test_nobind

real 0m59.129s
user 0m17.933s
sys 0m8.965s
mgogala@ubuntu:/tmp$ time ./test_bind

real 0m20.358s
user 0m5.768s
sys 0m2.640s
mgogala@ubuntu:/tmp$

Inserts with bind variables are 2.5 times faster than without bind
variables. Anybody care to comment?
Compiling (preparing) the sql statement takes time and resources. The
same happens with Oracle too just as it happens with any backend that
allows one to prepare a statement once with bind variables to execute
and re-execute.

Reply With Quote
  #4  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-09-2010 , 07:08 AM



On Sun, 08 Aug 2010 21:19:26 -0300, Bob Badour wrote:

Quote:
Compiling (preparing) the sql statement takes time and resources. The
same happens with Oracle too just as it happens with any backend that
allows one to prepare a statement once with bind variables to execute
and re-execute.
Actually, the motivation for this test came from the 1st chapter of the
Tom Kyte's new book. Unfortunately, from what I have seen around me,
people think that Postgres is different and the applications are
frequently using the trick from the "nobind" script, with assembling SQL
from the input and then executing it. In addition to being insecure
because of the SQL injection, such practice also produces underperforming
apps.



--
http://mgogala.byethost5.com

Reply With Quote
  #5  
Old   
Bob Badour
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-09-2010 , 03:46 PM



Mladen Gogala wrote:

Quote:
On Sun, 08 Aug 2010 21:19:26 -0300, Bob Badour wrote:

Compiling (preparing) the sql statement takes time and resources. The
same happens with Oracle too just as it happens with any backend that
allows one to prepare a statement once with bind variables to execute
and re-execute.

Actually, the motivation for this test came from the 1st chapter of the
Tom Kyte's new book. Unfortunately, from what I have seen around me,
people think that Postgres is different and the applications are
frequently using the trick from the "nobind" script, with assembling SQL
from the input and then executing it. In addition to being insecure
because of the SQL injection, such practice also produces underperforming
apps.
I don't think anyone would disagree.

Reply With Quote
  #6  
Old   
M. Strobel
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-11-2010 , 07:05 AM



Am 09.08.2010 01:13, schrieb Mladen Gogala:
Quote:
I was confused by the role of the bind variables for Postgres
applications so I created 2 scripts, doing the same thing, with or
without bind variables. The difference is staggering. Here are the
scripts, first with binds:

mgogala@ubuntu:/tmp$ cat test_bind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =
0});
my $INS = "insert into TAB1 values(?,?)";
my $sth = $dbh->prepare($INS);
foreach my $i (1 .. 100000)
{
$sth->execute(($i,"Postgres"));
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

And without binds:

mgogala@ubuntu:/tmp$ cat test_nobind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =
0});
my $INS = "insert into TAB1 values(";
foreach my $i (1 .. 100000)
{
my $SQL=$INS.$i.",'Postgres')";
my $sth=$dbh->prepare($SQL);
$sth->execute();
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}


The difference in speed is enormous:

mgogala@ubuntu:/tmp$ time ./test_nobind

real 0m59.129s
user 0m17.933s
sys 0m8.965s
mgogala@ubuntu:/tmp$ time ./test_bind

real 0m20.358s
user 0m5.768s
sys 0m2.640s
mgogala@ubuntu:/tmp$


Inserts with bind variables are 2.5 times faster than without bind
variables. Anybody care to comment?
For a complete test I would like to see the same prog with no
bind variables and no prepare step, just directly execute the sql
string.

Max

Reply With Quote
  #7  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Difference with and without bind variables - 08-12-2010 , 10:38 AM



On Wed, 11 Aug 2010 14:05:33 +0200, M. Strobel wrote:

Quote:
Am 09.08.2010 01:13, schrieb Mladen Gogala:
I was confused by the role of the bind variables for Postgres
applications so I created 2 scripts, doing the same thing, with or
without bind variables. The difference is staggering. Here are the
scripts, first with binds:

mgogala@ubuntu:/tmp$ cat test_bind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit
=> 0});
my $INS = "insert into TAB1 values(?,?)"; my $sth =
$dbh->prepare($INS);
foreach my $i (1 .. 100000)
{
$sth->execute(($i,"Postgres"));
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

And without binds:

mgogala@ubuntu:/tmp$ cat test_nobind
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit
=> 0});
my $INS = "insert into TAB1 values("; foreach my $i (1 .. 100000)
{
my $SQL=$INS.$i.",'Postgres')";
my $sth=$dbh->prepare($SQL);
$sth->execute();
}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}


The difference in speed is enormous:

mgogala@ubuntu:/tmp$ time ./test_nobind

real 0m59.129s
user 0m17.933s
sys 0m8.965s
mgogala@ubuntu:/tmp$ time ./test_bind

real 0m20.358s
user 0m5.768s
sys 0m2.640s
mgogala@ubuntu:/tmp$


Inserts with bind variables are 2.5 times faster than without bind
variables. Anybody care to comment?

For a complete test I would like to see the same prog with no bind
variables and no prepare step, just directly execute the sql string.

Max
Hmmm, results are strange:

mgogala@nycwxp2622:~$ time /tmp/test_nobind2

real 0m17.880s
user 0m3.244s
sys 0m1.100s
mgogala@nycwxp2622:~$

The "test_nobind2" script is written according to your specification:
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Pg:dbname=scott","mgogala","", {AutoCommit =>
0});
$dbh->{pg_server_prepare} = 0;
my $INS = "insert into TAB1 values(";
foreach my $i (1 .. 100000)
{
my $SQL=$INS.$i.",'Postgres')";
$dbh->do($SQL);

}
$dbh->commit();

END
{
$dbh->disconnect if defined($dbh);
}

That means that "prepare" is the most expensive part of the execution. If
I allow it to use already prepared statements, it's faster still:

mgogala@nycwxp2622:~$ time /tmp/test_nobind2

real 0m17.803s
user 0m3.124s
sys 0m1.088s

That, of course, has pg_server_prepare set to "1".


--
http://mgogala.byethost5.com

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.