バインディングパラメータを使用している場合にのみ、特定のクエリで奇妙なパフォーマンスの問題が発生します。ここでバインドパラメータのMySQLパフォーマンスの問題
は私のコードです:
$statement = $connection->prepare('SELECT count(item_id) AS count FROM MyView WHERE active = 1 AND context_id = 5 AND snapshot_id = 24');
$time_start = microtime_float();
$statement->execute();
$time_end = microtime_float();
$time1 = round($time_end - $time_start, 4);
$result = $statement->fetch()['count'];
$statement = $connection->prepare('SELECT count(item_id) AS count FROM MyView WHERE active = :filter_active AND context_id = :filter_context_id AND snapshot_id = :filter_snapshot_id');
$statement->bindValue('filter_active', 1, 2);
$statement->bindValue('filter_context_id', 5, 2);
$statement->bindValue('filter_snapshot_id', 24, 2);
$time_start = microtime_float();
$statement->execute();
$time_end = microtime_float();
$time2 = round($time_end - $time_start, 4);
$result = $statement->fetch()['count'];
enter code here
時間の結果:私たちは、この巨大なパフォーマンスの差を取得する理由
time1 = 0.79 sec
time2 = 31.50 sec
は、誰かが私に説明してもらえますか?
おかげ
EDIT &ソリューション: パラメータの型が間違っていました。代わりにPDO :: PARAM_INTを使用してください。 私がparam型を指定しないと、パフォーマンスが非常に悪いことに注意してください。
a)microtime_float()は正確に何を行い、マイクロタイム(true)とどのように違うのですか? b) 'PDO :: PARAM_STR = 2'、' PDO :: PARAM_INT = 1' - なぜ整数値を文字列としてバインドするのですか? c)テーブル構造はどのように見えるのですか、その中にどのくらいのデータがあり、どのindizesを使用していますか? –
文字列としてのバインディングは、私が最初にこの問題について推測するものです。 – Kickstart
ありがとうございました!それは間違っていたバインディングタイプです。 PDO :: PARAM_INTを使用すると0.0005秒の実行時間が得られます! –