Showing posts with label stored procedure. Show all posts
Showing posts with label stored procedure. Show all posts

Thursday, January 20, 2011

When to use Stored Procedures?

Hello,

Before some days, I talked about system calls. While including a file to the script is a system call, connecting to database server is also a system call. So obviously it will make the site slow.

There is a popular belief that SPs are helpful in getting faster database performance. But very few know why. Most people believe that it is because the code is pre-compiled. This is not true. The fact that you can pass variables to a SP, it has to be dynamic and cannot be pre-compiled. So. is SP really faster as they say? The answer is Yes and No. A SP is fast is it is used for appropriate reason. Otherwise there is no difference. So what is the right use of SP?

As I said in the beginning, all the database calls are system calls. So the site becomes slow when you fire more and more queries on the database. In most occasions this is inevitable. But in some cases there is an alternative. Sometimes when you are doing database operations, you fire a query to get some result. Now based on this result you have to fire another query to get some more result. Also, this process may not be possible joins. So in this case, you would be firing two system calls. So instead of firing two queries, just create a stored procedure that would fetch the first value from database, do the necessarily operation, fire the next query and give you the result as the output. Thus you make only one system call. The fact that the stored procedure is the part of the database engine, it would not make extra system call when it is firing a query. So here you go, you get two query worth output with only one system call. so it is now faster!

Hope I made sense here.

Friday, March 19, 2010

Array in MySql stored procedures

Hi guyz,

I saw a weird short coming in MySql stored procedures today.
It doesn't allow to pass in or pass out arrays.
The reason (I would say unreasonable reason) is that they only support variable types that they support for the data types in database storage. May be the architecture of MySql limits them. I'm not an expert of database engine architectures by any means but if other databases can offer, MySql MUST be able to offer this facility.

The alternative for this (as explained in MySql's developer forum) to create a temporary table in which you can store the output (or input) of the stored procedure and then you can fetch them.

Now, why would I do this? As for performance optimization purpose I would want to keep the database calls by PHP to MySql in minimum number. So when I want to make multiple fetch operation for a single task I would use stored procedure. But if I have to redirect my output in an another table I will have to make a PHP call to fetch the result from the tmp table anyways!! This would kill it's very reason.

You can pass CSV in string variable in place of a single dimension array, but what about multi dimension array? 9 out of 10 times you would need multi dimension array in practical scenario.

So what is the solution? Sadly, nothing.
Suddenly an alternative blinked to me and I tried it.
I simply furnished the output array in serialized format manually from the stored procedure.
Since the array became a string it was easy to pass to PHP, and unserialize an array in PHP is a cake walk!!

Try passing the following string from a stored procedure to PHP

a:2:{i:0;a:4:{s:5:"title";s:29:"MySql does not support arrays";s:11:"description";s:47:"Strange problem!! MySql does not support arrays";s:11:"website_url";s:20:"kapsdave.blogger.com";s:6:"number";i:1;}i:1;a:4:{s:5:"title";s:50:"How to pass out array in stored procedure in MySql";s:11:"description";s:35:"You can do this using serialization";s:11:"website_url";s:20:"kapsdave.blogger.com";s:6:"number";i:1;}}Array ( [0] => Array ( [title] => MySql does not support arrays [description] => Strange problem!! MySql does not support arrays [website_url] => kapsdave.blogger.com [number] => 1 ) [1] => Array ( [title] => How to pass out array in stored procedure in MySql [description] => You can do this using serialization [website_url] => kapsdave.blogger.com [number] => 1 ) )

And then unserialize the above text.
You will get an array similar to the following.

Array ( [0] => Array ( [title] => MySql does not support arrays [description] => Strange problem!! MySql does not support arrays [website_url] => kapsdave.blogger.com [number] => 1 ) [1] => Array ( [title] => How to pass out array in stored procedure in MySql [description] => You can do this using serialization [website_url] => kapsdave.blogger.com [number] => 1 ) )

I'm not sure if anybody has tried this before but I could not find anybody mentioning this on web so I finally decided to write a blog post.

Hope this helps!!