Download The PHP/Oracle Experience: Building for Scalability

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

SQL wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

PL/SQL wikipedia , lookup

Oracle Database wikipedia , lookup

Transcript
<Insert Picture Here>
The PHP/Oracle Experience: Building for
Scalability
Kuassi Mensah
Group Product Manager, Java Platform Group
http://db360.blogspot.com
PHP/Oracle: Building for Scalability
9PHP
• What is PHP
• Agile Web Development
•
•
•
•
Oracle Database
Using Oracle with PHP
PHP/Oracle: Building for Performance and Scalability
What’s New
What is PHP?
• Most Popular Scripting Language
• Simple/Versatile
• Most popular language after Java, C and C++
• Dynamically typed
• PHP5 has OO capabilities
• Free
• Open Source
• BSD-style license
• Multi-platform
• Widespread, and Strong Community
• 20+ million sites running PHP
PHP Enables Agile Web Development
• Rapid, Iterative, and Production Quality Applications
development
• High-Performance, and large scale Web Applications
deployment
• Integrated, Web front ends to Existing/Heterogeneous
systems
• Excellent Text & XML Processing
• Web 2.0, Web Services, and SOA
Basic PHP Example
<html>
<head> Basic PHP Example </head>
<body>
<p>Agent: So who do you think you are? <p/>
<?php
// print output
echo 'I am Neo, but people call me The One.';
?>
</body>
PHP Extensions
NET Functions, Apache-specific Functions, Alternative PHP Cache, Advanced PHP debugger, Array Functions, Aspell functions [deprecated],
BCMath Arbitrary Precision Mathematics Functions, PHP bytecode Compiler, Bzip2 Compression Functions, Calendar Functions, CCVS API
Functions [deprecated], Class/Object Functions, Classkit Functions, ClibPDF Functions, COM and .Net (Windows), Crack Functions, Character
Type Functions, CURL, Client URL Library Functions, Cybercash Payment Functions, Credit Mutuel CyberMUT functions, Cyrus IMAP
administration Functions, Date and Time Functions, DB++ Functions, Database (dbm-style) Abstraction Layer Functions, dBase Functions, DBM
Functions [deprecated], dbx Functions, Direct IO Functions, Directory Functions, DOM Functions, DOM XML Functions, enchant Functions,
Error Handling and Logging Functions, Exif Functions, Expect Functions, File Alteration Monitor Functions, Forms Data Format Functions,
Fileinfo Functions, filePro Functions, Filesystem Functions, Filter Functions, Firebird/InterBase Functions, Firebird/Interbase Functions
(PDO_FIREBIRD), FriBiDi Functions, FrontBase Functions, FTP Functions, Function Handling Functions, GeoIP Functions, Gettext, GMP
Functions, gnupg Functions, Net_Gopher, hash Functions, HTTP, Hyperwave Functions, Hyperwave API Functions, IBM DB2, Cloudscape and
Apache Derby Functions, ICAP Functions [removed], iconv Functions, ID3 Functions, IIS Administration Functions, Image Functions, IMAP,
POP3 and NNTP Functions, Informix Functions, Informix Functions (PDO_INFORMIX), Ingres II Functions, IRC Gateway Functions, PHP / Java
Integration, JSON Functions, KADM5, LDAP Functions, libxml Functions, Lotus Notes Functions, LZF Functions, Mail Functions, mailparse
Functions, Mathematical Functions, MaxDB PHP Extension, MCAL Functions, Mcrypt Encryption Functions, MCVE (Monetra) Payment
Functions, Memcache Functions, Mhash Functions, Mimetype Functions, Ming functions for Flash, Miscellaneous Functions, mnoGoSearch
Functions, Microsoft SQL Server Functions, Microsoft SQL Server and Sybase Functions (PDO_DBLIB), Mohawk Software Session Handler
Functions, mSQL Functions, Multibyte String Functions, muscat Functions, MySQL Functions, MySQL Functions (PDO_MYSQL), MySQL
Improved Extension, Ncurses Terminal Screen Control Functions, Network Functions, Newt Functions, NSAPI-specific Functions, Object
Aggregation/Composition Functions, Object property and method call overloading, Oracle Functions, ODBC Functions (Unified), ODBC and DB2
Functions (PDO_ODBC), oggvorbis, OpenAL Audio Bindings, OpenSSL Functions, Oracle Functions [deprecated], Oracle Functions
(PDO_OCI), Output Control Functions, Ovrimos SQL Functions, Paradox File Access, Parsekit Functions, Process Control Functions, Regular
Expression Functions (Perl-Compatible), PDF Functions, PDO Functions, PHP Options&Information, POSIX Functions, Regular Expression
Functions (POSIX Extended), PostgreSQL Functions, PostgreSQL Functions (PDO_PGSQL), Printer Functions, Program Execution Functions,
PostScript document creation, Pspell Functions, qtdom Functions, Radius, Rar Functions, GNU Readline, GNU Recode Functions, RPM Header
Reading Functions, runkit Functions, Satellite CORBA client extension [deprecated], SDO Functions, SDO XML Data Access Service Functions,
SDO Relational Data Access Service Functions, Semaphore, Shared Memory and IPC Functions, SESAM Database Functions, PostgreSQL
Session Save Handler, Session Handling Functions, Shared Memory Functions, SimpleXML functions, SNMP Functions, SOAP Functions,
Socket Functions, Standard PHP Library (SPL) Functions, SQLite Functions, SQLite Functions (PDO_SQLITE), Secure Shell2 Functions,
Statistics Functions, Stream Functions, String Functions, Shockwave Flash Functions, Sybase Functions, TCP Wrappers Functions, Tidy
Functions, Tokenizer Functions, Unicode Functions, URL Functions, Variable Handling Functions, Verisign Payflow Pro Functions, vpopmail
Functions, W32api Functions, WDDX Functions, win32ps Functions, win32service Functions, xattr Functions, xdiff Functions, XML Parser
Functions, XML-RPC Functions, XMLReader functions, xmlwriter Functions, XSL functions, XSLT Functions, YAZ Functions, YP/NIS Functions,
Zip File Functions, Zlib Compression Functions,
PHP/Oracle: Building for Scalability
• PHP
9Oracle Database
• Using Oracle with PHP
• PHP/Oracle: Building for
Performance and Scalability
• What’s New
Oracle
Oracle Database
Manage All Types of Data Rich Features Set
•
•
•
•
•
•
•
Relational
Object-Relational
Text
XML
Multi-media
Spatial
“Content”
Robust Enterprise
Services
•
•
•
•
Data Integrity
Concurrency
Transactions
Security
•
•
•
•
•
•
•
•
•
•
•
SQL, PL/SQL
RAC/HA
XML Database
Rules Engine
Expression Filters
Analytic Functions
Regular Expressions
Virtual Private Database
Partitioning
LOBs and BFILEs
Streams/AQ
Scalability
• Session Architecture
PHP/Oracle
PHP
Oracle
• The most popular Web
Scripting Language
• The most popular Enterprise
RDBMS
PHP and Oracle Together
• Agile and Scalable Web Applications
• Complete, Rich, Robust
Enterprise Services, and
Scalable Data Server
PHP/Oracle: Building for Scalability
• PHP
• Oracle
9Using Oracle with PHP
• Oracle Commitments to PHP
• How They Stack Up
• OCI8 Extensions
• PHP/Oracle: Building for Performance and Scalability
• What’s New
Oracle Commitments to PHP
• Long time commitment to PHP
• Thousands of developers use Oracle and PHP
• Oracle in various PHP communities and expert groups
• Participate to PHP Events and Conferences
• Submit Bug Fixes to the Community
• Zend Core for Oracle: Joint effort with Zend
• Prebuilt stack (Apache, PHP, Oracle OCI-based driver)
• Resulted in improved/refactored OCI8 extension
• Released October 2005, Enterprise support from Zend
PHP/Oracle: Three Tier Web Model
Apache
PHP
OCI8 Extension
Oracle Client
Libraries 8i, 9i or 10g
Web User
Mid Tier
Oracle Database
8i, 9i or 10g
OCI8 Extension
• Example
Easy Connect
[//]host[:port][/service_name]
<?php
$c = oci_connect('hr', 'hrpw', 'localhost/XE');
$s = oci_parse($c, 'select city from locations');
oci_execute($s);
while ($r = oci_fetch_assoc($s))
echo $r['CITY'] . "<br>";
?>
OCI8 Extension Continued
• php.ini parameters
• Persistent connection management
• Row prefetching
• Client side statement cache
• Distribution
• php.net
• Source code, Windows Binaries
• Includes oci8 extension
• PECL - PHP Extension Community Library
• Useful for updating PHP4 with new oci8
• Zend Core for Oracle
• Linux, Solaris, Windows, AIX
OCI8 Extension Continued
• Globalization support is built-in, inherited from OCI
• Mostly transparent to PHP applications
• NLS_LANG defines the Oracle locale
• Multibytes character sets require PHP mbstring or iconv
libraries
• User ISO locale is retrieved from HTTP Accept Language
header
• PHP applications that call PL/SQL should map ISO locale to
NLS_LANGUAGE and NLS_TERRITORY
• See “The Underground PHP and Oracle Manual” for
more details.
PHP/Oracle: Building for Scalability
• PHP
• Oracle
• Using Oracle with PHP
9PHP/Oracle: Building for Performance and Scalability
• Connection Management
• Statement Management
• Transaction Management
• What’s New
<Insert Picture Here>
Connection Management
OCI8 Connections
• Non-Persistent Connections
• Standard Connection
• Multiple Unique Connections
• Persistent Connections
Non Persistent Connections
Standard Connection
$c = oci_connect($username, $password, $dbname);
• Connection bound to the life of the script
• Second oci_connect() in script returns same DB connection
Multiple Unique Connections
$c = oci_new_connect($username, $password,$dbname);
• Connection bound to the life of the script
• Each oci_new_connect() returns a new DB connection
• Use for independent operations
Persistent Connections
$c = oci_pconnect($username, $password, $dbname);
• Not automatically closed at script completion
• Fast for subsequent connections
• But holds resources when application idle
• Configurable in php.ini
oci8.max_persistent
oci8.persistent_timeout
oci8.ping_interval
Persistent Connections are Cached
oci_pconnect()
user:db:charset:privilege
hr:XE:ALU32UTF8:normal
system:XE:ALU32UTF8:sysdba
Connection Cache
<Insert Picture Here>
Statement Management
SQL Statement Execution
The Steps for Executing a SQL Statement with OCI8:
• Parse - oci_parse
• Prepares a statement for execution
• Bind - oci_bind_by_name
• Optionally binds variables in WHERE clause
• Execute – oci_execute
• The Database executes the statement and buffers the results
• Fetch - oci_fetch_all
• Optionally retrieves results from the database
Round Trips Between PHP and Oracle
Round Trip
Tuning Goal: minimize round trips
Row Prefetching
Set oci8.default_prefetch
PHP
OCI8 Extension
Beijing
Database
Oracle Client
Libraries
Beijing
Bern
Bombay
...
Reduces round trips
OCI8 Prefetch Rows
• “prefetched rows” are cached internally by Oracle
• Improves query performance by reducing “round trips”
• Default Prefetch Size in php.ini
oci8.default_prefetch = 10
• Maximum number of rows in each DB "round trip"
• Memory limit of 1024 * oci8.default_prefetch is
also set
• Value can also be set inline
oci_set_prefetch($s, 100);
Statement Caching: No Bind Variables
select col
from tab
where v = 1
select col
from tab
where v = 1
select col
from tab
where v = 2
select col
from tab
where v = 2
Poor use of cache
1: select col from
tab where v = 1
2: select col from
tab where v = 2
Database cache
Statement Caching: Bind Variables
Automatic Database feature
select col
from tab
where v = :bv
select col
from tab
where v = :bv
select col
from tab
where v = :bv
select col
from tab
where v = :bv
Increases
performance
and security
1: select col from
tab where v = :bv
Database cache
OCI8 Bind Example
$s = oci_parse($c,"select last_name from employees
where employee_id = :eidbv");
$myeid = 101;
oci_bind_by_name($s, ":EIDBV", $myeid);
oci_execute($s);
oci_fetch_all($s, $res);
echo "Last name is:"
.$res['LAST_NAME'][0]."<br>\n";
$myeid = 102;
oci_execute($s);
// No need to re-parse
oci_fetch_all($s, $res);
echo "Last name is: "
. $res['LAST_NAME'][0] ."<br>\n";
Statement Caching: Client
Set oci8.statement_cache_size
select col
from tab
select col
from tab
“Use statement 1”
1: select col from tab
1: select col from tab
Client cache
Database cache
select col
from tab
OCI8 Statement Cache
• OCI8 extension has client side cache
• php.ini
oci8.statement_cache_size = 20
• Unit is number of statements
• Moves cache management load from DB to PHP
side
• Uses memory on PHP side for cached statement
handles
• Uses memory on DB for per session cursors
<Insert Picture Here>
Transaction Management
Auto Commit
By default oci_execute() auto-commits
function do_simple_insert($conn, $array)
{
foreach ($array as $v) {
$s = oci_parse($conn,
"insert into ptab (pdata) values ('".$v."')");
$r = oci_execute($s);
}
}
This is slow
Explicit Commit
function do_transactional_insert($conn, $array)
{
$s = oci_parse($conn,
'insert into ptab (pdata) values (:bv)');
oci_bind_by_name($s, ':bv', $v, 20, SQLT_CHR);
foreach ($array as $v)
$r = oci_execute($s, OCI_DEFAULT);
oci_commit($con);
}
This is fast
Bulk Inserts (PHP Code)
function do_bulk_insert($conn, $array)
{
$s = oci_parse($conn,
'begin mypkg.myproc(:c1); end;');
oci_bind_array_by_name($s, ":c1", $array,
count($array), -1, SQLT_CHR);
oci_execute($s);
}
Could be fastest
Bulk Inserts (PL/SQL Side)
create or replace package mypkg as
type arrtype is table of varchar2(20)
index by pls_integer;
procedure myproc(p1 in arrtype);
end mypkg;
create or replace package body mypkg as
procedure myproc(p1 in arrtype) is
begin
forall i in indices of p1
insert into ptab values (p1(i));
end myproc;
end mypkg;
PHP/Oracle: Building for Scalability
• PHP
• Oracle
• Using Oracle with PHP
• PHP/Oracle: Building for Performance and Scalability
9What’s New
• OCI8 Enhancements in Oracle Database 11g
• Database Resident Connection Pool
• Client Query Result Cache
OCI8 Enhancements in Oracle
Database 11g
• Grid support
• Open source community release
• Database Resident Connection Pool
• Dramatic Improvement of PHP application scalability and
performance
• Seamless Uptake by existing applications simply by setting
php.ini parameters
• Client Result Cache
<Insert Picture Here>
Database Resident
Connection Pool
PHP Users have asked for Database
Connection Pooling
• Web apps issue hundreds connects per second
• Connection establishment is expensive
• PHP Does not allow real multi-threading
• Persistent vs Non Persistent Connections
• Existing Client-side connection pooling not possible
• Options:
• Apache process can repeatedly connect/disconnect
• Apache process can hold onto private connection
• Results in inefficient use of the database resources
Oracle Database 11g DRCP
No Connection Pooling
11g Database Resident Connection Pooling
Database Resident Connection Pool
• 11g Database Feature
• Allows multiple Oracle clients to share a Database
Resident pool of Sessions
• USERIDs must match
• Ability to control sharing of connections on a per application
basis
• Session Pool shared across middle tier instances
• Works in RAC
• Highly scalable, supporting tens of thousands of
database connections
• Requires no Application Code change
DRCP Functionality
• Pooling is optionally enabled by DBA on server
• Min, Max, Timeout etc. for Pool
• Client directed to Database Resident Connection Pool
• Client connect string:
• hostname/service:POOLED
• (SERVER=POOLED)
DRCP: Sharing Boundary
• Only clients logged in as ‘scott’ can share other ‘scott’
connections present in the pool
• CONNECTION_CLASS
• Allows for further partitioning of a user connections
• Clients with different connection classes do not share
• Only clients with same “user.connection_class” share
connections
• Useful for separating different applications with same
username
DRCP: PHP View
• Download the new oci8 extension for 11g with Pooling
capability
• Set “:POOLED” in connect string
• Set oci8.connection_class = “<your_string>"
• use the same setting on all apache servers that can share
connections.
• No Application Code Changes
• Extremely simple to turn on
Best Practices
• For Application Developers
• Do not hold on to a pooled connection when not necessary
• Do not wait for script end to implicitly close connection
• Try not to interleave other work (e.g static content) with
database calls, when holding connection
• For Administrators
• Identify all apache instances serving an application
• Modify php.ini connection_class setting appropriately
<Insert Picture Here>
Client Query Result Cache
Client Query Result Cache
• Cache enabled during deployment
• No changes to OCI applications, OCI drivers
• Optional SQL hints
• In Process for 11g
• Fast query cache access
• Shared across multiple threads/connection pool
• Match on client => no server execute or fetch
• Invalidations
• Database changes (DDL, DML, Security)
• Session state change
• RAC consistency
• Drivers built on OCI get query caching:
• PHP, ODP.Net, JDBC-OCI, OCCI, ODBC, Precomp etc
• Custom abstraction layers over OCI
Summary
• Oracle Database makes PHP applications more
scalable and robust
• Oracle Database 11g brings new scalability
mechanisms
• PHP applications can use these new functionalities
through the new oci8 extension for 11g
The preceding is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle’s
products remain at the sole discretion of Oracle.
Oracle Technology Network
PHP Developer Center
• Articles
• Install guides
• Underground PHP
and Oracle Manual
• Online forum
• Oracle JDeveloper
10g PHP extension
http://oracle.com/technology/php
Oracle Resources
• OTN PHP Developer Center
• Underground PHP and Oracle Manual
• Articles, FAQs, links to blogs, JDeveloper PHP Extension, Zend
Core for Oracle
• www.oracle.com/technology/php
• Information
• [email protected]
• blogs.oracle.com/opal
• ISVs and hardware vendors
oraclepartnernetwork.oracle.com