🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1481

Re: PreparedStatements

#1481
From: TBerry
Date: Fri, 21 Dec 2001 15:59
62 lines
4108 bytes
Excellent explanation - Thanks

Andy Blanchard wrote:

> The answer to #1 is: "It depends".
>     A prepared statement *can* save substantial time (overall) if a
> particular query is run often (or several times in a row).  When you prepare
> a statement, the DB server pre-works the execution plan (and often runs it
> through the query optimizer).  Then, when you execute the prepared
> statement, the server already has a [optimized] execution plan for the
> query, eliminating *at least* some of the work (perhaps alot of work for
> complex SQL statements).
>     The downside of prepared statements is that they consume DB server
> resources.  Most database instances will have a max number of prepared
> statements (either per-user or per-database instance).  If you over-use
> prepared statements you'll quickly run into a "resources exhausted"
> situation.  This will also happen if you forget to release prepared
> statements.  Remember that these are kept *on the server*, so until you
> explicitly tell the server to release the prepared statement, the server
> will hang on to it, anticipating that you will be using it again in the
> future.
>     Prepared Statements can be great -- especially for mass (bulk)
> transactions and such, with bound parameters.  For example, you wanna upload
> 50,000 records:  prepare a statement such as "INSERT INTO customer (id,
> name, address, phone) VALUES (?,?,?,?)".  Then begin binding variables and
> executing the prepared statement.  Then, release the prepared statement when
> you're finished!
>
> The answer to #2: No.
>     Do NOT create a prepared statement every time you want to execute a
> query.  Preparing a statement (a) takes time, (b) uses valuable DB
> resources, and (c) should be treated as a protected resource.  Use a normal
> statement for your on-the-fly queries.  Use a prepared statement for
> often-executed queries.  Store the prepared-statement in an instance (or
> static) variable so that you can reference it on the next call.  By creating
> a "new" PreparedStatement (event if its identical to the previous one), your
> DB will grind through the process of preparing the new statement (killing
> any performance improvement) AND use up another prepared-statement block of
> DB memory.
>
> Hope this helps,
> Andy
>
> "TBerry" <tberry@vhb.com> wrote in message news:3C1E52A9.80F240C@vhb.com...
> > I have two questions on PreparedStatements:
> >
> > 1. For basic queries (e.g. SELECT a, b, c, d FROM table Z ORDER BY a),
> > is there any benefit to making this a PreparedStatement versus a
> > Statement?
> >
> > 2. My web app. is an interactive GUI front end to a database. SELECT,
> > INSERT and UPDATE queries are routinely executed. I have not yet
> > implemented connection pooling but will be. Regarding executing a
> > PreparedStatement, after the execution, do I recreate the statement with
> > new values (if any), each time? That's what the documentation seems to
> > imply. And since I've no connection pooling yet, I'm closing each
> > Statement/PreparedStatement when it's completed. If it matters I'm using
> > Microsoft's type 4 JDBC driver.
> >
> > Thank you
> > Tom Berry
> >
Technical Details
Message-ID: <3C235C7B.3D702EA3@vhb.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews
References: <3C1E52A9.80F240C@vhb.com> <3c230687_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,68fc473350be3803
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-12-21 08:04:10 PST
Message-ID: <3C235C7B.3D702EA3@vhb.com>
Date: Fri, 21 Dec 2001 10:59:55 -0500
From: TBerry <tberry@vhb.com>
X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: PreparedStatements
References: <3C1E52A9.80F240C@vhb.com> <3c230687_2@dnews>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 204.243.174.175
X-Trace: dnews 1008950342 204.243.174.175 (21 Dec 2001 07:59:02 -0800)
Lines: 63
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews