CS 440 Database Management Systems Stored procedures & OR mapping 1

advertisement
CS 440
Database Management Systems
Stored procedures & OR mapping
1
Writing Large DB Programs
• How to to write large scale programs that use a database?
• SQL
– cannot express all types of queries: e.g., recursive
queries.
– no support for advanced features, e.g., GUI.
• Embed SQL in a programming language to write
programs that contain many queries
– Libraries to submit SQL queries as strings to RDBMS
– PHP, Java: JDBC,
2
Problems with Using SQL in PL
• The programs are hard to debug and maintain
• Programmers have to run their programs to find SQL
compilation bugs.
• If a programmer adds an attribute to a table, she has to
manually find and change many lines in the source code.
• Communication overhead
– queries are sent to RDBMS per each call.
3
Stored Procedures
•
•
•
•
Programming modules written in a procedural language.
Stored in the database.
Compiled and run by RDBMS.
Most RDBMS provide their own language to write stored
procedures.
– Oracle: PL / SQL
– SQL Server: Transact-SQL
• Some RDBMSs use widely used programming languages
– VoltDB: Java
4
Example: PL/SQL
• Create a stored procedure that inserts the integer numbers
between 1 and 100 into table
NumOddEven(numVal, oddEven).
CREATE OR REPLACE PROCEDURE sample IS
BEGIN
FOR i IN 1.. 100 LOOP
IF MOD(i,2) = 0 THEN
-- i is even
INSERT INTO numOddEven VALUES (i, ‘i is even’);
ELSE
INSERT INTO numOddEven VALUES (i, ‘i is odd’);
END IF;
END LOOP;
COMMIT;
END
5
Example: PL/SQL
• Called by user for from a program.
> Execute sample
• Users can pass input parameters to stored procedures
– For example, the number of values to insert in table
NumOddEven.
6
Advantages of Stored Procedures
• They are more expressive than SQL.
• It is easy to use SQL in them
– SQL queries are parts of the programming language.
– many errors are detected in compilation phase.
• They run faster than using SQL in programming
languages such as PHP
– minimal communication overhead for submitting queries
to RDBMS.
• and sometimes no communication overhead for results
– queries are pre-compiled and may be pre-optimized.
7
Disadvantages of Stored Procedures
• They do not support some advanced features
– e.g., graphical user interface.
• Each RDBMS has its own.
– If we write a program over a RDBMS, we cannot run it
over another one.
• They are harder to write, debug, and maintain than the
programs written in Java or C++.
8
When to use Stored Procedures
• Generally Speaking:
– They should be used for portions of the program whose
performances matter a lot.
• ID generation
– They should contain minimal amount of business logic
• keep them simple so they are easy to debug and maintain.
9
Other problems with using SQL in PL
• Programmers must deal with two different data models
– Relational model
create table Bars(name varchar(50),
addr varchar(100)
…)
– object-oriented model
public class Bars {
private String name;
private String addr;
…}
10
Object Relational Mapping (ORM)
• ORM tools hide relational model from the programmers
(as much as possible)
• Programmers deal with only object oriented data model.
• They write small number of SQL queries or no SQL
query at all.
11
How an ORM tool work
1. Programmers write their programs in an object oriented
language.
2. They let the ORM tool know which objects to store in
DB (as tables).
3. The ORM tool generates the SQL queries to create
tables and retrieve/manipulate the data.
12
Example
• Various tools for many programming languages.
– Hibernate, Zend, …
• We use Hibernate (www.hibernate.org) for Java in our
example.
13
Example
• We like to write a Java program that stores and retrieves
information about bars, where each bar has name,
address, and license.
• We write a class in Java for Bar.
public class Bars implements Serializable {
private String name;
private String addr;
private String license;
Interface for persistent
…}
classes
14
Example, cont’d
• We add setter and getter methods for member
variables of the class.
public class Bars implements Serializable{
private String name;
private String addr;
private String license;
// Setter and getter methods
public void setName(string name){
this.name = name;
}
public String getName(){
return name;
}
…
}
15
Example, cont’d
• DBA creates table Bars(name, addr, license) in DB. (or
programmer)
create table Bars ( name varchar(50),
addr varchar(100),
license varchar(100)
…)
• DBA writes the relationship between the relation and the
Java class in a configuration file
– Bars.name  Bars.name
– Bars.addr  Bars.addr
– Bars.license  Bars.license
16
Example: Cont.
try{
SessionFactory factory =
new Configur().configure().buildSessionFactory();
Session session = factory.openSession();
Bars JoeBar = new Bars();
JoeBar.setName(“Joe Bar”);
JoeBar.setaddr(“12 Madison St. Corvallis,OR,97331”);
JoeBar.setLicense(“license324”);
session.save(JoeBar);
…
}
catch(Exception ex){…}
Hibernate reads the configuration
finally{
file, generate the insert query, and
session.close();
inserts a new tuple into the DB.
}
17
ORM Configuration file
• We can describe integrity constraints over a single table
– Not NULL
– Keys
–…
• We can describe relationships between objects (tables)
– Primary key to foreign key constraints
– One to one, one to many
18
Advantages of ORM Tools
• Programmers write minimal amount of SQL queries.
– DBA may do most of the RDBMS related work.
• Our programs will work over various RDBMSs.
– Test the program over MySQL, deploy it over Oracle
19
Disadvantages of ORM Tools
• They are slower than stored procedures
– they are outside DB
– they may be slower than using SQL in PL
– too many function calls.
• We cannot use customized and fast commands of an
RDBMS.
• They are generally harder to learn than JDBC and PHP
– API + configuration files
20
When to Use What
• There is no exact rule!
• Generally speaking:
– small programs: SQL in programming language (JDBC, PHP)
– small/moderate business logic + performance => stored
procedures
– complex business logic + large scale program: ORM
• We usually use a mixed of these techniques.
– e.g., separate small parts of the program where
performance is essential, and make them stored procedures
and use ORM for other parts.
21
Download