© 2009 Oracle Corporation
<Insert Picture Here>




PHP Oracle Web Applications:
Best Practices and Caching Strategies
Kuassi Mensah Server Technologies Oracle
       Mensah,       Technologies,
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 Prefetching
           • Scaling Very Complex Queries
           • Scaling with Stored Procedures
        • Caching Strategies
           • Resultset Caching
           • Continuous Query Notification and Mid-tier Cache
             Invalidation
           • I Memory Database Cache
             In M        D t b    C h

© 2009 Oracle Corporation
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
        • 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 binaries
                  5.3        code,
        • PECL - PHP Extension Community Library
           • For updating PHP 4 - 5.2 with OCI8 1.3
        • htt //
          http://oss.oracle.com/projects/php
                           l      / j t / h
           • RPMs for Linux with OCI8
        • Unbreakable Linux Network
           • Oracle's Linux Support program
           • OCI8 RPM available for PHP
        • Zend Se e
            e d Server
           • 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
           • 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 Views
           • Client Query Result Cache
              • Table Annotation
              • View Annotation
           • Client query result cache (CQRC) supported with DRCP
           • Pre-fetching supported in more places
        • Edition Based Redefinition
        • RAC One Node option
        • ...


© 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 Strategies
           • 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
           • I Memory Database Cache
             In M        D t b    C h

© 2009 Oracle Corporation
Database Resident
            Connection 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 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_persistent
               oci8.persistent_timeout
               oci8.ping_interval




© 2009 Oracle Corporation
Database Resident Connection Pool
            Solving the C20K Problem
                                               Dedicated servers
        Connection                Connection
        ( DB handle)                Broker

                                                   Session
                                                    Session
                                                      Session
                                                (User Handle)
                                           1           Session
                                                       S i
                                                 (User Handle)
                                                  (User Session
                                                         Handle)
                                                    (User Handle)
                            Oracle Net               (User Handle)

                                  2

           • Pool of dedicated servers
           • (1) Server allocated/locked on Appl Connect
                                             Appl.
           • (2) Direct server access after handoff
           • (3) Server released on Appl. ConnClose .
           • N man-in-the-middle, l
             No       i th     iddl low l t
                                         latency

© 2009 Oracle Corporation
DRCP: System Overview




© 2009 Oracle Corporation
PHP DRCP Benchmark




                                • See PHP DRCPC
                                  Whitepaper
                                • 2GB RAM
                                • 1 connection broker
                                • 100 pooled servers




© 2009 Oracle Corporation
Sample Sizing for 5000 Users
                                     Dedicated      DRCP Servers
                                      Servers       (Pool of 100)

                     Database
                     Servers        5000 * 4 MB      100 * 4 MB

                     Session
                     Memory         5000 * 400 KB    100 * 400 KB

                     DRCP
                     Connection                      5000 * 35 KB
                     Broker
                     Overhead

                     Total Memory     21 GB           610 MB




© 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
            SQL> execute dbms connection pool.configure pool(
                           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
                max think time          => 600
                                           600,
                max_use_session             => 500000,
                max_lifetime_session => 86400);
        • Sart the pool:
              SQL> execute dbms_connection_pool.start_pool();
        • Set oci8.connection_class in php.ini
              oci8.connection_class MY_PHP_APP
              oci8 connection class = MY PHP APP


© 2009 Oracle Corporation
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 Change
            Change the TNS alias in tnsnames.ora configuration file



© 2009 Oracle Corporation
Some DRCP recommendations.

        • Read the PHP DRCP whitepaper
               http://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
                of 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 size
                  V$CPOOL_STATS




© 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
              capabilities.
                   biliti




© 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 Prefetching
                                              g               g
           • Scaling Very Complex Queries
           • Scaling with Stored Procedures
        • Caching Strategies
           • Resultset Caching
           • Continuous Query Notification and Mid-tier Cache
             Invalidation
           • In Memory Database Cache
© 2009 Oracle Corporation
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
           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 Binding
        From a query example by Tom Kyte:
                                   Not Binding        Binding
        Parse time elapsed                485            36
        Parse count (hard)              5,000             1
        Latches                       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
           • Parse time is a relatively small cost
        • CURSOR_SHARING parameter
           • S in “
             Set “session” or database init.ora
           • Makes every statement appear to have bound data, but
             optimizer now doesn't see constants
           • For bind-unfriendly applications
                 bind unfriendly
        • 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.ini
               oci8.statement_cache_size
               oci8 statement cache size = 20
          Unit is number of statements
        • Set it big enough for working set of statements
        • Reduces network traffic and context switches
                                ff
           • 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

                            Reduces round trips
© 2009 Oracle Corporation
Prefetching Improves Query Times




                            Your results will vary


© 2009 Oracle Corporation
Prefetching is Enabled by Default

        • Enabled by default
        • Can tune per statement:

           $s = oci_parse($c, 'select city from locations');
           oci_set_prefetch($s, 200)
             i   t    f t h($ 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.2
        • Oracle 11.2 supports REF CURSOR prefetching too
        • Tuning goal:
           • R d
             Reduce round t i
                          d 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_name,
      t 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 Views
           Transparent to SQL Queries


                            SQL Query                      Materialized Views

    Region
    R i                                 Date
                                        D t


                                               Query
                                               Rewrite


   Product                          Channel
                                               Automatic      OLAP Cube
                                               Refresh
© 2009 Oracle Corporation
Scaling with Stored Procedures




© 2009 Oracle Corporation
Scaling with Stored Procedures
           Java or PL/SQL

                                                      Client
                  Client
                                                      Any Language
                  Any Language
                                                                 Stored Procedure Call

                                  Multiple Unique
                                  SQL Calls

                                                        Java or PL/SQL


                                                      Calls


                            SQL                                SQL


                                                    Up to 10 x Faster!

© 2009 Oracle Corporation
Agenda

        • PHP, the OCI8 Extension and Oracle Database
        • S li
          Scaling D t b
                    Database C Connectivity
                                        ti it
           • Database Resident Connection Pool – Solving the C20K
             Problem
           • 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
           • In Memory Database Cache

© 2009 Oracle Corporation
Resultset Caching




© 2009 Oracle Corporation
Oracle 11g Client & Server Result Caches

        • Results of queries can be cached
           • Server 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
          Per-client i sqlnet.ora: OCI RESULT CACHE MAX SIZE
          P    li t in l t         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
                      11.1       Annotation,
            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
PARSE CALLS EXECUTIONS SQL TEXT            SQL_TEXT
----------- ---------- -----------------------------------
        2        100 select * from cjcrc
        2          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")
        oci parse($c               result_cache          $t");
   oci_execute($s, OCI_DEFAULT);
   while ($row = oci_fetch_array($s, OCI_ASSOC)) {
     foreach ($row as $item) {echo $item "n";}}}
                                      $item. n ;}}}

$ siege -c 20 -t 30S        http://localhost/clientcache.php

Without result cache: select * from $t
Transaction rate: 32.02 trans/sec

With result cache cache: select /*+ result cache */ * from $t
                                    result_cache
Transaction rate: 36.79 trans/sec

Result Caching was approx. 15% better

© 2009 Oracle Corporation
Continuous Query Notification
            and Mid Tier Cache Invalidation
                Mid-Tier




