Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2. include ('include/php/sql.php');
  3.  
  4. //Generate DummyData.
  5.  
  6. //
  7. // CREATE TABLE showTable(id int(10), city varchar(25), `show` varchar(25));
  8. //
  9. if (1 == 2){ //change to generate data
  10.     $shows =  array("Show1", "Show2", "Show3", "Show4", "Show5", "Show6", "Show7", "Show8", "Show9");
  11.     $cities =  array("A","B","C","D","E","F","G","H","I");
  12.     $sql = new SQL();
  13.     $values = array();
  14.  
  15.     for ($i=1; $i<=250000; $i++){
  16.        $c = mt_rand(0,8);
  17.        $s = mt_rand(0,8);
  18.        
  19.        $values[] = "(".$i.", '".$cities[$c]."', '".$shows[$s]."')";
  20.        
  21.        if (count($values) < 250){
  22.            //do nothing
  23.        }else{
  24.            $query = "INSERT INTO showTable (id, city, `show`) VALUES ".implode(", ", $values);
  25.            $sql->query($query);
  26.            
  27.            $values=array();
  28.        }
  29.     }
  30. }
  31.  
  32. //Run each query 100 times, measuare average time.
  33.  
  34. $sql = new SQL();
  35. $dognose_times = array();
  36.  
  37. $runCount = 50;
  38.  
  39. for ($i = 0; $i<$runCount; $i++){
  40.     $start = microtime(true);
  41.     //dognose's version
  42.    
  43.     $sql->query("SELECT * FROM (
  44.                     SELECT city, `show`, count(id)  FROM showTable GROUP BY city,`show` ORDER BY count(id) DESC
  45.                     ) as temp GROUP BY city");
  46.  
  47.     $time_elapsed_secs = microtime(true) - $start;
  48.     $dognose_times[] = $time_elapsed_secs; 
  49. }
  50.  
  51. $sql = new SQL();
  52. $strawberry_times = array();
  53. for ($i = 0; $i<$runCount; $i++){
  54.     $start = microtime(true);
  55.     //strawberrys's version
  56.    
  57.     $sql->query(" SELECT x.*
  58.                       , z.total
  59.                    FROM showTable x
  60.                    JOIN
  61.                       ( SELECT city, MAX(id) max_id FROM showTable GROUP BY city ) y
  62.                      ON y.city = x.city
  63.                     AND y.max_id = id
  64.                    JOIN (SELECT city, `show`, COUNT(*) total FROM showTable GROUP BY city,`show`) z
  65.                      ON z.city = x.city
  66.                     AND z.`show` = x.`show`;");
  67.  
  68.     $time_elapsed_secs = microtime(true) - $start;
  69.     $strawberry_times[] = $time_elapsed_secs;  
  70. }
  71.  
  72. //Show average.
  73. echo "<hr>";
  74. echo "Average dognose Query: ".array_sum($dognose_times) / count($dognose_times)."<br />";
  75. echo "Average strawbe Query: ".array_sum($strawberry_times) / count($strawberry_times)."<br />";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement