X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F3EAED7302 for ; Sun, 11 Sep 2005 06:03:32 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79563-07 for ; Sun, 11 Sep 2005 09:03:28 +0000 (GMT) Received: from smtp2.rz.uni-karlsruhe.de (smtp2.rz.uni-karlsruhe.de [129.13.185.218]) by svr1.postgresql.org (Postfix) with ESMTP id 44F82D6F4B for ; Sun, 11 Sep 2005 06:03:27 -0300 (ADT) Received: from rzstud1.stud.uni-karlsruhe.de (rzstud1.stud.uni-karlsruhe.de [193.196.41.33]) by smtp2.rz.uni-karlsruhe.de with esmtp (Exim 4.43 #1) id 1EENkG-0007hC-7W; Sun, 11 Sep 2005 11:03:28 +0200 Received: from uwi7 by rzstud1.stud.uni-karlsruhe.de with local (Exim 3.36 #1) id 1EENkO-0003f3-00; Sun, 11 Sep 2005 11:03:36 +0200 From: Andreas Seltenreich To: Guido Neitzer Cc: pgsql-performance@postgresql.org Subject: Re: Index not used with prepared statement References: <33A6D7B7-FDF3-456E-9154-402E9C5BC5D9@pharmaline.de> X-PGP-Key: 0x2C006B340F8C8C1B X-Face: $:F<87a[gD1?#R6S3j21cr1&C&7bd63GHC.tSdskUb}hhwG(ci*=D5kJ<_N+p9q(7-, PnG. Et.Yh (Guido Neitzer's message of "Sun, 11 Sep 2005 10:29:09 +0200") User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) Message-ID: <87vf18x9gk.fsf@gate450.dyndns.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.001 required=5 tests=[AWL=0.001] X-Spam-Level: X-Archive-Number: 200509/127 X-Sequence-Number: 14435 Guido Neitzer schrob: > I have a performance problem with prepared statements (JDBC prepared > statement). > > This query: > > PreparedStatement st = conn.prepareStatement("SELECT id FROM > dga_dienstleister WHERE plz like '45257'"); > > does use an index. > > This query: > > String plz = "45257"; > PreparedStatement st = conn.prepareStatement("SELECT id FROM > dga_dienstleister WHERE plz like ?"); > st.setString(1, plz); > > does NOT use an index. > > As it should in the end result in absolutely the same statement, the > index should be used all the time. I'm not perfectly sure, but since the index could only be used with a subset of all possible parameters (the pattern for like has to be left-anchored), I could imagine the planner has to avoid the index in order to produce an universal plan (the thing behind a prepared statement). Is there a reason you are using the like operator at all? IMO using the =-operator instead in your example should produce an "index-using prepared statement". HTH Andreas --