Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

am i wasting server time if i call one method for each request for insertion

i am working on PHP on OOP's concept, this is the class called 'connect' which connects to database and also inserts the request in database_

class connect(){
        public function insert($column, $value)
            {
            $insert = mysqli_query($this->con, "INSERT INTO $tablename ($column)
    VALUES ('$value')");
            }/* end of insert method */
}/* end of class 'connect' */

all i just want to know that if i insert each time by calling 'insert' method for each request, will it be a waste of time or wasting more server time?, or should i just make only one method for inserting all the request at a time?;

$call = new connect("localhost", "root", "", "facebook","fb1");
$call->insert("username", "testInsert141");
$call->insert("username", "testInsert141");
$call->insert("username2", "testInsert142");
$call->insert("username3", "testInsert143");
like image 338
GAURAV MAHALE Avatar asked Jun 07 '13 12:06

GAURAV MAHALE


1 Answers

I assume you're referring to the fact that calling a function can be "costly", compared to dumping all commands in a huge file.

Basically, yes - it will take more time (in theory). Should you worry about this time? No.

What you should worry about is the parts of your code that actually has an impact of time consumption. In your example, the mysql initiation is likely to take 90% of your processing time, if not more. You should therefor make sure you only connect to mysql once.

Also, the insert query may be faster on the mysql side, when sending one single query to do all inserts. However, this is also likely to be negligible. The best way to determine this is to test, and profile your code.

Bottom line - you should worry about making your code readable and maintainable first and foremost. Then do profiling after to detect real bottlenecks.

like image 180
Jon Skarpeteig Avatar answered Sep 27 '22 15:09

Jon Skarpeteig