note 48934 added to function.mysql-list-tables

From: Date: Wed, 12 Jan 2005 10:47:17 +0000
Subject: note 48934 added to function.mysql-list-tables
Groups: php.notes 
Request: Send a blank email to php-notes+get-83134@lists.php.net to get a copy of this message
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 ! ---- Manual Page -- http://www.php.net/manual/en/function.mysql-list-tables.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48934 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48934&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48934&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83134) next »