R
R
Roman Basharin2016-11-10 21:06:01
PHP
Roman Basharin, 2016-11-10 21:06:01

How can I speed up the database connection script?

Salut, guys, tell me if it's possible to rewrite the script better, if I'm a krivoruk.
I watched the script execution speed through

$start = microtime(true);
  //code_here
echo (microtime(true) - $start).' сек.';

And I found that 75% of the time it devours this piece of code
$db = new Database('localhost','devbase','developer','pass');

An example of a constructor and one of the methods responsible for deleting
class Database {
  private $host;
  private $dbName;
  private $user;
  private $pass;
  private $DBH;

  function __construct($host,$dbname,$user,$pass) {
    $this->host = $host;
    $this->dbname = $dbname;
    $this->user = $user;
    $this->pass = $pass;
    $this->DBH = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->user,$this->pass);
  }

  function delete($tableName, $conditions, $priority = '') {

    $query = $this->DBH->prepare("DELETE $priority FROM $tableName WHERE $conditions");
    return $result = $query->execute();

    $query = null;
    $this->DBH = null;
  }
}

What is the logic now:
1. When a page is requested, this object is always created. there is always a query to DB
2. Then in script 1 or several times there is a query like $db->select('phone, address','clients WHERE id=5567');
3. In general, this part, where even a dozen times a request is made to the database in this way to display data, takes about 25% of the script's time, along with the rest of the rendering logic.
At the moment, less than 10 people work with the application, in the future the number of people working at the same time will be several hundred.
The specifics of the work is such that sometimes you need to open a couple of dozen pages, and if several people do it at the same time, then you have to wait 10-15s until everything opens and draws.
In general, not comme il faut, and it would be cool to increase the speed of the script by several tens of percent at once.
The time taken to create this object is about 0.24 seconds, with a total time to display the page of 0.3 seconds. On the scale of 1 person, there is no savings on matches, on the scale of hundreds.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel K, 2016-11-10
@Hellek

Try changing localhost to 127.0.0.1 the connection first tries to resolve the name.

R
RoverWhite, 2016-11-10
@RoverWhite

The time to create a connection to the database server on the local machine is 240ms, which is not normal.
Is there something wrong with the server perhaps?

A
asd111, 2016-11-11
@asd111

What OS are you testing on? What DB? What version of php?
If on windows, then everything is fine - in windows, connecting php to the database is slow. Linux will be faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question