0% found this document useful (0 votes)
52 views

Java DB Performance: Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Java DB is Sun's supported distribution of Apache Derby, a SQL database written entirely in Java. It can be used embedded within Java applications or in a client-server architecture. The document discusses several tips for optimizing Java DB performance such as separating data and log devices, increasing the database buffer size, using prepared statements, avoiding table scans with indexes, and understanding locking issues. It also provides examples of query plan and runtime statistics and how to override the optimizer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Java DB Performance: Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Java DB is Sun's supported distribution of Apache Derby, a SQL database written entirely in Java. It can be used embedded within Java applications or in a client-server architecture. The document discusses several tips for optimizing Java DB performance such as separating data and log devices, increasing the database buffer size, using prepared statements, avoiding table scans with indexes, and understanding locking issues. It also provides examples of query plan and runtime statistics and how to override the optimizer.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Java DB Performance

Olav Sandst
Sun Microsystems, Trondheim, Norway
Submission ID: 86

AGENDA
! "ava D# introduction
! $on%i&urin& "ava D# %or 'er%ormance
! (ro&rammin& ti's
! )nderstandin& "ava D# 'er%ormance
! O'en*source database 'er%ormance

Java DB
! Sun+s su''orted distribution o% ,'ache Derby
- all develo'ment done in the ,'ache Derby community
! $om'lete S./ database includin&:
- views, tri&&ers, stored 'rocedures, %orei&n 0eys
- multi*user transaction su''ort
! Security:
- data encry'tion, client authentication, 12,NT3245O64
! Standard based:
- "D#$ 78 and S./9:3993:;3<M/
! => "ava, bundled in Sun "D6 6 and 1lass%ish
! The database %or "ava a''lications


Java DB Architecture: Embedded
Log and Data
"
a
v
a

5
i
r
t
u
a
l

M
a
c
h
i
n
e
"D#$
S./
,ccess
Stora&e
Database buffer
,''lication
! Include derby8?ar in your
class'ath
! #oot the "ava D# en&ine
=@
Class.forName(
org.apache.derby.jdbc.
EmbeddedDriver);
! $reate a new database
Connection conn
Driver!anager.getConnection(
jdbc"derby"dbName; #
createtr$e);
=@ O'tional when runnin& with "D6 6

Java DB Architecture: Client-erver
Log and Data
"ava 5irtual Machine
"D#$
S./
,ccess
Stora&e
Database buffer
Networ0 Server
Application
"D#$
Application
"D#$
Application
"D#$

AGENDA
! "ava D# introduction
! Configuring Java DB for !erformance
! (ro&rammin& ti's
! )nderstandin& "ava D# 'er%ormance
! O'en*source database 'er%ormance

Performance "i! #:
e!arate Data and Log Devices
Log on se!arate dis$:
! )tiliAe seBuential write bandwidth
on dis0
! $on%i&uration:
"D#$ connection url:
logDevice%path&
Performance ti!:
! )se se'arate dis0s %or data and lo& device

Dis$ Activit%
Disk head movement
for 5 seconds of
database activity
Data and lo&
on same dis0
Data and lo&
on se'arate dis0s

Performance "i! &:
"une Database Buffer i'e
! $ache o% %reBuently used data
'a&es in memory
! $ache*miss leads to read %rom
dis0
! SiAe:
- de%ault 7 M#
-
derby.storage.pageCache'i(e
Performance ti!:
! Increase the siAe o% the database bu%%er to &et %reBuently accessed
data in memory

Performance and Durabilit%:
Data and Log Devices
Log device:
! SeBuential write o% transaction
lo&
! Synchronous as 'art o% commit
! 1rou' commit
Data device:
! Data re&ularly written to dis0 as
'art o% chec0'oint
! Data read %rom dis0 on demand
Data
"ava 5irtual Machine
"ava D#
Database
buffer
Log
buffer
Log

Performance "i! (:
"rade Durabilit% for Performance
Log device configuration:
! Dis0+s write cache:
- disabled
- enabled
! Disable durability:
derby.system.d$rabilitytest
- lo& %lushed to dis0 after commit
)A*N+NG: Crite cache reduces 'robability o% success%ul recovery a%ter
'ower %ailure

Log Device Configuration:
Effect on Durabilit%
durabilit%
, test
)rite
cache
No -rite
cache
durabilit%
, test
)rite
cache
No -rite
cache

=
:
;
7
D
6
E
8
9
=
Process crash Po-er failure
.ailed to recover
Loss of u!dates
uccessfull recover%
T
e
s
t
Durabilit% ti!:
Disable the dis0+s write cache on the lo& device

