![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Inserts with bind variables are 2.5 times faster than without bind variables. Anybody care to comment? |
#3
| |||
| |||
|
|
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? |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
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? |
#7
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |