Java DB Help (was: Please help with SQL/Join Trouble)

Status
Not open for further replies.
So, I've been provided with a database structure and the corresponding data from an upstream provider, which looks kinda like:

Code:
PARENT
------------------
PARENT_INDEX
.
. 411 other fields...
.

CHILD_SEQUENCE     -- 192991 rows
------------------
PARENT_INDEX
CHILD_SEQUENCE_NUM       -- Gives the order to the values of the sequence
CHILD_INDEX         
CHILD_SEQ_ID

CHILD_SEQUENCE_NAME      -- this just gives an optional name to the sequence
-------------------
PARENT_INDEX
CHILD_SEQ_ID
CHILD_SEQ_NAME

CHILD_VALUES      -- 171368 rows
----------------
PARENT_INDEX
CHILD_INDEX
CHILD_VALUE1
CHILD_VALUE2

what I need in order to build and link into the parent data structure:
parent_index, child_seq_id, child_seq_name, child_sequence_num, child_index, child_value1, child_value2

I started out with a single join to get the parent and child relations:

Code:
select 
       cs.parent_index, cs.child_sequence_num, cs.child_index, cs.child_seq_id, 
       c.child_index, c.child_value1, c.child_value2 
  from 
       child_sequence cs
  join child_values c 
    on cs.parent_index = c.parent_index and 
       cs.child_index = c.child_index

which runs fairly fast.

Then, to add the sequence name to the result, I add the second join:

Code:
select 
       cs.parent_index, cs.child_sequence_num, cs.child_index, cs.child_seq_id, 
       csn.child_seq_name,
       c.child_index, c.child_value1, c.child_value2 
  from 
       child_sequence cs
  join child_values c 
    on cs.parent_index = c.parent_index and 
       cs.child_index = c.child_index
  join child_sequence_name csn
    on cs.parent_index = csn.parent_index and
       cs.child_seq_id = csn.child_seq_id

But this never seems to finish (I usually wait up to 15m to make sure.) I'm guessing I'm accidentally causing a cross join somehow?

I'm correct in my thinking that I should never have more than SELECT COUNT(1) FROM CHILD_SEQUENCE rows in my final query results, yes?
 

J^M

Ars Scholae Palatinae
815
Subscriptor++
But this never seems to finish (I usually wait up to 15m to make sure.) I'm guessing I'm accidentally causing a cross join somehow?
The behavior is consistent. I'm assuming this code isn't a copy/paste... check the real thing to be sure it doesn't look something like this:
cs.child_seq_id = cs.child_seq_id
I've embarrassed myself once or twice that way.
 
FoamingPipeSnake":1emp2w5c said:
Missing or bad index on child_sequence_name? What does the sql plan output say?

Um... Any idea how I would generate that using Derby (Java DB)? [Another limitation foisted upon me. Not that its a crappy DB (is it?) but that I don't know it very well.]

I neglected to show indexes, too.

On CHILD_SEQUENCE, there is a PRIMARY KEY/INDEX using PARENT_INDEX, CHILD_SEQUENCE_NUM, CHILD_SEQ_ID.
On CHILD_SEQUENCE_NAME there is a PRIMARY KEY/INDEX comprising of PARENT_INDEX and CHILD_SEQ_ID.
On CHILD_VALUES, there's a PRIMARY_KEY/INDEX comprising of PARENT_INDEX and CHILD_INDEX.

J^M":1emp2w5c said:
But this never seems to finish (I usually wait up to 15m to make sure.) I'm guessing I'm accidentally causing a cross join somehow?
The behavior is consistent. I'm assuming this code isn't a copy/paste... check the real thing to be sure it doesn't look something like this:

What? People don't use super-generic terms in their real-life databases? ;)

cs.child_seq_id = cs.child_seq_id
I've embarrassed myself once or twice that way.

Luckily, no, nothing like that. That WOULD be embarrassing, but I was pretty diligent trying to keep all that stuff lined up.
 
Never mind, I got the execution plan report... lemme see if I can make some sense of it.

I'm guessing this is the bad piece

Code:
Source result set:
  Nested Loop Exists ResultSet
  .
  .
  Left result set:
    Nested Loop Join ResultSet:
    .
    .
    Left result set:
      Table Scan ResultSet for CHILD_VALUES at read committed isolation level using instantaneous share row locking chosen by the optimizer.

All the other ResultSet's show as Index Scans. It appears to be ignoring the PK/index on CHILD_VALUES... gonna manually add separate indexes for PARENT_INDEX and CHILD_INDEX to see if it helps.
 
I don't see the value in indexing a name/desc field, but do see the value in indexing any foreign keys. Derby supports creating indexes...

What's provided is very abstracted. Data type helps, because the more "narrow" (small) the data type is - the better the JOIN performance. IE: INT is better than GUID, but it really depends on what your model is. But I'd need a good reason to use GUIDs over INT. And you never expose your primary key to users...
 
Well, I've given up; I'll just do the "join" in memory. Since this is the last bit I have to accomplish to assemble my object hierarchy other than some CLOB "comment" fields AND I'm already, technically, doing multiple other joins in memory (parent-to-child assembly), may as well...

Now to tackle those CLOB fields and not make it take an hour... maybe I'll make them load on demand... hrm...
 
Okay, so I'm slurping all the text out of CLOBs and it is taking forever... well, relatively -- it doubled the time that it takes to build my object graph from 20 seconds to over 40 seconds.

I've tried the simplest:

Code:
dataItem.setComment(resultSet.getString("CLOB_FIELD_NAME"))

and

Code:
dataItem.setComment(slurp(resultSet.getClob("CLOB_FIELD_NAME").getCharacterStream()))

where slurp(Reader r) implemented in a couple ways:

Code:
String slurp(Reader r) throws IOException {
  try (BufferedReader rdr = new BufferedReader(r)) {
    StringBuilder sb = new StringBuilder();
    String line;
    while((line = rdr.readLine()) != null) {
      sb.append(line).append("\n"));
    }
    return sb.toString();
  }
}

and

Code:
String slurp(Reader r) throws IOException {
  StringBuilder sb = new StringBuilder();
  char[] buffer = new char[BUFFER_SIZE];
  int bytes = r.read(buffer);
  while (r.ready() && bytes > -1) {
    sb.append(buffer, 0, bytes);
    bytes = r.read(buffer);
  }
  return sb.toString();
}

with buffer sizes at 2K, 4K, and 6K. Nothing seriously improves the timing, although raising the buffer size seems to help by a few seconds over a 40 second total runtime.

After profiling, it looks like I'm spending my time reading from the network socket. Is there any obvious way to speed up CLOB reads? Compression settings?

Thanks for any further insight!
 

Redshift

Ars Scholae Palatinae
1,260
I have spent some time in the derby codebase recently.

-- Foreign key column(s) automatically get an index, even if you don't declare it. And they must reference a primary key or unique index in the referenced/parent table.

-- Derby is barely under development, only a handful of minor changes in the last few releases going back several years.

-- The JDBC driver is implemented using a ancient and obsolete DRDA protocol from DB2 first drafted in like 1989 if I remember correctly. It does really really stupid stuff--the over the wire format for Timestamp types, for example: derby driver uses a Calendar object to extract the individual fields of the date/time, appends those to a string, encodes the string as ASCII bytes, and send those over the wire, then does the reverse client side. Why not send the 8 byte integer used to represent the timestamp in storage?

-- The network stack pre-dates java new io. All network code is blocking and there is one thread per connection.

-- Even with a small data set, say 10,000 rows, it is possible to write queries (subqueries) that run for 15 minutes in derby but are sub-second on Postgres.

-- H2 is under active development. It has nowhere near as much query optimization/re-writing as Postgres, but generally performs much better than derby because it uses sane serialization in its jdbc driver.
 
I'd like to be able to use a "real" stand alone DB, but the original tool that talks to the DB (and built the DB structure and all) doesn't support it. Hopefully, we'll be able to figure out a way to (a) talk to the developer's of that tool to allow other DBs [unlikely], (b) reverse engineer the tool and remove the dependency on Derby [ugh], or (c) create our own tool to manage the DB [ugh, but more flexibility.]

Anyhow, we're mainly a Java shop here, running our processes on Solaris with backend DB being Oracle RDBMS. So, I've been working all this from the Solaris/UNIX side.

For S&Gs, I checked out my code on the Windows workstation, and, after about 20 minutes waiting for the draconian A/V to scan All The Things(tm), had NetBeans up-and-running, cranked up Derby [more A/V waiting], compiled my source [even more A/V waiting] and running [and still more A/V waiting], I find that performance is NEARLY DOUBLE that of the Solaris environment (which includes NFS working directories and LDAP.) Also, my trouble with the "hanging" query has disappeared.

With the DB and my program running on the same machine, Solaris "load time" is, on average, 38-45 seconds; on Windows the same is 25-27 seconds.

Thinking that the performance might be due to NFS, I moved my DB directory to /tmp (a non-NFS directory) and tried again. Nope, same performance.

For further S&Gs, I tried going from Solaris client to a Windows server and vice versa. Both of those were dreadfully slow, up in the 150 second timeframes.

So, platform matters. (Duh!)

(Side note: going back and doing everything on the Solaris side my "hanging" query no longer does that. I must've had a bad .class file or something that didn't want to get overwritten because I didn't change the query at all. :sigh:)
 
Status
Not open for further replies.