note 48934 deleted from function.mysql-list-tables by sp
| From: | sp@php.net | Date: | Sun, 29 Jan 2006 20:31:49 +0000 |
| Subject: | note 48934 deleted from function.mysql-list-tables by sp | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-102686@lists.php.net to get a copy of this message | ||
Note Submitter: tim at top-cat dot com
----
TABLE EXISTS: Performance test...
To test thebitman at attbi dot com's assertion that his method is the fastest way to check if a
table exists - I ran the following code, just changing the function name between runs to test the
speed of the two functions:
$t_start = array_sum(explode(' ', microtime()));
// Using STATUS method...
function mysql_table_exists($tablename) {
$res = mysql_query("SHOW TABLE STATUS LIKE '$tablename'") or
die(mysql_error());
return mysql_num_rows($res) == 1;
}
// Using A SELECT...
function mysql_table_exists2($table){
$exists = mysql_query("SELECT 1 FROM $table LIMIT 0");
if ($exists) return true;
return false;
}
for ($a=0; $a<1000; $a++) {
if (mysql_table_exists2('Products')) echo "Y";
else echo 'N';
}
$exectime = array_sum(explode(' ', microtime()))-$t_start;
echo "<br><p>Exec Time: $exectime</p>\r\n";
RESULTS:
1st method (STATUS) runtime
= 1.92s if the table exists
= 1.85s if table doesn't exist
2nd method (SELECT) runtime
= 0.16s if the table exists
= 0.32s if the table doesn't exist.
Ladies and gentlemen, we have a clear winner... Nice one thebitman !