You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by Olav Sandstaa <Ol...@Sun.COM> on 2006/01/09 14:53:23 UTC

Message round-trips between Derby client and server

We are looking into the network traffic between the Derby JDBC driver
and the Network server and have compared this against MySQL and
PostgreSQL. The main finding is that Derby sends more messages than
the two other databases.

For two simple transaction loads the following number of round-trips
between the client and server is done:

           Derby     MySQL    PostgreSQL
----------------------------------------  
"Select"     4         2          2
"TPC-B"      8         6          6

The "Select" load consists of one SELECT operation followed by a
commit. The "TPC-B" like load consists of five SQL operations (3
updates, 1 insert and 1 select) followed by a commit.

These numbers indicate that Derby needs two extra round-trips between
the client and the server per transaction. These extra round-trips add
to network traffic and to more CPU usage by the Network server. I
think it would be good for the Derby performance if we could get rid
of two of the message round-trips.

This experiment was done using the Derby JDBC client driver. We have
not (yet) tested using the IBM DB2 JDBC Universal driver.

..olav

Re: Message round-trips between Derby client and server

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
Kathey Marsden <km...@sbcglobal.net> writes:

> This sounds like a correct analysis and a good solution.   A couple of 
> areas of vague concern:
>
> You might want to consider if there is any impact on the fix for
> DERBY-213.  I think it should be fine, but popped into my mind as a red
> flag.

Thanks, I'll keep that in mind when I start looking at it.

> Large objects tend to be very sensitive to these types of changes, so
> you might want to give them some  thought for various scenarios
> especially when autocommit is on.  Sorry I can't offer anything more
> concrete.

I will do that. Thanks for your comments.

-- 
Knut Anders


Re: Message round-trips between Derby client and server

Posted by Kathey Marsden <km...@sbcglobal.net>.
Knut Anders Hatlen wrote:

>Bryan Pendleton <bp...@amberpoint.com> writes:
>
>  
>
>>Knut Anders Hatlen wrote:
>>    
>>
>>>I haven't been thinking about the details, but at least it seems like
>>>this is what PostgreSQL and MySQL do.
>>>      
>>>
>
>I think I may have jumped to the wrong conclusions here. I don't think
>PostgreSQL and MySQL are piggybacking ResultSet.close() on commit (and
>I'm starting to agree with Dag that it's not desirable). Instead, I
>think the entire result set is pre-fetched in executeQuery() (given
>that the size of the result set is smaller than the default fetch
>size), and if the result set is forward only, it can be closed on the
>server side.
>
>I have looked more at the behaviour of Derby's client driver. A simple
>select query seems to be executed like this:
>
>  (Let's for the sake of argument assume that the result set has N
>  rows, where N is smaller than the default fetch size, and that we
>  are using forward-only result sets.)
>
>  1. PreparedStatement.executeQuery() is called. The client has one
>     round trip to the server opening the result set. No rows are
>     fetched.
>
>  2. ResultSet.next() is called. The client has one round trip to the
>     server fetching all the rows. The result set on the server side
>     is exhausted, but still open.
>
>  3. The next N-1 calls to ResultSet.next() use the pre-fetched rows
>     and do not generate network activity.
>
>  4. A last call to next() (which returns false) will have one round
>     trip, and the result set on the server is closed. The result set
>     on the client is still open.
>
>  5. ResultSet.close() is called. Since the result set on the server
>     is already closed, no network activity is generated. The result
>     set on the client side is now closed.
>
>  6. Connection.commit() is called, and a round trip to the server is
>     triggered. The transaction is committed on the server.
>
>This is how I understand the code and interpret what I see on the
>network. Does anyone know if this is what actually happens?
>
>Assuming I have got it right this time, this is what I propose that we
>try to do:
>
>  a) Pre-fetching of rows should be done in executeQuery() instead of
>     the first next() call.
>
>  b) Forward-only result sets should be implicitly closed on the
>     server side when all rows have been fetched by the client, so
>     that the client can assume that the result set is closed on the
>     server when it reads end-of-data.
>
>The DRDA protocol supports both a) and b), and there seems to be code
>for both of them in the network server. So in theory we can achieve
>this just by changing the client driver.
>
>  
>
This sounds like a correct analysis and a good solution.   A couple of 
areas of vague concern:

You might want to consider if there is any impact on the fix for
DERBY-213.  I think it should be fine, but popped into my mind as a red
flag.

Large objects tend to be very sensitive to these types of changes, so
you might want to give them some  thought for various scenarios
especially when autocommit is on.  Sorry I can't offer anything more
concrete.

Thanks for looking at this.

