make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $db = Illuminate\Support\Facades\DB::connection('mysql')->getPdo(); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // A connection-local temporary table disappears on exit. No application tables // are dropped, indexed, truncated or modified by this measurement. $db->exec('CREATE TEMPORARY TABLE benchmark_quote_history (id CHAR(26) PRIMARY KEY, dealer_id BIGINT UNSIGNED NOT NULL, status VARCHAR(16) NOT NULL, created_at DATETIME NOT NULL, subtotal_cents INT UNSIGNED NOT NULL) ENGINE=InnoDB'); $db->beginTransaction(); for ($batch=0; $batch<50; $batch++) { $values=[]; $marks=[]; for ($offset=1; $offset<=1000; $offset++) { $i=$batch*1000+$offset; $marks[]='(?,?,?,?,?)'; array_push($values,str_pad((string) $i,26,'0',STR_PAD_LEFT),(($i*17)%250)+1,['pending','approved','converted'][($i*7)%3],gmdate('Y-m-d H:i:s',1735689600+$i*60),5000+(($i*7919)%100000)); } $db->prepare('INSERT INTO benchmark_quote_history VALUES '.implode(',',$marks))->execute($values); } $db->commit(); $sql='SELECT id, subtotal_cents, created_at FROM benchmark_quote_history WHERE dealer_id = ? AND status = ? ORDER BY created_at DESC, id DESC LIMIT 20'; $args=[42,'approved']; function measuredQuery(PDO $db,string $sql,array $args): array { $plan=$db->prepare('EXPLAIN ANALYZE '.$sql);$plan->execute($args);$raw=$plan->fetchAll(PDO::FETCH_COLUMN); $query=$db->prepare($sql);for($i=0;$i<5;$i++){$query->execute($args);$query->fetchAll(PDO::FETCH_ASSOC);} $times=[];$rows=[]; for($i=0;$i<30;$i++){$start=hrtime(true);$query->execute($args);$rows=$query->fetchAll(PDO::FETCH_ASSOC);$times[]=(hrtime(true)-$start)/1e6;} sort($times); return ['explain_analyze'=>$raw,'median_ms'=>round(($times[14]+$times[15])/2,6),'p95_ms'=>round($times[28],6),'rows_returned'=>count($rows),'result_sha256'=>hash('sha256',json_encode($rows,JSON_THROW_ON_ERROR))]; } $before=measuredQuery($db,$sql,$args); $db->exec('CREATE INDEX dealer_status_recent ON benchmark_quote_history(dealer_id,status,created_at)'); $after=measuredQuery($db,$sql,$args); if($before['result_sha256']!==$after['result_sha256'])throw new RuntimeException('Optimization changed the result rows.'); $result=['measured_at_utc'=>gmdate(DATE_ATOM),'runtime'=>['php'=>PHP_VERSION,'database'=>$db->query('SELECT VERSION()')->fetchColumn(),'driver'=>'pdo_mysql','os'=>PHP_OS_FAMILY],'dataset'=>['type'=>'synthetic','rows'=>50000,'dealers'=>250,'table'=>'connection-local temporary quote-history slice, same filter/order/limit shape as quote listing','seed'=>'deterministic integer sequence; see mysql-benchmark.php'],'method'=>['runs'=>30,'warmups'=>5,'clock'=>'hrtime monotonic','query'=>$sql,'binds'=>$args,'index'=>'dealer_status_recent(dealer_id,status,created_at)','environment'=>'disposable GitHub CI MySQL service; single client, warm query slice, no customer data or public endpoint load'],'before'=>$before,'after'=>$after,'limits'=>'Engine/query-slice evidence only. Not a production throughput, concurrency, authentication or full-dataset benchmark.']; $path=__DIR__.'/../../evidence/mysql-query-benchmark.json'; file_put_contents($path,json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_THROW_ON_ERROR).PHP_EOL;