X-Original-To: pgsql-jdbc-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 4F5C59FA19B for ; Wed, 21 Jun 2006 12:49:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 11609-03 for ; Wed, 21 Jun 2006 12:49:12 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp.profdata.nl (server.profdata.nl [213.196.2.244]) by postgresql.org (Postfix) with SMTP id 110289FA42B for ; Wed, 21 Jun 2006 12:49:11 -0300 (ADT) Received: (qmail 68927 invoked from network); 21 Jun 2006 15:49:10 -0000 Received: from unknown (HELO ?10.0.0.10?) (212.32.250.201) by server.profdata.nl with SMTP; 21 Jun 2006 15:49:10 -0000 Message-ID: <44996A6C.4070602@sebster.com> Date: Wed, 21 Jun 2006 17:49:00 +0200 From: Sebastiaan van Erk User-Agent: Thunderbird 1.5.0.2 (X11/20060513) MIME-Version: 1.0 To: Dave Cramer CC: pgsql-jdbc@postgresql.org Subject: Re: Limit vs setMaxRows issue References: <44990D57.9070506@sebster.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/71 X-Sequence-Number: 15351 Hi, I'm sorry, but I don't really know what you mean with setMaxRows using a cursor. Note that the *same query* (including the order by) is fast when it contains the LIMIT 100 sql appended to it. So this query *also* does an order by. The only difference between the queries is that one uses the setMaxRows() call to limit the number of rows in the result set, and the other uses SQL. Greetings, Sebastiaan Dave Cramer wrote: > Sebastiaan, > > I believe the setMaxRows will use a cursor, because you have an order > by on the cursor it will have to be fully materialized > > Try it without the order by > > Dave > On 21-Jun-06, at 5:11 AM, Sebastiaan van Erk wrote: > >> Hi, >> >> When using the ps.setMaxRows() call on PreparedStatement, the jdbc >> driver sets the row limit via the "raw" postgres protocol. However, >> in the words of Tom Lane, "the row limit in the protocol only says >> how many rows to deliver in the first batch. The presumption is that >> you'll eventually grab the rest, and so the query is planned on that >> basis." >> >> What this means that when we do the following query: >> >> select action_id from actions order by action_id >> >> with a ps.setMaxRows(100), it takes about 1.8 seconds for the query >> to complete. However, if we do the following query: >> >> select action_id from actions order by action_id limit 100 >> >> without any ps.setMaxRows() the query only takes 0.156 seconds. This >> is more than a factor of 10 faster. >> >> I'm not 100% sure of what JDBC says about setMaxRows (it's kind of >> ambiguous in the java doc, as usual), but as far as I can tell, if >> you call setMaxRows on the prepared statement there is no way in to >> ever retrieve more than that number of rows. If this is indeed the >> case, it seems to me that currently there is a mismatch between the >> JDBC api and the postgresql api, and JDBC should somehow tell >> postgres that this is a hard limit and it should not plan for a >> second batch. >> >> Therefore, my question is: is this a bug? It is not feasable for me >> to add LIMIT clauses to all the SQL queries in my code, so if this IS >> a bug, I hope it can be fixed. If it is NOT a bug, is there an >> alternative workaround that does not involve changing all of my sql >> statements? >> >> Thanks in advance, >> Sebastiaan >> >> >> >> ---------------------------(end of broadcast)--------------------------- >> TIP 2: Don't 'kill -9' the postmaster >> > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster