DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman)
Upcoming SlideShare
Loading in...5
×
 

DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman)

on

  • 11,964 views

Dutch PHP Conference

Dutch PHP Conference

Statistics

Views

Total Views
11,964
Views on SlideShare
11,923
Embed Views
41

Actions

Likes
2
Downloads
294
Comments
1

3 Embeds 41

http://www.slideshare.net 21
http://www.phpconference.nl 19
https://react.parse.nl 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman) DPC2007 MySQL Stored Routines for PHP Developers (Roland Bouman) Presentation Transcript

  • Amsterdam, June 16 2007 ● MySQL: Quick Introduction ● MySQL Stored Routines for PHP developers ● Questions and Discussion Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 1
  • Roland Bouman (rpbouman.blogspot.com) Certification Developer MySQL AB, Leiden ● MySQL Community Contributor since 2005 ● Joined MySQL AB in July 2006 ● Certification Developer: ● MySQL 5.1 Cluster DBA exam (CMCDBA) ● MySQL Associate exam (CMA) ● Attained: ● CMDEV ● CMDBA ● Formerly: Consultant & Application Developer (mostly Oracle, some MS SQL) Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 2
  • Amsterdam, June 16 2007 ● MySQL: Quick Introduction ● MySQL Stored Routines for PHP developers ● Questions and Discussion Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 3
  • MySQL: Quick Introduction ● MySQL Software Products ● RDBMS (Database) ● Monitoring and Advisory Service ● Drivers and APIs ● Client (GUI) Tools & Utilities ● MySQL Professional Services ● Technical Support ● Consulting ● Training ● Certification ● Indemnification Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 4
  • MySQL Open Source RDBMS Products ● Generally Available, Stable Releases ● MySQL 5.0 Server Community Edition ● Patches from community ● MySQL 5.0 Server Enterprise Edition ● Stablility and Robustness, Early Bugfixes ● Monitoring and Advisory Service ● Binaries ● Development Releases: ● MySQL 5.1 (New: Events, Partitioning) ● MySQL 6.0 (New: Falcon) ● Other RDBMS Products: ● MySQL Cluster / Carrier Grade Edition ● Embedded ● MaxDB (SAP Certified) Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 5
  • MySQL Monitoring and Advisory Service Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 6
  • MySQL Drivers and APIs ● For PHP ● ext/mysql: “MySQL Functions” ● ext/mysqli: “MySQL Improved Extension” ● mysqlnd: “MySQL native driver for PHP” ● Beta 5. ● Built Into PHP 5; PHP 6 ● Replaces libmysql ● Other: ● ODBC: Connector/ODBC ● JDBC: Connector/J ● ADO.NET: Connector/.NET Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 7
  • Amsterdam, June 16 2007 ● MySQL: Quick Introduction ● MySQL Stored Routines for PHP developers ● Questions and Discussion Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 8
  • MySQL Stored Routines ● Overview of MySQL Stored Routines ● MySQL Stored Routine Language ● PHP Techniques ● Use cases ● Best Practices Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 9
  • MySQL Stored Routines ● Overview of MySQL Stored Routines: ● Terminology, Purpose, Application ● MySQL Stored Routine Language ● Block Structure, Parameters and Variables, Flow Control Constructs, SQL inside stored routines. ● PHP Techniques ● Creating and Calling Stored Procedures, Processing Result set, Handling Multiple Result sets. ● Use cases ● Best Practices ● What to do on the client, and what on the server ● What to do in PHP, and what in Stored Routines. ● Performance: how MySQL Stored Routines can help or hurt performance. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 10
  • MySQL Stored Routines: Overview ● Programs as Database Schema Objects ● Executed in-process with the Database ● Types of Stored Routines: ● Procedures ● Functions ● Triggers ● Events (Temporal triggers; new in MySQL 5.1) ● Language: ● Subset of Standard SQL:2003 SQL/PSM ● Procedural, Block structured ● Do not confuse with User Defined Functions (UDF)! ● Available as of MySQL 5.0 (October 2005) Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 11
  • MySQL Stored Routine Types: Overview ● Stored Procedures & Functions ● Encapsulate tasks or Calculations for reuse ● Single point of definition for Business Logic ● Source Safely stored and backed up ● Added layer of Security ● Triggers ● Data-Driven ● Enforce Data quality through Basic validation ● Enforce complex Business Rules ● Automatically Update Aggregate tables ● Events (MySQL Server 5.1 beta) ● Schedule Code Execution in time. ● Use instead of cron or windows event scheduler ● Automatically Update Aggregate tables Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 12
  • MySQL Stored Routines: Purpose / Advantages ● Performance ● Save network roundtrips, lower latency ● Portability and Reuse ● Single point of definition ● Reusable from many application contexts ● Security ● DEFINER versus INVOKER ● Grant only Execution Privilege ● Ease of Maintenance ● Code stored in the database ● Browse using information_schema database ● 'Headless' administrative tasks ● No additional runtime environment required Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 13
  • MySQL Stored Routines: Caveat / Disadvantages ● Performance ● Overhead may result in higher latency ● Increased usage of database server computing power may negatively affect throughput ● Portability and Reuse ● Which point of view? ● Database portability ? ● Or Application portability? Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 14
  • MySQL Stored Routines ● Overview of MySQL Stored Routines: ● Terminology, Purpose, Application ● MySQL Stored Routine Language ● Block Structure, Parameters and Variables, Flow Control Constructs, SQL inside stored routines. ● PHP Techniques ● Creating and Calling Stored Procedures, Processing Result set, Handling Multiple Result sets. ● Use cases ● Best Practices ● What to do on the client, and what on the server ● What to do in PHP, and what in Stored Routines. ● How MySQL Stored Routines can help or hurt performance. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 15
  • MySQL Stored Routine Language ● Subset of Standard SQL “Persistent Stored Modules” (SQL/PSM) ● Procedural constructs with embedded SQL ● Parameters and (Local) variables ● Manipulate values ● Statement Sequence ● execute statements in order ● Choice ● conditionally execute a particular sequence ● Repetition ● execute a particular sequence multiple times ● A bit like Pascal with embedded SQL statements ● Valid inside all stored routine types ● Can be mixed with most SQL statements Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 16
  • Creating a MySQL Stored Procedure • Prerequisite: CREATE ROUTINE and ALTER ROUTINE privileges CREATE PROCEDURE sp_hello(   p_who VARCHAR(32) ) SELECT CONCAT('Hello, ',v_what,'!!'); DDL statement • CREATE PROCEDURE • Created in the current schema (= database) Name (sp_hello) must be unique with in the • schema, may be qualified (my_db.sp_hello) Parameter (p_who): IN parameter by default • • Procedure body is one single statement, in this case, an ordinary SQL SELECT statement. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 17
  • Calling a MySQL Stored Procedure • Prerequisite: EXECUTE ROUTINE privilege CALL sp_hello('PHP'); statement • CALL • Name identifies the procedure within the schema, and maybe qualified: CALL my_schema.sp_hello('PHP') • Must pass a parameter value Result: Hello, PHP! • Result set returned to the client Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 18
  • Generic Statement Sequence: BEGIN...END CREATE PROCEDURE sp_greating(   p_who  VARCHAR(32) , p_what VARCHAR(32) ) BEGIN   SELECT CONCAT('Hello ', p_who);   SELECT CONCAT(p_what,'!'); END is a compound statement; it may • BEGIN...END contain multiple other statements. • Contained statements executed Sequentially (in order of appearance) • (Sidenote: 2 result sets are returned to the client) Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 19
  • Variables and Parameters CREATE PROCEDURE sp_fibonacci(   INOUT p_m INT, INOUT p_n INT, OUT p_s DOUBLE)  BEGIN   DECLARE v_m INT DEFAULT COALESCE(p_m,0);   DECLARE v_n INT DEFAULT COALESCE(p_n,1);   SET p_m := v_n;      ­­ single assignment   SET p_n := v_m + v_n ­­ multiple   ,   p_s := p_m/p_n;  ­­ assignments   SELECT p_m, p_n, p_s; END; and INOUT parameters • IN, OUT • DECLARE local variables, optionally assign a default value • Use SET to assign values to one or more variables Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 20
  • Variables and Parameters call sp_fibonacci(@m,@n,@s); +­­­­­­+­­­­­­+­­­­­­+ | p_m  | p_n  | p_s  | +­­­­­­+­­­­­­+­­­­­­+ |    1 |    1 |    1 | +­­­­­­+­­­­­­+­­­­­­+ call sp_fibonacci(@m,@n,@s); +­­­­­­+­­­­­­+­­­­­­+ | p_m  | p_n  | p_s  | +­­­­­­+­­­­­­+­­­­­­+ |    1 |    2 |  0.5 | +­­­­­­+­­­­­­+­­­­­­+ call sp_fibonacci(@m,@n,@s); +­­­­­­+­­­­­­+­­­­­­­­­­­­­+ | p_m  | p_n  | p_s         | +­­­­­­+­­­­­­+­­­­­­­­­­­­­+ |    2 |    3 | 0.666666666 | +­­­­­­+­­­­­­+­­­­­­­­­­­­­+ Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 21
  • Variable Scope / Visibility BEGIN   DECLARE v_script, v_http VARCHAR(32);   SET v_script := 'PHP', v_http := 'Apache';   SELECT v_script, v_http;   BEGIN     DECLARE v_http VARCHAR(32);     SET v_http := 'lighttpd';     SELECT v_script, v_http;   END;    SELECT v_script, v_http; END; • Variables are visible only inside the declaring block • Nearest Scope: inner declarations mask outer ones Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 22
  • Choice Constructs ● Compound statements ● IF...END IF statement ● Simple test of a single condition ● Conditionally start a sequence of statements (branch) ● Optionally, chooses between two branches Don't confuse with the IF() function! ● ● CASE...END CASE statement ● Conditionally starts one out of multiple branches ● Simple CASE statement ● Just like switch in PHP ● Searched CASE statement ● Just like nested if...elseif...else in PHP ● Don't confuse with the CASE..END expression! Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 23
  • Syntax: IF...END IF ● Tests condition, branches when TRUE ● Conditional branch can contain a sequence //main, unconditional branch IF <condition> THEN   <statements>  ­­ “true” branch END IF; ● Optionally, include a branch for the other case: IF <condition> THEN   <statements>  ­­ ”true” branch ELSE   <statements>  ­­ ”false” branch END IF; Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 24
  • IF statement vs IF function ● An IF statement chooses between sequences of statements IF CURRENT_TIME < '12:00:00' THEN   SELECT 'Good Morning'; ELSE   SELECT 'Good Afternoon'; END IF; function chooses between expressions ● IF SELECT IF(CURRENT_TIME < '12:00:00'        , 'Good Morning'        , 'Good Afternoon'        ); Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 25
  • Syntax: Simple CASE..END CASE ● Evaluate expression and compare ● Conditional branch can contain a sequence ● Optional ELSE branch ● Just like switch...case in PHP CASE <expression>    WHEN <expression1> THEN      <statements>     WHEN <expression2> THEN     <statements>     ELSE      <statements>   END CASE; Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 26
  • Syntax: Searched CASE..END CASE ● Search first TRUE condition, then branch ● Conditional branch can contain a sequence ● Optional ELSE branch ● Just like if...elsif...else in PHP CASE   WHEN <condition>     THEN <statements>     WHEN <expression>     THEN <statements>     ELSE      <statements>   END CASE; Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 27
  • Repetition ● Unstructured Loop ● No explicit logic to end the loop LOOP   <statement>   END LOOP; ● Structured ● Logic to end the loop is part of the construct REPEAT WHILE   <statement>   <condition> UNTIL DO   <condition>   <statement> END REPEAT; END WHILE; ● Iterate: ● Leave: exit the current block Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 28
  • Triggers CREATE   [DEFINER = { <user­name> | CURRENT_USER }] TRIGGER <trigger­name>    {BEFORE | AFTER}   {INSERT | UPDATE | DELETE} ON <table­name> FOR EACH ROW    <single­statement> ● Automatically executed in response to row- level events occurring on table ● Can refer to OLD and NEW pseudo-records ● INSERT: NEW ● DELETE: OLD both OLD and NEW ● UPDATE: ● Executed as part of transaction Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 29
  • Event Scheduler (Temporal Triggers) ● New in MySQL 5.1 ● Automatically executed according to time schedule ● Can be recurring ● Can be scheduled to start in the future ● Can be instructed to clean itself up Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 30
  • MySQL Stored Routines ● Overview of MySQL Stored Routines: ● Terminology, Purpose, Application ● MySQL Stored Routine Language ● Block Structure, Parameters and Variables, Flow Control Constructs, SQL inside stored routines. ● PHP Techniques ● Creating and Calling Stored Procedures, Processing Result Set, Handling Multiple Result Sets. ● Use cases ● Best Practices ● What to do on the client, and what on the server ● What to do in PHP, and what in Stored Routines. ● How MySQL Stored Routines can help or hurt performance. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 31
  • PHP and MySQL Stored Routines ● Two relevant PHP extensions ● MySQL Functions (ext/mysql) ● CREATE PROCEDURE and CALL work fine ● Just use the PHP function mysql_query() ● However, obtaining a result set is impossible ● MySQL Improved extension (ext/mysqli) ● use mysqli_query() for one result set ● For multiple resultsets, use mysqli_multi_query() ● mysqli_use_result() ● mysqli_store_result() ● mysqli_next_result() ● mysqli_more_results() ● Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 32
  • mysql_query() and Stored Routines <?php   $db = mysql_connect($host,$usr,$pwd);   mysql_select_db('test',$db);   $result = mysql_query(     quot;CALL sp_hello('PHP')quot;,$db   );   $num_rows = mysql_affected_rows($db);   echo '<br/>num: ', $num_rows;   echo '<br/>msg: ', mysql_error($db);   echo '<br/>no: ', mysql_errno($db); ?> num: ­1 msg: PROCEDURE test.sp_hello can't return  a result set in the given context no: 1312 Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 33
  • mysqli_query() and Stored Routines <?php   $db = mysqli_connect($host,$usr,$pwd);   $db­>select_db('test');   $result = mysqli_query(     $db,quot;CALL sp_hello('PHP')quot;   );   $row = mysqli_fetch_row($result);   echo $row[0]; ?> Hello, PHP! Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 34
  • Handling Multiple result sets <?php   $db = mysqli_connect($host,$usr,$pwd);   $db­>select_db('test');   $db­>multi_query(     quot;CALL sp_greating('PHP','Good Morning')quot;   );   while($result = $db­>store_result()) {     while ($row = $result­>fetch_row()) {       echo $row[0];     }     $result­>close();     $db­>next_result();   } ?> Hello PHP, Good Morning! Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 35
  • MySQL Stored Routines ● Overview of MySQL Stored Routines: ● Terminology, Purpose, Application ● MySQL Stored Routine Language ● Block Structure, Parameters and Variables, Flow Control Constructs, SQL inside stored routines. ● PHP Techniques ● Creating and Calling Stored Procedures, Processing Result set, Handling Multiple Result sets. ● Use Cases ● Best Practices ● What to do on the client, and what on the server ● What to do in PHP, and what in Stored Routines. ● How MySQL Stored Routines can help or hurt performance. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 36
  • MySQL Stored Routine Use Cases ● Stored Procedures ● subtypes and vertical partitioning ● data intensive transformation ● Stored Functions ● domain specific calculations ● data transformation ● Triggers ● Auditing ● Automatically Aggregate tables ● Events ● Logging status ● Updating aggregate tables “Materialized views” ● ETL processes Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 37
  • MySQL Stored Routines ● Overview of MySQL Stored Routines: ● Terminology, Purpose, Application ● MySQL Stored Routine Language ● Block Structure, Parameters and Variables, Flow Control Constructs, SQL inside stored routines. ● PHP Techniques ● Creating and Calling Stored Procedures, Processing Result set, Handling Multiple Result sets. ● Use Cases ● Best Practices ● What to do on the client, and what on the server ● What to do in PHP, and what in Stored Routines. ● How MySQL Stored Routines can help or hurt performance. Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 38
  • MySQL Stored Procedures: Best Practices ● Use pure SQL when you can ● Use stored procedures for data-intensive operations ● Don't use stored procedures for complex computation ● Don't use stored procedures for single layer encapsulation ● Simple CRUD layers don't scale ● Stored Procedure should add significant functionality ● Return multiple result sets from stored procedures to reduce network roundtrips Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 39
  • MySQL Triggers: Best Practices ● Use triggers to enforce integrity of data ● Using triggers does not mean the application can forget about validation Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 40
  • Amsterdam, June 16 2007 ● MySQL: Quick Introduction ● MySQL Stored Routines for PHP developers ● Questions and Discussion Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 41
  • Copyright 2007 MySQL AB The World’s Most Popular Open Source Database 42