Performance "i! /:
0se Embedded Java DB
(er%ormance advanta&es:
! saves inter*'rocess or server
communication
! reduces $() usa&e
! reduces hardware cost
(otential issues:
! scalability Fone machine@
! "5M con%i&uration
"D#$
Java DB
A!!lication

Glassfish and Java DB:
Client-erver vs Embedded1 e2am!le
$lient*Server 4mbedded

=
:
;
7
D
6
E
8
9
=
==
=:
=;
Throu&h'ut
$lient*Server 4mbedded

:8D
D
E8D
=
=:8D
=D
=E8D
:
::8D
:D
:E8D
;
;:8D
;D
;E8D
$() )sa&e Fms@
"ava D# 1lass%ish
"D#$
Java DB
Glassfish
Glassfish
"D#$
Glassfish
Java DB

AGENDA
! "ava D# introduction
! $on%i&urin& "ava D# %or 'er%ormance
! Programming ti!s
! )nderstandin& "ava D# 'er%ormance
! O'en*source database 'er%ormance

Performance "i! 3:
0se Pre!ared tatements
! $om'ilation o% S./ statements is eG'ensive:
'tatement s c.create'tatement();
)hile (...) *
s.e+ec$te,$ery('E-EC. / 012! t 34E1E a # id);
5
- &enerates "ava byte code and loads &enerated classes
! (re'ared statements eliminate this cost:
6repared'tatement s
c.prepare'tatement('E-EC. / 012! t 34E1E a 7);
)hile (...) *
s.set8nt(9: id);
s.e+ec$te,$ery();
5
- &enerated "ava byte code can be "IT com'iled

0se Pre!ared tatements1 e2am!le
"hrough!ut: CP0 usage:
(re'aredStatement Statement

8:D
8D
8ED
=
=8:D
=8D
=8ED
:
:8:D
:8D
:8ED
$
(
)

u
s
a
&
e

F
m
s
3
t
G
@
Performance ti!:
! 0E 're'ared statements * and *E0E them
=8
Performance "i! 4:
Avoid "able cans
Two ways o% locatin& data:
! Table scan: reads the entire table
! IndeG: %inds the data by readin& a %ew bloc0s
,void table scans:
! )se indeGes to o'timiAe %reBuently used access 'aths:
C1E;.E 8NDE< inde+Name 2N tableName (col$mn)
=9
Avoid "able cans1 e2am!le
= = = = = 6 = 6 = M
8=
8=
=
=
=
=
2etrieval time %or one record
IndeG
No indeG
Table siAe Fnumber o% records@
2
e
s
'
o
n
s
e

t
i
m
e

F
m
s
@
Performance ti!:
! $reate and use indeGes
:
Performance "i! 5:
6el! the Database to Perform
"D#$ ti's:
! $lose "D#$ ob?ects a%ter in use
- $onnections, Statements, 2esultSets, Streams
! )se transactions * do not rely on auto*commit
- (articularly %or insert3u'date3delete o'erations
! #atch u'dates reduce networ0 tra%%ic

AGENDA
! "ava D# introduction
! $on%i&urin& "ava D# %or 'er%ormance
! (ro&rammin& ti's
! 0nderstanding Java DB !erformance
! O'en*source database 'er%ormance
::
Performance "i! 7:
8no- the Load and the *esource 0sage
! 6now the load on the database:
derby.lang$age.log'tatement.e+ttr$e
- all eGecuted Bueries written to derby8lo&
! 6now how the Bueries are eGecuted:
derby.lang$age.log,$ery6lantr$e
! .uery*'lan and run*time statistics:
'='C'>?.8-.'='C'>'E.>1?N.8!E'.;.8'.8C'(9)
'='C'>?.8-.'='C'>'E.>'.;.8'.8C'>.8!8N@(9)
'='C'>?.8-.'='C'>@E.>1?N.8!E'.;.8'.8C'()
! )se OS and "ava tools to %ind resource usa&e:
- $(), memory, dis0 IO %or lo& and data device
Performance ti!:
! )se the available tools to understand what the database is doin& and
where resources are s'ent

