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.

Monday, January 10, 2011

What makes your site slow the most? System Calls.

Hello,

Today I will talk about a highly discussed topic. But I'm not going to talk about the usual reasons given for this. I'm going to talk about a much unknown and and un-talked factor. The system calls.

Believe it or not, the most time taking process in site execution is the "System Calls". Whenever you "require" or "include" a file, it makes a system call on server to include that file in your script. So it depends upon how many files you are including in your script. In today's world of MVCs, there is literally no limit of the files you include. This is not to take away the fact that MVCs are marvelous platforms to build any web application (for many reasons) but they do have impact on the speed. The reason behind Magento being slow is much more this.

If you want a phenomenal speed, write the whole code on a single file! I know it sounds ridiculous but the applications who have a HUGE amount of load everyday, do exactly this! Example SourceForge and GForge. They have millions of lines of code in a single file.

However, MVCs do have the work around for this problem. They do caching. Using caching they evade this short coming greatly. And we all know the power of MVCs.

So guys, keep using MVCs for all the reasons. But it's just that when you create a SourceForge or GForge, avoid them!

Monday, October 18, 2010

Amazone EC2 ... A headache! Do not buy it!


My experience on Amazon AWS EC2 hosting.

Thought 1). Oops!! I made a mistake by choosing it.

Thought 2). Oh god!! what have I done by choosing it.

Thought 3). Ummm, it's ok. It's not so bad dicision.

Thought 4). Wow, it's wonderful. Thank god I chose it.

Thought 5). Oh shit!! it can be nightmare sometimes.

Thought 6). Bloody hell!! They don't provide support even if we purchase the most expensive support package.

Thought 7). Woosh!! Finally all done. I lost my sleep for a few nights but at the end I learned a few things.

Also, no matter what level of support package you purchase, absolutely nobody is going to help you. Only your own knowledge of Linux will come to rescue (Ofcourse, only if you choose the Linux server - which is most likely). 

In simple term what they give is a plain server box on which you have full control, nothing else. But the other advantages they provide are awesome like Elastic size of storage, elastic processing resources, elastic IPs, elastic databases etc. The specs looked fantastic to me but there are numerous types of things you have to manage when you are supposed to host a comprehensive website. In short I had to acquire all the knowledge that a linux hosting provider needs to have. In the end it was fun ......... :)

Saturday, September 18, 2010

Check if variable is serialized in PHP

Want to check if a variable is serialized or not in PHP? Here you go ....

/**
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/**
* Tests if an input is valid PHP serialized string.
*
* Checks if a string is serialized using quick string manipulation
* to throw out obviously incorrect strings. Unserialize is then run
* on the string to perform the final verification.
*
* Valid serialized forms are the following:
*
    *
  • boolean: b:1;
  • *
  • integer: i:1;
  • *
  • double: d:0.2;
  • *
  • string: s:4:"test";
  • *
  • array: a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
  • *
  • object: O:8:"stdClass":0:{}
  • *
  • null: N;
  • *
    *
    * @author Chris Smith
    * @copyright Copyright (c) 2009 Chris Smith (http://www.cs278.org/)
    * @license http://sam.zoy.org/wtfpl/ WTFPL
    * @param string $value Value to test for serialized form
    * @param mixed $result Result of unserialize() of the $value
    * @return boolean True if $value is serialized data, otherwise false
    */
    function is_serialized($value, &$result = null)
    {
    // Bit of a give away this one
    if (!is_string($value))
    {
    return false;
    }

    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
    $result = false;
    return true;
    }

    $length = strlen($value);
    $end = '';

    switch ($value[0])
    {
    case 's':
    if ($value[$length - 2] !== '"')
    {
    return false;
    }
    case 'b':
    case 'i':
    case 'd':
    // This looks odd but it is quicker than isset()ing
    $end .= ';';
    case 'a':
    case 'O':
    $end .= '}';

    if ($value[1] !== ':')
    {
    return false;
    }

    switch ($value[2])
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    break;

    default:
    return false;
    }
    case 'N':
    $end .= ';';

    if ($value[$length - 1] !== $end[0])
    {
    return false;
    }
    break;

    default:
    return false;
    }

    if (($result = @unserialize($value)) === false)
    {
    $result = null;
    return false;
    }
    return true;
    }