No announcement yet.

How to optimize your code

  • Filter
  • Time
  • Show
Clear All
new posts

  • How to optimize your code

    I am a PHP Developer and i am working on this technology from last 4 yrs. I have worked on problems regarding the performance of the site. Many clients came to me with the same issue. As a PHP developer i would like to share my knowledge with all of you.

    As all of us have the question in our mind that 'How to improve the site performance and page loading time?'.

    With my experiance i would like to categorised the optimization in following categories
    1. Code optimization.
    2. Query optimization.
    3. DB optimization.

    All are may aware of 'Query Optimization' and 'DB Optimization' now lets disscuss about 'Code Optimization'.

    What is Code Optimization : This is really intresting while you are developing any site.
    The code optimization inclues how do you write the code. We have a bad habbit to write code without having any idea of site performance, We do add lots of looping conditions which we can reduce, The looping conditions also takes lots of time to load the page.

    e.g Say i have to show records from two tables customer_master, customer_details. Some people do fetch all the records from customer_master table and in while fetching the rows they will fired the second query in loop to get the details from custoer_details table. If we have 100 records in both the tables then it will first execute one query to get records from customer_table and in loop it will execute 100 queries to get records from customer_details table.

    Now lets see the following code snippet to get an idea of what i want to explain.

    $sql = “select * from customer_master”; // query to recirds from customer_master table
    $res = mysql_query($sql);
    while($row = mysql_fetch_array($res))
    $sqlDetails = “select * from customer_details where cust_id = '”.$row[“cust_id”].”'”; // query to get customer details from custoer_details table
    $resDetails = mysql_query($sqlDetails)
    Now think if we have thousands or more customers. It will slow down the page loading time as we get more and more records in the table.

    To avoid such a problem we can simply join both tables in a single query
    So the code should be like this:
    $sql = “select * from customer_master t1, customer_details t2 where t1.cust_id = t2.cust_id”; // query to get customer details from two tables
    $res = mysql_query($sql);
    It a very basic and simple logic to optimize the site performance and improve the page loading time.

    Now take a another example: When we are adding pagination/navigation to the page.

    e.g We have to add navigation for customer list page. In such case we generally use one function of pagination class and we will pass the query to that function and that function will return result with navigation. But we do not check how it's working.

    We do use same query to count the total records are present in table but that is very bad way of coding in respect of optimization.

    Suppose we have one function navigation() and we will pass the query, start and end limit. For navigation we always need to count all the records to show the total no of pages. So we will execute same query twice. Firstly to get all total records and secondly to get only some records using start and end limit .

    Now suppose query is like this “select * from customer_master

    So we will count total records using mysql_num_rows() function, but the query will take more time to execute when we will have records in thousands.

    To optimize this use separate query to get total no of records, I mean we can pass two separate query to navigation function, one will count total records and another will give the limited list

    To count the records we can use query as “select count(customer_id) from customer_master”.

    The mysql 'count()' function will take less time to execute rather than the way which we discussed above.

    I hope this will help other developers....

  • #2
    now what is DataBase optimization?

    Adding indexes to the table is the best way to optimization.

    - We can add index or full index to the table fields. We can add indexes to numeric data fields and full text to varchar or text data fields.

    - To reduce the Overhead of the table we should always optimize tables by executing query :
    PHP Code:
    OPTIMIZE TABLE  `table name

    - I have implemented one simple logic to optimization, We can set cron to optimize all the tables or just execute optimization query after you deleteing records from php code.

    example :
    PHP Code:
    $sql="delete from user_master where id='1'"// query to delete record
    $res mysql_query($sql);

    $sql="OPTIMIZE TABLE  `user_master"// query to optimize table
    The table size will get increased if we do not optimize it. And this will also cause to slow down the query executions

    You can check size of 'Overhead' in phpMyAdmin. Check the overheads size in table structure


    • #3
      Web developer

      Really useful especially for newbies. Thanks a lot, lol.
      Cheap website hosting india


      • #4

        Thanks for sharing this usefull inormation.i would like to say By optimizing an executable, you can achieve a balance between fast execution speed and small code size…
        I found a few things to be interesting to increase your web site performance—
        Minimize HTTP Requests
        Use a Content Delivery Network
        Add an Expires or a Cache-Control Header
        Gzip Components
        Put Stylesheets at the Top
        Put Scripts at the Bottom
        Make JavaScript and CSS External
        Reduce DNS Lookups