© 2009 Oracle Corporation
Continuous Query Notification

         Problem to solve:
         Be notified when changes in the database invalidates
         an existing query result set
                                                                       2.Upon Change
                                                                       (       p
                                                                       (DMLImpacting   g
             <?php                                                     the result set)
             …
                                  Callout
             4.Invalidate cache
             5.repopulate cache
                 p p
             …
             ?>
                                                                       1. Register
                                            3.Automatic                the query
                  Custom cache                Notification
                                             (Java or PL/SQL database job
                                             as noificaion callback)



© 2009 Oracle Corporation
Example - The Cache Depends On This
             Table

        $ sqlplus cj/cj
        create table cjtesttab (
          group_id number,
          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
     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
     if (ntfnds.event_type = dbms_cq_notification.event_querychange) then
      f( f                               f                         )
      req := utl_http.begin_request(
         'http://mycomp.us.oracle.com/~cjones/cache.php&tabname='
         || ntfnds.query_desc_array(1).table_desc_array(1).table_name);
             tf d        d         (1) t bl d          (1) t bl      )
      resp := utl_http.get_response(req);
      utl_http.end_response(resp);
     end if;
 end;
 /




© 2009 Oracle Corporation
Example - Register 'mycallback' for a
            Query
                y
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 cache
        • PL/SQL callback procedure mycallback()
        • Registered query
          select name f
             l t        from cjtesttab where group_id = 2;
                               jt tt b h              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
         2009 09 23 13:11:39: Table was: CJ.CJTESTTAB
                                          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 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
                 t t i        t          it  t t d li
            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
          Mechanisms
      • Cache Currency
              • Content can get stale over time – invalidate or refresh cache
      • R d O l vs. Updatable C h
        Read-Only   U d bl Cache
              • Updates require synchronization with Oracle database
      • Query Capability
             y     p    y
      • Persistence in the Application Tier
      • Availability
              • Should access to data be available if back end database is not
                                                      back-end
                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 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
          Example f
                 l from htt //ti
                           http://tinyurl.com/dbmsxaex
                                        l    /db
          HTTP Request #1:
          rc := DBMS_XA.XA_START(DBMS_XA_XID(123), DBMS_XA.TMNOFLAGS);
          UPDATE employees SET salary=salary*1.1 WHERE employee_id = 100
                     l           l      l *1 1            l     id 100;
          rc := DBMS_XA.XA_END(DBMS_XA_XID(123), DBMS_XA.TMSUSPEND);

          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”)       XE )
           • Same code base as full database
           • Windows & Linux 32 bit
        • SQL Developer
           • Thick client SQL and PL/SQL development tool
           • Connects to MySQL too
        • Application Express ( Apex )
                                     (“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/php
            • Free book: Underground PHP and Oracle Manual
            • Whitepapers, Articles, FAQs, links to blogs, JDeveloper
          PHP Extension, PHP RPMs
         • Information
         kuassi.mensah@oracle.com
         db360.blogspot.com
         christopher.jones@oracle.com
         blogs.oracle.com/opal
         • SQL and PL/SQL Questions
         asktom.oracle.com
         • ISVs and hardware vendors
                oraclepartnernetwork.oracle.com
                oraclepartnernetwork oracle com

© 2009 Oracle Corporation
© 2009 Oracle Corporation

PHP Oracle Web Applications by Kuassi Mensah

  • 1.
    © 2009 OracleCorporation
  • 2.
    <Insert Picture Here> PHPOracle Web Applications: Best Practices and Caching Strategies Kuassi Mensah Server Technologies Oracle Mensah, Technologies,
  • 3.
    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 Prefetching • Scaling Very Complex Queries • Scaling with Stored Procedures • Caching Strategies • Resultset Caching • Continuous Query Notification and Mid-tier Cache Invalidation • I Memory Database Cache In M D t b C h © 2009 Oracle Corporation
  • 4.
    PHP and theOCI8 Extension © 2009 Oracle Corporation
  • 5.
    Three Tier WebModel © 2009 Oracle Corporation
  • 6.
    What is OCI8? • Main Oracle Database extension for 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
  • 7.
    Get the LatestOCI8 1.3 • php.net • PHP 5 3 Source code Windows binaries 5.3 code, • PECL - PHP Extension Community Library • For updating PHP 4 - 5.2 with OCI8 1.3 • htt // http://oss.oracle.com/projects/php l / j t / h • RPMs for Linux with OCI8 • Unbreakable Linux Network • Oracle's Linux Support program • OCI8 RPM available for PHP • Zend Se e e d Server • Linux, Windows, Mac • Support from Zend © 2009 Oracle Corporation
  • 8.
    Oracle Database 11.1and PHP • Oracle 11.1 was released August 2007 • Connection Pooling - DRCP • Server (database) Query Result Cache • Client Query Result Cache • Query Annotation • Continuous Query Notification • Cube Organized Materialized Views • Total Recall • ... © 2009 Oracle Corporation
  • 9.
    Oracle Database 11.2and 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 Views • Client Query Result Cache • Table Annotation • View Annotation • Client query result cache (CQRC) supported with DRCP • Pre-fetching supported in more places • Edition Based Redefinition • RAC One Node option • ... © 2009 Oracle Corporation
  • 10.
    Agenda • PHP, the OCI8 Extension and Oracle Database • ►Scaling Database Connectivity • Database Resident Connection Pool – Solving the C20K Problem • Logon Storm Shield • Scaling Strategies • 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 • I Memory Database Cache In M D t b C h © 2009 Oracle Corporation
  • 11.
    Database Resident Connection Pool - Solving the C20K problem © 2009 Oracle Corporation
  • 12.
    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 user • Maxes out database server memory • Cannot handle the Digg effect © 2009 Oracle Corporation
  • 13.
    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_persistent oci8.persistent_timeout oci8.ping_interval © 2009 Oracle Corporation
  • 14.
    Database Resident ConnectionPool Solving the C20K Problem Dedicated servers Connection Connection ( DB handle) Broker Session Session Session (User Handle) 1 Session S i (User Handle) (User Session Handle) (User Handle) Oracle Net (User Handle) 2 • Pool of dedicated servers • (1) Server allocated/locked on Appl Connect Appl. • (2) Direct server access after handoff • (3) Server released on Appl. ConnClose . • N man-in-the-middle, l No i th iddl low l t latency © 2009 Oracle Corporation
  • 15.
    DRCP: System Overview ©2009 Oracle Corporation
  • 16.
    PHP DRCP Benchmark • See PHP DRCPC Whitepaper • 2GB RAM • 1 connection broker • 100 pooled servers © 2009 Oracle Corporation
  • 17.
    Sample Sizing for5000 Users Dedicated DRCP Servers Servers (Pool of 100) Database Servers 5000 * 4 MB 100 * 4 MB Session Memory 5000 * 400 KB 100 * 400 KB DRCP Connection 5000 * 35 KB Broker Overhead Total Memory 21 GB 610 MB © 2009 Oracle Corporation
  • 18.
    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
  • 19.
    Configuring and Startingthe POOL • Install PHP OCI8 1.3 and Oracle 11g • Configure the pool (optional) SQL SQL> execute dbms connection pool.configure pool( 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 max think time => 600 600, max_use_session => 500000, max_lifetime_session => 86400); • Sart the pool: SQL> execute dbms_connection_pool.start_pool(); • Set oci8.connection_class in php.ini oci8.connection_class MY_PHP_APP oci8 connection class = MY PHP APP © 2009 Oracle Corporation
  • 20.
    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 Change Change the TNS alias in tnsnames.ora configuration file © 2009 Oracle Corporation
  • 21.
    Some DRCP recommendations. • Read the PHP DRCP whitepaper http://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 of 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 size V$CPOOL_STATS © 2009 Oracle Corporation
  • 22.
    Logon Storm Shield ©2009 Oracle Corporation
  • 23.
    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 capabilities. biliti © 2009 Oracle Corporation
  • 24.
    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 Prefetching g g • Scaling Very Complex Queries • Scaling with Stored Procedures • Caching Strategies • Resultset Caching • Continuous Query Notification and Mid-tier Cache Invalidation • In Memory Database Cache © 2009 Oracle Corporation
  • 25.
    Bind Variables © 2009Oracle Corporation
  • 26.
    Not Binding Literals in SQL statements disable sharing Poor use of cache © 2009 Oracle Corporation
  • 27.
    Binding Scalars © 2009Oracle Corporation
  • 28.
    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 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
  • 29.
    DB Statistics Withoutand With Binding From a query example by Tom Kyte: Not Binding Binding Parse time elapsed 485 36 Parse count (hard) 5,000 1 Latches 328,496 118,614 • Overall system is more efficient • PHP user elapsed time directly benefits © 2009 Oracle Corporation
  • 30.
    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 • Parse time is a relatively small cost • CURSOR_SHARING parameter • S in “ Set “session” or database init.ora • Makes every statement appear to have bound data, but optimizer now doesn't see constants • For bind-unfriendly applications bind unfriendly • Oracle 11g has Adaptive Cursor Sharing • Can have multiple execution plans for same statement © 2009 Oracle Corporation
  • 31.
    Statement Caching © 2009Oracle Corporation
  • 32.
    Client (aka PHP)Statement Caching Less traffic and DB CPU © 2009 Oracle Corporation
  • 33.
    OCI8 Uses theStatement Cache. • No PHP code change needed • Oracle Client library cache of statement text & meta data • On by default in php.ini oci8.statement_cache_size oci8 statement cache size = 20 Unit is number of statements • Set it big enough for working set of statements • Reduces network traffic and context switches ff • 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
  • 34.
    Row Prefetching © 2009Oracle Corporation
  • 35.
    Prefetching Reduces Roundtrips • Temporary buffer cache for query duration No DB access for next fetch Reduces round trips © 2009 Oracle Corporation
  • 36.
    Prefetching Improves QueryTimes Your results will vary © 2009 Oracle Corporation
  • 37.
    Prefetching is Enabledby Default • Enabled by default • Can tune per statement: $s = oci_parse($c, 'select city from locations'); oci_set_prefetch($s, 200) i t f t h($ 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
  • 38.
    PHP OCI8 RowPrefetching. • 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.2 • Oracle 11.2 supports REF CURSOR prefetching too • Tuning goal: • R d Reduce round t i d trips • For big data sets: transfer reasonable chunks, not huge sets © 2009 Oracle Corporation
  • 39.
    Scaling Very ComplexQueries © 2009 Oracle Corporation
  • 40.
    Scaling Very ComplexSQL 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_name, t 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
  • 41.
    Cube Organized MaterializedViews Transparent to SQL Queries SQL Query Materialized Views Region R i Date D t Query Rewrite Product Channel Automatic OLAP Cube Refresh © 2009 Oracle Corporation
  • 42.
    Scaling with StoredProcedures © 2009 Oracle Corporation
  • 43.
    Scaling with StoredProcedures Java or PL/SQL Client Client Any Language Any Language Stored Procedure Call Multiple Unique SQL Calls Java or PL/SQL Calls SQL SQL Up to 10 x Faster! © 2009 Oracle Corporation
  • 44.
    Agenda • PHP, the OCI8 Extension and Oracle Database • S li Scaling D t b Database C Connectivity ti it • Database Resident Connection Pool – Solving the C20K Problem • 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 • In Memory Database Cache © 2009 Oracle Corporation
  • 45.
    Resultset Caching © 2009Oracle Corporation
  • 46.
    Oracle 11g Client& Server Result Caches • Results of queries can be cached • Server 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 Per-client i sqlnet.ora: OCI RESULT CACHE MAX SIZE P li t in l t OCI_RESULT_CACHE_MAX_SIZE • Has a configurable 'lag' time • If no roundtrip within defined time, cache assumed stale © 2009 Oracle Corporation
  • 47.
    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 11.1 Annotation, to queries instead: select /*+ result_cache */ last_name from employees • V$RESULT_CACHE_* views show cache usage © 2009 Oracle Corporation
  • 48.
    No DB AccessWhen Client Cache Used SQL> select parse_calls, executions, sql_text from v$sql where sql_text like '%cjcrc%'; PARSE_CALLS PARSE CALLS EXECUTIONS SQL TEXT SQL_TEXT ----------- ---------- ----------------------------------- 2 100 select * from cjcrc 2 2 select /*+ result_cache */ * from cjcrc © 2009 Oracle Corporation
  • 49.
    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") oci parse($c result_cache $t"); oci_execute($s, OCI_DEFAULT); while ($row = oci_fetch_array($s, OCI_ASSOC)) { foreach ($row as $item) {echo $item "n";}}} $item. n ;}}} $ siege -c 20 -t 30S http://localhost/clientcache.php Without result cache: select * from $t Transaction rate: 32.02 trans/sec With result cache cache: select /*+ result cache */ * from $t result_cache Transaction rate: 36.79 trans/sec Result Caching was approx. 15% better © 2009 Oracle Corporation
  • 50.
    Continuous Query Notification and Mid Tier Cache Invalidation Mid-Tier © 2009 Oracle Corporation
  • 51.
    Continuous Query Notification Problem to solve: Be notified when changes in the database invalidates an existing query result set 2.Upon Change ( p (DMLImpacting g <?php the result set) … Callout 4.Invalidate cache 5.repopulate cache p p … ?> 1. Register 3.Automatic the query Custom cache Notification (Java or PL/SQL database job as noificaion callback) © 2009 Oracle Corporation
  • 52.
    Example - TheCache Depends On This Table $ sqlplus cj/cj create table cjtesttab ( group_id number, name varchar2(20) ); insert into cjtesttab values (1, 'alison'); insert into cjtesttab values (2, 'pearly'); insert into cjtesttab values (2, 'wenji'); © 2009 Oracle Corporation
  • 53.
    Example - ThePHP Cache-Updating Code <?php // cache.php $g = date('Y-m-d H:i:s').": Table was: ".$_GET[tabname]; file_put_contents( /tmp/cqn.txt 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
  • 54.
    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 if (ntfnds.event_type = dbms_cq_notification.event_querychange) then f( f f ) req := utl_http.begin_request( 'http://mycomp.us.oracle.com/~cjones/cache.php&tabname=' || ntfnds.query_desc_array(1).table_desc_array(1).table_name); tf d d (1) t bl d (1) t bl ) resp := utl_http.get_response(req); utl_http.end_response(resp); end if; end; / © 2009 Oracle Corporation
  • 55.
    Example - Register'mycallback' for a Query y 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
  • 56.
    Example Recap • Table cjtesttab • PHP script http://.../cache.php to update the cache • PL/SQL callback procedure mycallback() • Registered query select name f l t from cjtesttab where group_id = 2; jt tt b h id 2 • Aim: refresh mid-tier cache when this query results change © 2009 Oracle Corporation
  • 57.
    Example - InAction • 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 2009 09 23 13:11:39: Table was: CJ.CJTESTTAB 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
  • 58.
    The following isintended 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 t t i t it t t d li 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
  • 59.
    Challenges with CurrentCaching Mechanisms • Cache Currency • Content can get stale over time – invalidate or refresh cache • R d O l vs. Updatable C h Read-Only U d bl Cache • Updates require synchronization with Oracle database • Query Capability y p y • Persistence in the Application Tier • Availability • Should access to data be available if back end database is not back-end available? • Significant Development effort © 2009 Oracle Corporation
  • 60.
    Oracle Database andPHP Roadmap • PHP OCI8 integration with g • TimesTen In Memory Database • A fast in memory, persistent DB • TimesTen In Memory Database Cache • Cache for Oracle Database • No need for separate caching logic © 2009 Oracle Corporation
  • 61.
    Extra Slides andFree Stuff © 2009 Oracle Corporation
  • 62.
    DBMS_XA: Transactions AcrossRequests • Oracle 11gR1 Feature • Can we use it on the web? Upgrading thick client applications? • E Example f l from htt //ti http://tinyurl.com/dbmsxaex l /db HTTP Request #1: rc := DBMS_XA.XA_START(DBMS_XA_XID(123), DBMS_XA.TMNOFLAGS); UPDATE employees SET salary=salary*1.1 WHERE employee_id = 100 l l l *1 1 l id 100; rc := DBMS_XA.XA_END(DBMS_XA_XID(123), DBMS_XA.TMSUSPEND); 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
  • 63.
    Hey Look! FreeStuff • Oracle Instant Client • Easy to install • Client libraries for C, C++, Java, .Net access to a remote DB • Oracle Database Express Edition (aka “XE”) XE ) • Same code base as full database • Windows & Linux 32 bit • SQL Developer • Thick client SQL and PL/SQL development tool • Connects to MySQL too • Application Express ( Apex ) (“Apex”) • Web based Application Development tool • Try it at http://apex.oracle.com/ © 2009 Oracle Corporation
  • 64.
    Some PHP &Oracle Books © 2009 Oracle Corporation
  • 65.
    Oracle Resources • Free Oracle Techology Network (OTN) PHP Developer Center: otn.oracle.com/php • Free book: Underground PHP and Oracle Manual • Whitepapers, Articles, FAQs, links to blogs, JDeveloper PHP Extension, PHP RPMs • Information kuassi.mensah@oracle.com db360.blogspot.com christopher.jones@oracle.com blogs.oracle.com/opal • SQL and PL/SQL Questions asktom.oracle.com • ISVs and hardware vendors oraclepartnernetwork.oracle.com oraclepartnernetwork oracle com © 2009 Oracle Corporation
  • 66.
    © 2009 OracleCorporation