php oracle web applications by kuassi mensah
DESCRIPTION
PHP Oracle Web Applications:Best Practices and Caching Strategiesby Kuassi Mensah, Server Technologies, OracleTalk presented at PHP Barcelona Conference 2009.TRANSCRIPT
© 2009 Oracle Corporation
<Insert Picture Here>
PHP Oracle Web Applications: Best Practices and Caching StrategiesKuassi Mensah Server Technologies OracleKuassi Mensah, Server Technologies, Oracle
Agenda
• ► PHP, the OCI8 Extension and Oracle Database• Scaling Database Connectivity• Scaling Database Connectivity
• Database Resident Connection Pool – Solving the C20K Problem
• Logon Storm Shield• Logon Storm Shield• Scaling Database Operations
• Bind Variables, Statement Caching, Row PrefetchingScaling Very Complex Queries• Scaling Very Complex Queries
• Scaling with Stored Procedures• Caching Strategies
• Resultset Caching• Continuous Query Notification and Mid-tier Cache
Invalidation I M D t b C h
© 2009 Oracle Corporation
• In Memory Database Cache
PHP and the OCI8 Extension
© 2009 Oracle Corporation
Three Tier Web Model
© 2009 Oracle Corporation
What is OCI8?
• Main Oracle Database extension for PHP• Open source and part of PHP
<?php
Open source and part of PHP• Current version is OCI8 1.3 – in PHP 5.3 and in PECL<?php$c = oci_connect('un', 'pw', 'localhost/orcl');$s = oci_parse($c, 'select * from employees');oci_execute($s);while (($row = oci_fetch_array($s, OCI_ASSOC)) != false) foreach ($row as $item)( )
print $item;?>
© 2009 Oracle Corporation
Get the Latest OCI8 1.3
• php.net• PHP 5 3 Source code Windows binariesPHP 5.3 Source code, Windows binaries
• PECL - PHP Extension Community Library• For updating PHP 4 - 5.2 with OCI8 1.3
htt // l / j t / h• http://oss.oracle.com/projects/php• RPMs for Linux with OCI8
• Unbreakable Linux Network• Oracle's Linux Support program • OCI8 RPM available for PHP
• Zend Servere d Se e• Linux, Windows, Mac• Support from Zend
© 2009 Oracle Corporation
Oracle Database 11.1 and PHP
• Oracle 11.1 was released August 2007• Connection Pooling - DRCP• Server (database) Query Result Cache
Client Query Result Cache• Client Query Result Cache • Query Annotation
• Continuous Query Notification • Cube Organized Materialized Views• Total Recall•• . . .
© 2009 Oracle Corporation
Oracle Database 11.2 and PHP
• Oracle 11.2 was released September 2009• Many new features building on 11gR1 innovations
• Improved views for DRCP connection pooling• Continuous Query Notification for ViewsContinuous Query Notification for Views• Client Query Result Cache
• Table Annotation• View AnnotationView Annotation
• Client query result cache (CQRC) supported with DRCP• Pre-fetching supported in more places
• Edition Based Redefinition• Edition Based Redefinition• RAC One Node option• . . .
© 2009 Oracle Corporation
Agenda
• PHP, the OCI8 Extension and Oracle Database• ►Scaling Database Connectivity• ►Scaling Database Connectivity
• Database Resident Connection Pool – Solving the C20K Problem
• Logon Storm Shield• Logon Storm Shield• Scaling Strategies
• Bind Variables, Statement Caching, Row PrefetchingScaling Very Complex Queries• Scaling Very Complex Queries
• Scaling with Stored Procedures• Caching Strategies
• Resultset Caching• Continuous Query Notification and Mid-tier Cache
Invalidation I M D t b C h
© 2009 Oracle Corporation
• In Memory Database Cache
Database Resident Connection Pool - SolvingConnection Pool Solving the C20K problem
© 2009 Oracle Corporation
Standard OCI8 connections
$c = oci_connect($un, $pw, $db);
• High CPU Overhead • Connection establishment slow• Frequent connect and disconnect
• Does not scale in web environments• One database server process per PHP userOne database server process per PHP user• Maxes out database server memory
• Cannot handle the Digg effect
© 2009 Oracle Corporation
Persistent OCI8 Connections
$c = oci_pconnect($un, $pw, $db);
• Not closable (prior to OCI8 1.3) • Fast for subsequent connections • Some control configurable in php.ini
oci8.max_persistentoci8.persistent_timeoutoci8.ping_interval
© 2009 Oracle Corporation
Database Resident Connection Pool Solving the C20K ProblemSolving the C20K Problem
Connection ( DB handle)
Dedicated serversConnection
Broker( DB handle)
Session(User Handle)
SessionSessionS i
Broker
Oracle Net
(User Handle)(User Handle)Session
(User Handle)Session
(User Handle)Session
(User Handle)
1
• Pool of dedicated servers• (1) Server allocated/locked on Appl Connect
2
• (1) Server allocated/locked on Appl. Connect • (2) Direct server access after handoff• (3) Server released on Appl. ConnClose .
N i th iddl l l t
© 2009 Oracle Corporation
• No man-in-the-middle, low latency
DRCP: System Overview
© 2009 Oracle Corporation
PHP DRCP Benchmark
• See PHP DRCP See CWhitepaper
• 2GB RAM• 1 connection broker
100 pooled servers• 100 pooled servers
© 2009 Oracle Corporation
Sample Sizing for 5000 UsersDRCP Servers(Pool of 100)
Dedicated Servers
Session
100 * 4 MB5000 * 4 MBDatabaseServers
5000 * 35 KBDRCP
100 * 400 KB5000 * 400 KBSessionMemory
5000 * 35 KBConnection BrokerOverhead
610 MB21 GBTotal Memory
© 2009 Oracle Corporation
DRCP in PHP
• DRCP support is available in OCI8 1.3• Developer decision to use it or not• DRCP functionality available when linked with Oracle 11g
client libraries and connected to Oracle 11g• OCI8 1.3 is included in PHP 5.3 and PHP 6
• For PHP 4.3.9 – 5.2 use PECL OCI8 1.3
© 2009 Oracle Corporation
Configuring and Starting the POOL • Install PHP OCI8 1.3 and Oracle 11g• Configure the pool (optional)
SQL> execute dbms connection pool.configure pool(SQL execute dbms_connection_pool.configure_pool(pool_name => 'SYS_DEFAULT_CONNECTION_POOL',minsize => 4,maxsize => 40,incrsize => 2,session_cached_cursors => 20,inactivity_timeout => 300,max think time => 600max_think_time => 600,max_use_session => 500000,max_lifetime_session => 86400);
• Sart the pool:Sart the pool:SQL> execute dbms_connection_pool.start_pool();
• Set oci8.connection_class in php.inioci8 connection class = MY PHP APP
© 2009 Oracle Corporation
oci8.connection_class MY_PHP_APP
Using the Pool
1/ Changing the Connect String in PHP Applications
<?php$c = oci_pconnect("phpweb", "phpweb", "//localhost/orcl:pooled");
$s = oci_parse($c, 'select * from employees');oci_execute($s);oci_fetch_all($s, $res);var dump($res);var_dump($res);?>
2/ Zero Code Change2/ Zero Code ChangeChange the TNS alias in tnsnames.ora configuration file
© 2009 Oracle Corporation
Some DRCP recommendations.
• Read the PHP DRCP whitepaperhttp://www.oracle.com/technology/tech/php/index.html
• Make sure oci8.connection_class is set• Have > 1 Broker, but only a few• Close connections when doing non-DB processing• Explicitly control commits and rollbacks
• Avoid unexpectedly open transactions when an oci_close() or end-of scope occursof-scope occurs
• Scripts coded like this can use oci_close() to take advantage of DRCP but still be portable to older versions of the OCI8 extension
• Monitor V$CPOOL STATS view to determine best pool sizeMonitor V$CPOOL_STATS view to determine best pool size
© 2009 Oracle Corporation
Logon Storm Shield
© 2009 Oracle Corporation
Logon Storm Shield
• Logon Storm• On Mid-tier Reboot• DoS Attack
• Logon Storm Shield: Connection Rate Limiter g• configured in LISTENER.ORA
LISTENER=(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=sales)(PORT=1521)(RATE_LIMIT=3))
(ADDRESS=(PROTOCOL=tcp)(HOST=lmgmt)(PORT=1522)(RATE_LIMIT=no)))
• Set the Rate Limit to a value that matches your machine biliticapabilities.
© 2009 Oracle Corporation
Agenda
• PHP, the OCI8 Extension and Oracle Database• Scaling Database Connectivity
• Database Resident Connection Pool – Solving the C20K Problem
• Logon Storm Shield• ►Scaling Database Operations
• Bind Variables, Statement Caching, Row Prefetchingg g• Scaling Very Complex Queries• Scaling with Stored Procedures
• Caching StrategiesCaching Strategies• Resultset Caching• Continuous Query Notification and Mid-tier Cache
Invalidation
© 2009 Oracle Corporation
• In Memory Database Cache
Bind Variables
© 2009 Oracle Corporation
Not Binding Literals in SQL statements disable sharing
Poor use of cache
© 2009 Oracle Corporation
Binding Scalars
© 2009 Oracle Corporation
Binding Scalars
<?php$c = oci_connect('hr', 'hrpwd', 'localhost/orcl');$s = oci_parse($c,'insert into employees values (:bv)');$name = ‘Mensah';oci bind by name($s ':bv' $name);oci_bind_by_name($s, :bv , $name);oci_execute($s);
?>
• Associates PHP variable $name with placeholder :bv• Similar to JDBC PreparedStmt• No SQL Injection worry• Easier to write than adding quote escaping
© 2009 Oracle Corporation
DB Statistics Without and With BindingFrom a query example by Tom Kyte:
Not Binding BindingParse time elapsed 485 36Parse time elapsed 485 36Parse count (hard) 5,000 1Latches 328,496 118,614
• Overall system is more efficient• PHP user elapsed time directly benefits
© 2009 Oracle Corporation
Binding Best Practices
• Set length parameter to your upper data size for re-executed IN binds oci_bind_by_name($s, “:b”, $b, 40);_ _ y_ ( )
• Don't bind constants• Let the optimizer see them
• Long running unique queries may not benefit• Long running unique queries may not benefit• Parse time is a relatively small cost
• CURSOR_SHARING parameterS “• Set in “session” or database init.ora
• Makes every statement appear to have bound data, but optimizer now doesn't see constantsFor bind unfriendly applications• For bind-unfriendly applications
• Oracle 11g has Adaptive Cursor Sharing• Can have multiple execution plans for same statement
© 2009 Oracle Corporation
Statement Caching
© 2009 Oracle Corporation
Client (aka PHP) Statement Caching
Less traffic and DB CPU
© 2009 Oracle Corporation
OCI8 Uses the Statement Cache.
• No PHP code change needed• Oracle Client library cache of statement text & meta data
• On by default in php.inioci8 statement cache size = 20oci8.statement_cache_size = 20
Unit is number of statements• Set it big enough for working set of statements
ff• Reduces network traffic and context switches• 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
© 2009 Oracle Corporation
Row Prefetching
© 2009 Oracle Corporation
Prefetching Reduces Roundtrips• Temporary buffer cache for query duration
No DB access for next fetch
© 2009 Oracle Corporation
Reduces round trips
Prefetching Improves Query Times
Your results will vary
© 2009 Oracle Corporation
Prefetching is Enabled by Default
• Enabled by defaultCan tune per statement:
$s = oci_parse($c, 'select city from locations');i t f t h($ 200)
• Can tune per statement:
oci_set_prefetch($s, 200);oci_execute($s);while (($row = oci_fetch_array($s, OCI_ASSOC)) != false) foreach ($row as $item)
print $item;
Each database “round trip” prefetches 200 rows
© 2009 Oracle Corporation
PHP OCI8 Row Prefetching.
• No need to change PHP application• Rows are cached internally by Oracle client libraries
• php.ini oci8.default_prefetch = 100 rows• Was 10 rows in OCI8 1.2Was 10 rows in OCI8 1.2
• Oracle 11.2 supports REF CURSOR prefetching too• Tuning goal:
R d d t i• Reduce round trips• For big data sets: transfer reasonable chunks, not huge sets
© 2009 Oracle Corporation
Scaling Very Complex Queries
© 2009 Oracle Corporation
Scaling Very Complex SQL Queries
Problem to Solve: Query Sales and Quantity by Year,Department, Class and Country
The SQL Query:SELECT SUM(s.quantity) AS quantity, SUM(s.sales) AS sales,
t calendar year name p department name c class namet.calendar_year_name, p.department_name, c.class_name,cu.country_name
FROM times t, products p, channels c, customers cu, sales_fact s WHERE p.item_key = s.product AND s.day_key = t.day_key AND
s.channel = c.channel_key AND s.customer = cu.customer_key
GROUP BY p.department_name, t.calendar_year_name, c.class_name, cu country name;cu.country_name;
© 2009 Oracle Corporation
Cube Organized Materialized ViewsTransparent to SQL QueriesTransparent to SQL Queries
Materialized Views
R i D t
SQL Query
Region Date
QueryRewrite
Product ChannelAutomatic OLAP Cube
© 2009 Oracle Corporation
AutomaticRefresh
OLAP Cube
Scaling with Stored Procedures
© 2009 Oracle Corporation
Scaling with Stored ProceduresJava or PL/SQLJava or PL/SQL
Client
Any Language
ClientClient
Any Language
Multiple Unique SQL Calls
Stored Procedure Call
Java or PL/SQL
Calls
SQLSQL
© 2009 Oracle Corporation
Up to 10 x Faster!
Agenda
• PHP, the OCI8 Extension and Oracle DatabaseS li D t b C ti it• Scaling Database Connectivity
• Database Resident Connection Pool – Solving the C20K ProblemLogon Storm Shield• Logon Storm Shield
• Scaling Database OPerations• Bind Variables, Statement Caching, Row Prefetching• Scaling Very Complex Queries• Scaling with Stored Procedures
• ►Caching Strategies• Resultset Caching• Continuous Query Notification and Mid-Tier Cache
Invalidation
© 2009 Oracle Corporation
• In Memory Database Cache
Resultset Caching
© 2009 Oracle Corporation
Oracle 11g Client & Server Result Caches
• Results of queries can be cached• Server and client (aka PHP) have cachesServer and client (aka PHP) have caches• Recommended for small lookup tables• Client cache is per-process
• Caches automatically invalidated by server data changes
• Feature can be configured globally or per client• DB parameter: CLIENT_RESULT_CACHE_SIZE
P li t i l t OCI RESULT CACHE MAX SIZEPer-client in sqlnet.ora: OCI_RESULT_CACHE_MAX_SIZE• Has a configurable 'lag' time
• If no roundtrip within defined time, cache assumed stale
© 2009 Oracle Corporation
Oracle 11g Client & Server Result Caches
• With Oracle 11.2 Table or View Annotation, developer or DBA choose tables or view to be cached:alter table last_name result_cache create view v2 as select /*+ result cache */ col1, coln from t1
No need to change PHP application
• With Oracle 11 1 Query Annotation need to add hint• With Oracle 11.1 Query Annotation, need to add hint to queries instead:
select /*+ result_cache */ last_name from employees
• V$RESULT_CACHE_* views show cache usage
© 2009 Oracle Corporation
No DB Access When Client Cache Used
SQL> select parse_calls, executions, sql_text from v$sql where sql_text like '%cjcrc%';
PARSE CALLS EXECUTIONS SQL TEXTPARSE_CALLS EXECUTIONS SQL_TEXT----------- ---------- -----------------------------------
2 100 select * from cjcrc2 2 select /*+ result_cache */ * from cjcrc
© 2009 Oracle Corporation
Result Caching Test.$c = oci_pconnect('hr', 'hrpwd', 'localhost/orcl');$tbls = array('locations', 'departments', 'countries');foreach ($tbls as $t) {
$s oci parse($c "select /*+ res lt cache */ * from $t")$s = oci_parse($c, "select /*+ result_cache */ * from $t");oci_execute($s, OCI_DEFAULT);while ($row = oci_fetch_array($s, OCI_ASSOC)) {
foreach ($row as $item) {echo $item "\n";}}}foreach ($row as $item) {echo $item. \n ;}}}
$ siege -c 20 -t 30S http://localhost/clientcache.php
Without result cache: select * from $tTransaction rate: 32.02 trans/sec
With result cache cache: select /*+ result cache */ * from $tWith result cache cache: select /*+ result_cache */ * from $tTransaction rate: 36.79 trans/sec
Result Caching was approx. 15% better
© 2009 Oracle Corporation
Result Caching was approx. 15% better
Continuous Query Notification and Mid Tier Cache Invalidationand Mid-Tier Cache Invalidation
© 2009 Oracle Corporation
Continuous Query Notification
Problem to solve:Be notified when changes in the database invalidates
<?php
Be notified when changes in the database invalidates an existing query result set
2.Upon Change (DMLImpacting<?php
…4.Invalidate cache5.repopulate cache
( p gthe result set)
Callout
p p…?>
1. Registerthe query
Custom cache3.Automatic
Notification (Java or PL/SQL database jobas noificaion callback)
the query
© 2009 Oracle Corporation
Example - The Cache Depends On This Table
$ sqlplus cj/cj
create table cjtesttab (
group_id number,
name varchar2(20)name varchar2(20)
);
insert into cjtesttab values (1, 'alison');
insert into cjtesttab values (2, 'pearly');
insert into cjtesttab values (2, 'wenji');
© 2009 Oracle Corporation
Example - The PHP Cache-Updating Code
<?php
// cache.php
$g = date('Y-m-d H:i:s').": Table was: ".$_GET[tabname];
file put contents('/tmp/cqn txt' $g);file_put_contents( /tmp/cqn.txt , $g);
// In reality query the table and update the cache:
// $s = oci_parse($c, "select * from ".$_GET[tabname]);
// . . .
$// $memcache->set('key', ...);
?>
© 2009 Oracle Corporation
Example - Create 'mycallback' PL/SQL Procedure
create or replace procedure mycallback (
ntfnds in cq_notification$_descriptor) is
req utl_http.req;
resp utl_http.resp;
begin
f ( f f )if (ntfnds.event_type = dbms_cq_notification.event_querychange) then
req := utl_http.begin_request(
'http://mycomp.us.oracle.com/~cjones/cache.php&tabname='
|| tf d d (1) t bl d (1) t bl )|| ntfnds.query_desc_array(1).table_desc_array(1).table_name);
resp := utl_http.get_response(req);
utl_http.end_response(resp);
end if;end if;
end;
/
© 2009 Oracle Corporation
Example - Register 'mycallback' for a Queryy
declare
reginfo cq_notification$_reg_info;
v cursor sys refcursor;_ y _ ;
regid number;
begin
reginfo := cq_notification$_reg_info (
'mycallback', -- callback function
dbms_cq_notification.qos_query, -- result-set notification flag
0, 0, 0);
regid := dbms_cq_notification.new_reg_start(reginfo);
open v_cursor for select name from cjtesttab where group_id = 2;
close v_cursor;
dbms_cq_notification.reg_end;
end;
/
© 2009 Oracle Corporation
Example Recap
• Table cjtesttab • PHP script http://.../cache.php to update the cachePHP script http://.../cache.php to update the cache• PL/SQL callback procedure mycallback()• Registered query
l t f jt tt b h id 2select name from cjtesttab where group_id = 2;• Aim: refresh mid-tier cache when this query results change
© 2009 Oracle Corporation
Example - In Action
• Let's update the table:update cjtesttab set name = 'c' where group_id = 2;p j g p_ ;commit;
• Output in /tmp/cqn.txt is:2009 09 23 13:11:39: Table was: CJ CJTESTTAB2009-09-23 13:11:39: Table was: CJ.CJTESTTAB
• Update a different group_id:update cjtesttab set name = 'x' where group_id = 1;commit;
• No change notification is generated
© 2009 Oracle Corporation
The following is intended to outline our generalThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any
t t It i t it t t d licontract. 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.
© 2009 Oracle Corporation
Challenges with Current Caching MechanismsMechanisms
• Cache Currency• Content can get stale over time – invalidate or refresh cache
R d O l U d bl C h• Read-Only vs. Updatable Cache• Updates require synchronization with Oracle database
• Query Capabilityy p y• Persistence in the Application Tier• Availability
• Should access to data be available if back end database is not• Should access to data be available if back-end database is not available?
• Significant Development effort
© 2009 Oracle Corporation
Oracle Database and PHP Roadmap
• PHP OCI8 integration with g• TimesTen In Memory Database
• A fast in memory, persistent DB• TimesTen In Memory Database CacheTimesTen In Memory Database Cache
• Cache for Oracle Database• No need for separate caching logic
© 2009 Oracle Corporation
Extra Slides and Free Stuff
© 2009 Oracle Corporation
DBMS_XA: Transactions Across Requests
• Oracle 11gR1 Feature• Can we use it on the web? Upgrading thick client applications?
E l f htt //ti l /db• Example from http://tinyurl.com/dbmsxaexHTTP Request #1:rc := DBMS_XA.XA_START(DBMS_XA_XID(123), DBMS_XA.TMNOFLAGS);UPDATE l SET l l *1 1 WHERE l id 100UPDATE employees SET salary=salary*1.1 WHERE employee_id = 100;rc := DBMS_XA.XA_END(DBMS_XA_XID(123), DBMS_XA.TMSUSPEND);
HTTP Request #2:HTTP Request #2:rc := DBMS_XA.XA_START(DBMS_XA_XID(123), DBMS_XA.TMRESUME);SELECT salary INTO s FROM employees WHERE employee_id = 100;rc := DBMS_XA.XA_END(DBMS_XA_XID(123), DBMS_XA.TMSUCCESS);_ _ ( _ _ ( ) _ )
HTTP Request #3:rc := DBMS_XA.XA_COMMIT(DBMS_XA_XID(123), TRUE);
© 2009 Oracle Corporation
Hey Look! Free Stuff
• Oracle Instant Client• Easy to install• Client libraries for C, C++, Java, .Net access to a remote DB
• Oracle Database Express Edition (aka “XE”)Oracle Database Express Edition (aka XE )• Same code base as full database• Windows & Linux 32 bit
• SQL Developer• SQL Developer• Thick client SQL and PL/SQL development tool• Connects to MySQL too
Application Express (“Apex”)• Application Express ( Apex )• Web based Application Development tool• Try it at http://apex.oracle.com/
© 2009 Oracle Corporation
Some PHP & Oracle Books
© 2009 Oracle Corporation
Oracle Resources
• Free Oracle Techology Network (OTN) PHP Developer Center: otn.oracle.com/phpPHP Developer Center: otn.oracle.com/php
• Free book: Underground PHP and Oracle Manual• Whitepapers, Articles, FAQs, links to blogs, JDeveloper
PHP Extension, PHP RPMsPHP Extension, PHP RPMs• Informationkuassi.mensah@oracle.comdb360.blogspot.comdb360.blogspot.comchristopher.jones@oracle.comblogs.oracle.com/opal• SQL and PL/SQL Questions• SQL and PL/SQL Questionsasktom.oracle.com• ISVs and hardware vendors
oraclepartnernetwork oracle com
© 2009 Oracle Corporation
oraclepartnernetwork.oracle.com
© 2009 Oracle Corporation