Secrets of MySQL Optimization & Performance Tuning Session by S
MySQL Stored Procedures
Building High Performance Web Applications
by Sonali Minocha, OSSCube
Who Am I?
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)!
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
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
Advantages
Performance Overhead may result in higher latency Increased usage of database server computing power may negatively affect throughput
Disadvantages
How Fast Is the Stored Program Language?
Stored program to find prime numbersCREATE PROCEDURE sp_nprimes(p_num int)BEGIN DECLARE i INT; DECLARE j INT; DECLARE nprimes INT; DECLARE isprime INT; SET i=2; SET nprimes=0; main_loop: WHILE (idate_sub(currdate( ),interval 6 month) ORDER BY customer_id,sale_value DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET last_sale=1;
OPEN sales_csr; sales_loop: LOOP FETCH sales_csr INTO l_customer_id,l_product_id,l_quantity,l_sale_value; IF (last_sale=1) THEN LEAVE sales_loop; END IF;
**
**
IF l_customer_id l_last_customer_id THEN /* This is a new customer so first row will be max sale*/ INSERT INTO max_sales_by_customer (customer_id,product_id,quantity,sale_value) VALUES(l_customer_id,l_product_id,l_quantity,l_sale_value); END IF;
SET l_last_customer_id=l_customer_id;
END LOOP;END
we can use a stored program to retrieve the data in a single pass through the sales table
Using a stored program to optimize a complex self-join
Optimize Correlated Updates
Correlated UPDATE statementUPDATE customers c SET sales_rep_id = (SELECT manager_id FROM employees WHERE surname = c.contact_surname AND firstname = c.contact_firstname AND date_of_birth = c.date_of_birth) WHERE (contact_surname, contact_firstname, date_of_birth) IN (SELECT surname, firstname, date_of_birth FROM employees and );
Here employee table is accessed twice
Stored program alternative to the correlated update
CREATE PROCEDURE sp_correlated_update( ) MODIFIES SQL DATABEGIN DECLARE last_customer INT DEFAULT 0; DECLARE l_customer_id INT ; DECLARE l_manager_id INT;
DECLARE cust_csr CURSOR FOR select c.customer_id,e.manager_id from customers c, employees e where e.surname=c.contact_surname and e.firstname=c.contact_firstname and e.date_of_birth=c.date_of_birth;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET last_customer=1;
Stored program alternative to the correlated update(cont.)
OPEN cust_csr; cust_loop: LOOP FETCH cust_csr INTO l_customer_id,l_manager_id; IF (last_customer=1) THEN LEAVE cust_loop; END IF; UPDATE customers SET sales_rep_id=l_manager_id WHERE customer_id=l_customer_id; END LOOP;
END;
Here table is only accessed once and cursor is used to store data
Performance of a correlated update and stored program alternative
Optimizing Loops : Move Unnecessary Statements Out of a Loop
A poorly constructed loop
WHILE (i