Kathey



Re: Message round-trips between Derby client and server

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
Bryan Pendleton <bp...@amberpoint.com> writes:

> Knut Anders Hatlen wrote:
>> I haven't been thinking about the details, but at least it seems like
>> this is what PostgreSQL and MySQL do.

I think I may have jumped to the wrong conclusions here. I don't think
PostgreSQL and MySQL are piggybacking ResultSet.close() on commit (and
I'm starting to agree with Dag that it's not desirable). Instead, I
think the entire result set is pre-fetched in executeQuery() (given
that the size of the result set is smaller than the default fetch
size), and if the result set is forward only, it can be closed on the
server side.

I have looked more at the behaviour of Derby's client driver. A simple
select query seems to be executed like this:

  (Let's for the sake of argument assume that the result set has N
  rows, where N is smaller than the default fetch size, and that we
  are using forward-only result sets.)

  1. PreparedStatement.executeQuery() is called. The client has one
     round trip to the server opening the result set. No rows are
     fetched.

  2. ResultSet.next() is called. The client has one round trip to the
     server fetching all the rows. The result set on the server side
     is exhausted, but still open.

  3. The next N-1 calls to ResultSet.next() use the pre-fetched rows
     and do not generate network activity.

  4. A last call to next() (which returns false) will have one round
     trip, and the result set on the server is closed. The result set
     on the client is still open.

  5. ResultSet.close() is called. Since the result set on the server
     is already closed, no network activity is generated. The result
     set on the client side is now closed.

  6. Connection.commit() is called, and a round trip to the server is
     triggered. The transaction is committed on the server.

This is how I understand the code and interpret what I see on the
network. Does anyone know if this is what actually happens?

Assuming I have got it right this time, this is what I propose that we
try to do:

  a) Pre-fetching of rows should be done in executeQuery() instead of
     the first next() call.

  b) Forward-only result sets should be implicitly closed on the
     server side when all rows have been fetched by the client, so
     that the client can assume that the result set is closed on the
     server when it reads end-of-data.

The DRDA protocol supports both a) and b), and there seems to be code
for both of them in the network server. So in theory we can achieve
this just by changing the client driver.

>> This blocking behaviour is an
>> issue only in read committed mode, right?
>
> I found your comment very interesting because it made me realize
> that I hadn't been thinking about how concurrency control and
> isolation levels play into this analysis.

Oh, no! Just when I started thinking they were irrelevant... :)

> So, a question, and an observation:
>
> 1) When you did the original analysis, are you sure that all
> 4 configurations that you tested (Derby client, DB2 client, MySQL,
> and Postgres) were using exactly the same isolation level and
> transaction settings? If you accidentally had one of your tests
> running with different settings, the measurements could have been off.

I ran them with default isolation levels: read committed in Derby and
PostgreSQL, and repeatable read in MySQL/InnoDB. I think isolation
levels would play a role if ResultSet.close() were to be piggybacked
on a subsequent JDBC call, but I don't see how they could affect the
number of round trips if my pre-fetch and implicitly-close hypothesis
is correct. At least when auto-commit is off.

Anyway, MySQL has the highest isolation level, but still has fewer
round trips than Derby.

> 2) Isn't it true that Postgres uses a versioning-based implementation
> of isolation, rather than a locking-based implementation?

PostgreSQL doesn't have any read locks. Writers create a new copy of
the data, and readers just read the old copy. Writers have to do some
kind of locking to ensure that only one transaction is updating a row.

> If so, that
> may have major implications for a benchmark like this, because Postgres
> is basically trading off increased complexity at the server side for
> less round-trips to communicate things like lock requests and releases.

Yes, it may, but I don't think it does. And regardless of what
actually happens in MySQL and PostgreSQL, I think a single-record
select using the Derby client driver should have no more than two
round trips: one to fetch the data and one to end the
transaction. Hopefully, I'll have time to get around and start looking
closer at it next week.

-- 
Knut Anders


Re: Message round-trips between Derby client and server

Posted by Bryan Pendleton <bp...@amberpoint.com>.
Knut Anders Hatlen wrote:
> I haven't been thinking about the details, but at least it seems like
> this is what PostgreSQL and MySQL do. This blocking behaviour is an
> issue only in read committed mode, right? 

I found your comment very interesting because it made me realize
that I hadn't been thinking about how concurrency control and
isolation levels play into this analysis.

So, a question, and an observation:

1) When you did the original analysis, are you sure that all
4 configurations that you tested (Derby client, DB2 client, MySQL,
and Postgres) were using exactly the same isolation level and
transaction settings? If you accidentally had one of your tests
running with different settings, the measurements could have been off.

2) Isn't it true that Postgres uses a versioning-based implementation
of isolation, rather than a locking-based implementation? If so, that
may have major implications for a benchmark like this, because Postgres
is basically trading off increased complexity at the server side for
less round-trips to communicate things like lock requests and releases.

thanks,

bryan


Re: Message round-trips between Derby client and server

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
"Dag H. Wanvik" <Da...@Sun.COM> writes:

> Hi,
>
>>>>>> "Knut" == Knut Anders Hatlen <Kn...@Sun.COM> wrote:
>
> Knut> 
> Knut>                                 | Derby | DB2 univ | Postgres | MySQL
> Knut> ---------------------------------------------------------------------
> Knut> DriverManager.getConnection     |   X   |    X     |    X     |   X
> Knut> Connection.setAutoCommit(false) |       |          |          |   X
> Knut> Connection.prepareStatement     |   X   |          |          |   X
> Knut> ---------------------------------------------------------------------
> Knut> PreparedStatement.executeQuery  |   X   |    X     |    X     |   X
> Knut> ResultSet.next                  |   X   |    X     |          |
> Knut> ResultSet.close                 |   X   |    X     |          |
> Knut> Connection.commit               |   X   |    X     |    X     |   X
> Knut> 
> Knut> The first three methods are not candidates for optimization, in my
> Knut> opinion, since a well-written application won't call them
> Knut> frequently. But I think we should do something with the last four
> Knut> methods.
> Knut> 
> Knut> My initial thought is that we should try to:
> Knut> 
> Knut>   1) Pre-fetch rows in PreparedStatement.executeQuery(), saving one
> Knut>      round trip per select statement.
> Knut> 
> Knut>   2) Delay ResultSet.close() and send it piggybacked on the next JDBC
> Knut>      call, saving one round trip per select statement when autocommit
> Knut>      is off.
>
> If the next JDBC communication won't take place for a while, this
> second optimization can significantly delay the release of result set
> locks, so I would be hesitant to recommend this optimization (as a
> default behavior, anyway), since other transactions might block,
> without the user realizing the cause of it.

I haven't been thinking about the details, but at least it seems like
this is what PostgreSQL and MySQL do. This blocking behaviour is an
issue only in read committed mode, right? In read uncommitted there
are no shared locks, and in repeatable read or serializable the read
locks won't be released until commit or rollback anyway. In read
committed mode I think the shared lock on a row is released when
ResultSet.next() steps to the next row. Therefore, the only case I can
think of where delaying ResultSet.close() would cause more blocking is
when

  1) auto-commit is off, and
  2) the isolation level is read uncommitted, and
  3) the cursor in the ResultSet on the server side is on a valid
     position (i.e. not exhausted), and
  4) there is a long interval between ResultSet.close() and the next
     JDBC call, and
  5) another transaction tries to obtain an exclusive lock on the row
     that is still locked.

Is this a common scenario? I think the ResultSet in most cases will be
exhausted on the server side because of the pre-fetching done by the
client driver. And in any case I believe that most applications either
commit or execute a new statement right after ResultSet.close().

-- 
Knut Anders


Re: Message round-trips between Derby client and server

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
Daniel John Debrunner <dj...@apache.org> writes:

> Dag H. Wanvik wrote:
>
>> Hi,
>> 
>> 
>>>>>>>"Knut" == Knut Anders Hatlen <Kn...@Sun.COM> wrote:
[...]
>> Knut> My initial thought is that we should try to:
>> Knut> 
>> Knut>   1) Pre-fetch rows in PreparedStatement.executeQuery(), saving one
>> Knut>      round trip per select statement.
>> Knut> 
>> Knut>   2) Delay ResultSet.close() and send it piggybacked on the next JDBC
>> Knut>      call, saving one round trip per select statement when autocommit
>> Knut>      is off.
>> 
>> If the next JDBC communication won't take place for a while, this
>> second optimization can significantly delay the release of result set
>> locks, so I would be hesitant to recommend this optimization (as a
>> default behavior, anyway), since other transactions might block,
>> without the user realizing the cause of it.
>
> Can we take advantage of any knowledge of how the embedded engine is
> closing ResultSets automatically, at least for forward only result sets?
> From the 50,000 foot level it seems to me that for a forward only result
> set that has been exhausted (rs.next() returned false) the close on the
> client should not need to send any communication to the server

