I spend server time if I call one method for each insert request

I am working on PHP on the concept of OOP, this is a class called "connect" that connects to the database and also inserts the query into the 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 is that if I insert every time I call the paste method for each request, will it be a waste of time or spend more time on the server? "or do I need to do only one way to insert 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"); 
+6
source share
2 answers

I assume that you mean the fact that calling a function can be "expensive" compared to the fact that all commands in a huge file are reset.

In principle, yes - it will take more time (theoretically). If you are worried about this? No.

What you need to worry about are parts of your code that actually affect time consumption. In your example, mysql initiation is likely to take 90% of your processing time, if not more. Therefore, you must be sure to connect to mysql only once.

In addition, the insert request may be faster on the mysql side when sending a single request for all insertions. However, this may also be negligible. The best way to determine this is to check and profile your code.

On the bottom line, you should worry about your code being readable and supported in the first place. Then perform profiling after detecting real bottlenecks.

+6
source

Your path will be slower with respect to one request.

see this test http://blog.cnizz.com/2010/05/31/optimize-mysql-queries-fast-mutliple-row-inserts/

UPDATE:

Mysqli test:

 <?php $s = microtime(true); $mysqli = new mysqli("127.0.0.1", "root", "pass", "test", 3306); for($i=0;$i<1000;$i++){ $mysqli->query("INSERT INTO admin SET name='hello world'"); } $e = microtime(true); echo $e-$s; ?> 

28.007468938828 - INNODB

0.19577789306641 - MYISAM

 <?php $s = microtime(true); $mysqli = new mysqli("127.0.0.1", "root", "pass", "test", 3306); $sql = "INSERT INTO admin (`name`) VALUES "; for($i=0;$i<1000;$i++){ $sql.= "('hello world'),"; } $sql = substr($sql,0,-1); $mysqli->query($sql); $e = microtime(true); echo $e-$s; ?> 

0.06469202041626 - INNODB

0.052706003189087 - MYISAM

(tested on Athlon X2 2.7 MHz)

and on Intel I3 with a frequency of 2.4 MHz

+3
source

Source: https://habr.com/ru/post/946775/


All Articles