Saturday, November 10, 2007

Stored Procedures

What are Stored Procedures ?
MySQL 5.0 finally introduces functionality for Stored Procedures. So what exactly are stored procedures? That is the kind of question that gets database professionals who use other DBMS's raising their eyebrows. Stored procedures have been integral to Oracle, PostgreSQL, DB-2, MS-SQL server and others for years, and it has long been a sore point that MySQL has not had them. But there is no snobbery here - if you are a MySQL newbie, or have been using MySQL for years and want to find out what all the fuss is about, read on. If it is your eyebrows that are raised, and you just want to know how MySQL implements them, you will be relieved to know MySQL stored procedures are very similar to the DB2 implementation, as both are based on the SQL:2003 standard.

A stored procedure is simply a procedure that is stored on the database server. MySQL developers have to date unthinkingly written and stored their procedures on the application (or web) server, mainly because there hasn't been an option. That has been limiting. Some have claimed that there are two schools of thought - one claiming that logic should be in the application, the other saying it should reside in the database. However, most professionals would not bind themselves to one or other viewpoint at all times. As always, there are times when doing either makes sense. Unfortunately, some of the staunchest adherents of the in the application school are only there because until now they have had no choice, and it is what they are used to doing. So why would we want to place logic on the database server?

Why use Stored Procedures ?
  • They will run in all environments, and there is no need to recreate the logic. Since they are on the database server, it makes no difference what application environment is used - the stored procedure remains consistent. If your setup involves different clients, different programming languages - the logic remains in one place. Web developers typically make less use of this feature, since the web server and database server are usually closely linked. However, in complex client-server setups, this is a big advantage. The clients are automatically always in sync with the procedure logic as soon as its been updated.
  • They can reduce network traffic. Complex, repetitive tasks may require getting results, applying some logic to them, and using this to get more results. If this only has to be done on the database server, there is no need to send result sets and new queries back and forth from application server to database server. Network traffic is a common bottleneck causing performance issues, and stored procedures can help reduce this. More often though, it is the database server itself that is the bottleneck, so this may not be much of an advantage.
A Simple Example
stored procedure is simply some SQL statements.This one will simply say 'Hello'.
mysql> CREATE PROCEDURE Hello() SELECT 'Hello';
Query OK, 0 rows affected (0.00 sec)

It is as simple as that. And to call it:

mysql> CALL Hello()\G
*************************** 1. row ***************************
Hello: Hello
1 row in set (0.00 sec)

Hardly useful, but the basics are there. CREATE PROCEDURE sp_name() will define the procedure, and CALL sp_name() will call the procedure.

Parameters

The real benefit of a stored procedure is of course when you can pass values to it, as well as receive values back. The concept of parameters should be familiar to anyone who has had experience with any procedural programming experience.

There are three types of parameter:

  • IN: The default. This parameter is passed to the procedure, and can change inside the procedure, but remains unchanged outside.
  • OUT: No value is supplied to the procedure (it is assumed to be NULL), but it can be modified inside the procedure, and is available outside the procedure.
  • INOUT: The characteristics of both IN and OUT parameters. A value can be passed to the procedure, modified there as well as passed back again.

Mastery of stored procedures does require knowledge of session variables. Most of you probably know how to use session variables already, but if not, the concept is simple. You can assign a value to a variable, and retrieve it later.

Calling Stored Procedures from PHP
Consider the Procedure ViewComment and it has a IN parameter ID. The Procedure created with the following code
DELIMITER $$

DROP PROCEDURE IF EXISTS `Murali_InnoDB`.`ViewComment` $$
CREATE DEFINER=`root`@`%` PROCEDURE `ViewComment`(ID integer)
BEGIN
select * from tblPerson LEFT JOIN tblComment ON (tblComment.fk_PersonID = tblPerson.ID) where tblPerson.ID = ID;
END $$

DELIMITER ;

Then call Procedure ViewComment from a PHP file, it will be described below.

No comments:

Post a Comment

Yahoo! News: Technology News