Yes, I think we could close exhausted forward only result sets on the
server. The DRDA protocol actually has an optional parameter for this,
OPNQRY.qryclsimp, which "controls whether the target server implicitly
closes a non-scrollable query upon end of data (SQLSTATE 02000) in
association with the cursor type." It is not used by the client
driver, and the server default is NO. If we enable this in the client,
we could avoid a round trip in many cases and we wouldn't get the
locking issues Dag described.

OPNQRY (which is the command sent to the server in executeQuery())
also has another parameter, qryrowset, which the DRDA spec says
"specifies whether a rowset of rows is to be returned with the
command.  This is only honored for non-dynamic scrollable cursors
(...) and for non-scrollable cursors conforming to the limited block
query protocol." If I have understood this correctly, it means that we
can open a result set and request data in the same DRDA message, which
would save one round trip on the first call to next().

Unless someone says it's a very bad idea, I will file two separate
JIRA issues saying we should enable these features.

-- 
Knut Anders


Re: Message round-trips between Derby client and server

Posted by Daniel John Debrunner <dj...@apache.org>.
Dag H. Wanvik wrote:

> Hi,
> 
> 
>>>>>>"Knut" == Knut Anders Hatlen <Kn...@Sun.COM> wrote:
> 
> 
> Knut> 
> Knut>                                 | Derby | DB2 univ | Postgres | MySQL
> Knut> ---------------------------------------------------------------------
> Knut> DriverManager.getConnection     |   X   |    X     |    X     |   X
> Knut> Connection.setAutoCommit(false) |       |          |          |   X
> Knut> Connection.prepareStatement     |   X   |          |          |   X
> Knut> ---------------------------------------------------------------------
> Knut> PreparedStatement.executeQuery  |   X   |    X     |    X     |   X
> Knut> ResultSet.next                  |   X   |    X     |          |
> Knut> ResultSet.close                 |   X   |    X     |          |
> Knut> Connection.commit               |   X   |    X     |    X     |   X
> Knut> 
> Knut> The first three methods are not candidates for optimization, in my
> Knut> opinion, since a well-written application won't call them
> Knut> frequently. But I think we should do something with the last four
> Knut> methods.
> Knut> 
> Knut> My initial thought is that we should try to:
> Knut> 
> Knut>   1) Pre-fetch rows in PreparedStatement.executeQuery(), saving one
> Knut>      round trip per select statement.
> Knut> 
> Knut>   2) Delay ResultSet.close() and send it piggybacked on the next JDBC
> Knut>      call, saving one round trip per select statement when autocommit
> Knut>      is off.
> 
> If the next JDBC communication won't take place for a while, this
> second optimization can significantly delay the release of result set
> locks, so I would be hesitant to recommend this optimization (as a
> default behavior, anyway), since other transactions might block,
> without the user realizing the cause of it.

Can we take advantage of any knowledge of how the embedded engine is
closing ResultSets automatically, at least for forward only result sets?
>From the 50,000 foot level it seems to me that for a forward only result
set that has been exhausted (rs.next() returned false) the close on the
client should not need to send any communication to the server

Dan.


Re: Message round-trips between Derby client and server

Posted by "Dag H. Wanvik" <Da...@Sun.COM>.
Hi,

>>>>> "Knut" == Knut Anders Hatlen <Kn...@Sun.COM> wrote:

Knut> 
Knut>                                 | Derby | DB2 univ | Postgres | MySQL
Knut> ---------------------------------------------------------------------
Knut> DriverManager.getConnection     |   X   |    X     |    X     |   X
Knut> Connection.setAutoCommit(false) |       |          |          |   X
Knut> Connection.prepareStatement     |   X   |          |          |   X
Knut> ---------------------------------------------------------------------
Knut> PreparedStatement.executeQuery  |   X   |    X     |    X     |   X
Knut> ResultSet.next                  |   X   |    X     |          |
Knut> ResultSet.close                 |   X   |    X     |          |
Knut> Connection.commit               |   X   |    X     |    X     |   X
Knut> 
Knut> The first three methods are not candidates for optimization, in my
Knut> opinion, since a well-written application won't call them
Knut> frequently. But I think we should do something with the last four
Knut> methods.
Knut> 
Knut> My initial thought is that we should try to:
Knut> 
Knut>   1) Pre-fetch rows in PreparedStatement.executeQuery(), saving one
Knut>      round trip per select statement.
Knut> 
Knut>   2) Delay ResultSet.close() and send it piggybacked on the next JDBC
Knut>      call, saving one round trip per select statement when autocommit
Knut>      is off.

If the next JDBC communication won't take place for a while, this
second optimization can significantly delay the release of result set
locks, so I would be hesitant to recommend this optimization (as a
default behavior, anyway), since other transactions might block,
without the user realizing the cause of it.

