X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.192]) by svr1.postgresql.org (Postfix) with ESMTP id D5F16CC5D0 for ; Wed, 16 Jul 2003 20:29:03 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr4.postgresql.org [64.117.224.192]) (amavisd-new, port 10024) with ESMTP id 92675-04 for ; Wed, 16 Jul 2003 20:29:28 +0000 (GMT) Received: from mx.laposte.net (mx.laposte.net [213.30.181.11]) by svr1.postgresql.org (Postfix) with ESMTP id 205DACC5EA for ; Wed, 16 Jul 2003 17:29:02 -0300 (ADT) Received: from [200.50.115.134] (200.50.115.134) by mx.laposte.net (6.0.053) (authenticated as julien.legoff@laposte.net) id 3F144A2100044404 for pgsql-jdbc@postgresql.org; Wed, 16 Jul 2003 22:29:28 +0200 From: Julien Le Goff To: pgsql-jdbc@postgresql.org Subject: Prepared Statements Date: Wed, 16 Jul 2003 16:24:37 -0400 User-Agent: KMail/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200307161624.39340.julien.legoff@laposte.net> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/63 X-Sequence-Number: 7447 Hello everyone, I have a question regarding the efficiency of Prepared Statements. I'm working on a project, and my task now is to decide whether it's worth it to use PS. This problem came up when, beginning to implement jdbc classes, we noticed that we would need a lot of PS - something like 40 per class. Each PS would be a class variable, and it sounds weird to have 40 class variables... We could have a more elegant system using normal statements, but would it be much less efficient? I started doing some very simple tests: inserting 1000 elements to a table, doing 1.000.000 simple queries, then 1.000.000 queries with a join... But suprisingly, Prepared Statements didn't give better results than normal statements. Before warning the world that prepared statements are a big lie, I wanted to have your opinion. Has anyone done a reliable test showing the difference between PS and normal statements? Does anyone know "how" better PS are supposed to be? Then, concerning my test, what the hell could be wrong in what I did? The query is the following: String theJoinQueryPrepared = "SELECT tr.text FROM truc tr, test te " + "WHERE tr.id = te.id AND te.id = ?"; for a Prepared Statement, and String theJoinQuery = "SELECT tr.text FROM truc tr, test te " + WHERE tr.id = te.id AND te.id = "; for a Statement. Then I just do: for(int j = 0; j < 1000; j++) { for(int i = 0; i < 1000; i++) { thePS.setInt(1, i); ResultSet theResultSet = thePS.executeQuery(); } } and for(int j = 0; j < 1000; j++) { for(int i = 0; i < 1000; i++) { ResultSet theResultSet = theStatement.executeQuery( theJoinQueryPrepared + i); } } I realize that this test is ridiculously simple, but shouldn't the first loop be more efficient? On my server both are equally fast... Ok, I hope this message wasn't too long / too stupid. Thanks in advance, Julien