E2am!le:
9uer% Plan and *un-time tatistics
AA enable r$nBtime statistics
'tatement s c.create'tatement();
s.e+ec$te?pdate(CC;-- '='C'>?.8-.'='C'>'E.>1?N.8!E'.;.8'.8C'(9)C);
s.e+ec$te?pdate(CC;-- '='C'>?.8-.'='C'>'E.>'.;.8'.8C'>.8!8N@(9)C);
AA e+ec$te D$ery
1es$lt'et rs s.e+ec$te,$ery(C'E-EC. .9.cE from .9: .E
)here .9.cE .E.cE and .9.cE % FGGC);
)hile (rs.ne+t()) *5
rs.close();
AA retrieve D$ery plan and r$nBtime statistics
rs s.e+ec$te,$ery(CH;-?E'
'='C'>?.8-.'='C'>@E.>1?N.8!E'.;.8'.8C'()C);
rs.ne+t();
'tring str rs.get'tring(9);
'ystem.o$t.println(C,$ery 6lan" C # str);
T= T:
"oin
(ro?
:7
Performance "i! ::
;!timi'er ;verrides
! Override eGecution strate&y selected by o'timiAer
! Horce use o% s'eci%ic indeG:
'E-EC. / 012! t9 BBDE1I=B6126E1.8E' inde+t9>c9
34E1E c99
! Horce use o% constraint:
'E-EC. / 012! t9 BBDE1I=B6126E1.8E' constraintc
34E1E c99 and cEJ
! Horce s'eci%ic "OIN order and "OIN strate&y:
'E-EC. / 012! KBDE1I=B6126E1.8E' join2rder08<ED
t9:tE KBDE1I=B6126E1.8E' join'trategyNE'.ED-226
34E1E t9.c9tE.c9
- "oin strate&ies: I,SI and N4ST4D/OO(
:D
;!timi'er ;verrides E2am!le

'E-EC. t9.cE 012! KBDE1I=B6126E1.8E' join2rder08<ED
t9: tE KBDE1I=B6126E1.8E' join'trategyNE'.ED-226
34E1E t9.cE tE.cE
Performance ti!:
! )se o'timiAer overrides * but onl% when needed
;!timi'er overide Estimated cost CP0 <ms=
None :;== =:8
?oinOrderJHI<4D :DD =:8
?oinStrate&yJN4ST4D/OO( ;77 =78E
HI<4D and N4ST4D/OO( ;77 =787

Performance "i! #>:
0nderstand Loc$ing +ssues
! /oc0 based concurrency control
! Isolation level:
- 2educin& isolation level increases concurrency
! /oc0 escalation:
- De%ault: escalation %rom ro- loc$s to table loc$s when D loc0s
are set on the table
-
derby.locLs.escalation.hreshold9GG
-
-2CM .;I-E t9 8N *'4;1ENE<C-?'8HE5 !2DE
! Deadloc0 tracin&:
-
derby.locLs.monitortr$e
-
derby.locLs.deadlocL.racetr$e

0nderstand Loc$ing +ssues
2etrieve loc0 in%ormation:
'E-EC. / 012! '='C'>D8;@.-2CM>.;I-E
<8D N.=6E N!2DEN.;I-EN;!E N-2CMN;!E N'.;.EN8NDE<N;!E
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
9FO N123 N< N.E N(9:P) N@1;N.N
9FQ N123 N' N.E N(9:P) N3;8. N
9FF N123 N< N.9 N(9:99) N@1;N.N
9FO N123 N' N.9 N(9:99) N3;8. N
9FO N123 N' N.9 N(9:9) N@1;N.N',-GRGQESGEJ
9FF N123 N' N.9 N(9:9) N@1;N.N',-GRGQESGEJ
9FQ N123 N< N.9 N(9:R) N@1;N.N
9FF N123 N' N.9 N(9:R) N3;8. N
9FO N.;I-EN8< N.E N.ablelocL N@1;N.N
9FQ N.;I-EN8' N.E N.ablelocL N@1;N.N

AGENDA
! "ava D# introduction
! $on%i&urin& "ava D# %or 'er%ormance
! (ro&rammin& ti's
! )nderstandin& "ava D# 'er%ormance
! ;!en-source database !erformance
:9
Java DB #>?(: Performance +m!rovements
4mbedded:
! reduced synchroniAation K conteGt switches
! reduced $() usa&e
! reduced number o% dis0 u'dates to lo& device
! concurrent read3writes on data device
$lient*server:
! im'roved streamin& o% /O#s
S./ O'timiAer:
! im'roved o'timiAation
;*:> increased
throu&h'ut on
sim'le Bueries

Performance +m!rovement1 e2am!le
)'&radin& to Sun "D6 6 and "ava D# =8;8= beta:
/oad: Select one record in a table
4mbedded: $lient*server:
;=
;!en-ource Database Performance
Databases:
! "ava D# =8;8= beta
- embedded
- client*server
! (ost&reS./ 88=88
! MyS./ D88;;
- with InnoD#
Load clients:
#? elect load:
= sin&le*record select
&? 0!date load:
; u'dates, = insert, = select
"est Configuration:
! LOut*o%*the*boGM
! D M# database bu%%er
! Data and lo& on se'arate dis0s
! SunHire v:A F: ,MD O'teron@
! Solaris =, Sun "ava S4 6
;:
"hrough!ut: ingle-record elect
Main*memory database F= M#@: Dis0*based database F= 1#@:
;;
"hrough!ut: 0!date Load
Main*memory database F= M#@: Dis0*based database F= 1#@:

ummar%
! Trade*o%%s between durability and 'er%ormance
- 0now your reBuirements and select care%ully
! 6now what in%luence 'er%ormance
- "ava D# con%i&uration
- user a''lication
! Ti's and tools to %ind and solve 'er%ormance bottlenec0s
"ava D# (er%ormsN
- $om'arable to other o'en*source databases
;lav andst@ htt!:AAdevelo!ers?sun?comABavadbA
un Cicros%stems olavDsun?com

You might also like