undefined
undefined

Reputation: 2101

In PDO is there a difference in performance if a int is quoted as a string?

Is there a difference in performance if an integer is bound as a string in a prepared PDO query? In Mysql the queries work either if the value is bound as an int or a string, but is there any difference in the performance, or any pitfalls when doing that?

$pdo = new PDO(
    "mysql:host={$host};port={$port};dbname={$dbname};charset=utf8", 
    $username, 
    $password
);
$statement = $pdo->prepare("SELECT * FROM `table` WHERE `id` = :param");

// Is there any performance difference between the two rows below
$statement->bindValue(":param", 5);
$statement->bindValue(":param", 5, PDO::PARAM_INT);

$statement->execute();

Is there any difference between binding a parameter and specifying its type, or just quoting it as a string?

Upvotes: 4

Views: 436

Answers (2)

Rizier123
Rizier123

Reputation: 59691

If you want to take it exactly the method where you specify the type is slightly faster than where you not specify the type and the default type is PDO::PARAM_STR.

If you run it 1 million time the avarage is as followed:

  • int type specified: 0.53 seconds ($stmt->bindValue(":param", 5, PDO::PARAM_INT);)
  • no type specified: 0.66 seconds ($stmt->bindValue(":param", 5);)
  • str type sepcified: 0.70 seconds ($stmt->bindValue(":param", 5, PDO::PARAM_STR);)

Tested with PHP version 5.6.3; 32bit Windows;

Upvotes: 6

Kristiyan
Kristiyan

Reputation: 1663

In my experience, there is no such performance different. MySQL automatically will convert string to number (in depends on column type).

If I want to be precise, PDO::PARAM_STR is more slowly then PDO::PARAM_INT. PDO::PARAM_STR is by default.

It's because the server have to check your data.

Upvotes: 0

Related Questions