Dag

Re: Message round-trips between Derby client and server

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
Olav Sandstå <ol...@gmail.com> writes:

> On 1/9/06, Kathey Marsden <km...@sbcglobal.net> wrote:
>> Are the additional round trips just on the first execution of a prepared
>> statement?
>
> No, it is on every execute of the query/transaction. The numbers are
> average of several thousand transactions, thus the preparing of the
> initial statement should not influence much :-)
>
>> One difference between  our client and the DB2 Universal JDBC Driver is
>> that JCC "defers prepares"
>> which  means that  it waits to prepare the statement until it is first
>> executed, so can save a single  round trip  on the first statement
>> execution.  This optimization was the source of many bugs, complexity in
>> the code, and differences in embedded and client behaviour and not
>> really that helpful in the common scenario where a statement is prepared
>> once and executed many times.
>
> Knut Anders actually commented on this when he studied the messages
> that different drives send and he could not see that the JCC driver
> sent the prepare operation in a separate message. We both agreed that
> this likely was an optimization, but did not think that for an
> application using prepared statements "correctly" this would have much
> impact (but I have seen automagically generated code using prepared
> statements, one execute of it and then a new prepare of the same
> statements.... :-( ).

I have monitored the network activity while using a debugger to step
through the top-level JDBC calls of a single-record select transaction
with different JDBC drivers. The table below shows which calls that
are generating activity on the network.

                                | Derby | DB2 univ | Postgres | MySQL
---------------------------------------------------------------------
DriverManager.getConnection     |   X   |    X     |    X     |   X
Connection.setAutoCommit(false) |       |          |          |   X
Connection.prepareStatement     |   X   |          |          |   X
---------------------------------------------------------------------
PreparedStatement.executeQuery  |   X   |    X     |    X     |   X
ResultSet.next                  |   X   |    X     |          |
ResultSet.close                 |   X   |    X     |          |
Connection.commit               |   X   |    X     |    X     |   X

The first three methods are not candidates for optimization, in my
opinion, since a well-written application won't call them
frequently. But I think we should do something with the last four
methods.

My initial thought is that we should try to:

  1) Pre-fetch rows in PreparedStatement.executeQuery(), saving one
     round trip per select statement.

  2) Delay ResultSet.close() and send it piggybacked on the next JDBC
     call, saving one round trip per select statement when autocommit
     is off.

Should we file these as JIRA enhancement issues?

-- 
Knut Anders


Re: Message round-trips between Derby client and server

Posted by Olav Sandstå <ol...@gmail.com>.
On 1/9/06, Kathey Marsden <km...@sbcglobal.net> wrote:
> Are the additional round trips just on the first execution of a prepared
> statement?

No, it is on every execute of the query/transaction. The numbers are
average of several thousand transactions, thus the preparing of the
initial statement should not influence much :-)

> One difference between  our client and the DB2 Universal JDBC Driver is
> that JCC "defers prepares"
> which  means that  it waits to prepare the statement until it is first
> executed, so can save a single  round trip  on the first statement
> execution.  This optimization was the source of many bugs, complexity in
> the code, and differences in embedded and client behaviour and not
> really that helpful in the common scenario where a statement is prepared
> once and executed many times.

Knut Anders actually commented on this when he studied the messages
that different drives send and he could not see that the JCC driver
sent the prepare operation in a separate message. We both agreed that
this likely was an optimization, but did not think that for an
application using prepared statements "correctly" this would have much
impact (but I have seen automagically generated code using prepared
statements, one execute of it and then a new prepare of the same
statements.... :-( ).

Olav

Re: Message round-trips between Derby client and server

Posted by Kathey Marsden <km...@sbcglobal.net>.
Olav Sandstaa wrote:

>We are looking into the network traffic between the Derby JDBC driver
>and the Network server and have compared this against MySQL and
>PostgreSQL. The main finding is that Derby sends more messages than
>the two other databases.
>
>For two simple transaction loads the following number of round-trips
>between the client and server is done:
>
>           Derby     MySQL    PostgreSQL
>----------------------------------------  
>"Select"     4         2          2
>"TPC-B"      8         6          6
>
>  
>
Are the additional round trips just on the first execution of a prepared
statement? 

One difference between  our client and the DB2 Universal JDBC Driver is
that JCC "defers prepares"
which  means that  it waits to prepare the statement until it is first
executed, so can save a single  round trip  on the first statement
execution.  This optimization was the source of many bugs, complexity in
the code, and differences in embedded and client behaviour and not
really that helpful in the common scenario where a statement is prepared
once and executed many times.

Kathey