Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Last active December 10, 2015 05:48
Show Gist options
  • Save toshimaru/4390402 to your computer and use it in GitHub Desktop.
Save toshimaru/4390402 to your computer and use it in GitHub Desktop.
compare select query (mysql).
these queries are same run time.
<?php
$con = mysql_connect('localhost', 'root', 'passwd');
if (!mysql_select_db('sample_db')) {
die('Could not select database: ' . mysql_error());
}
$sql = <<<EOF
select * from ptable as p
inner join adtable as ad on p.pid = ad.pid
inner join atable as a on a.aid = ad.aid
where a.aid =
EOF;
// $sql = <<<EOF
// select * from adtable as ad
// inner join atable as a on a.aid = ad.aid
// inner join ptable as p on p.pid = ad.pid
// where a.aid =
// EOF;
// $sql = <<<EOF
// select * from atable as a, adtable as ad , ptable as p
// where a.aid = ad.aid
// and p.pid = ad.pid
// and a.aid =
// EOF;
// $sql = <<<EOF
// select * from adtable as ad, atable as a
// where a.aid = ad.aid
// and a.aid =
// EOF;
$time_start = microtime(true);
foreach (range(1,10000) as $id) {
$_sql = $sql . $id;
$result = mysql_query($_sql, $con);
while ($row = mysql_fetch_array($result)) {}
}
$time = microtime(true) - $time_start;
echo "{$time} min.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment