title: Make query faster. Php Sql in Wordpress
body: <p>In my website, i store and fetch basketball player's stats. Now it uses the code below, but since there are 11 attribute stats for each player, it needs 132 queries for each team, so 264 queries hust for the teams. With debug i saw that in needs 0,5 sec, for each query that is to much time for my site.</p>
  
  <ol>
  <li>Do the records in the database affect the execution time? I had 2m records, but now that i have 1.m records, i do not see much of a difference.</li>
  <li><p>Is there any better way for this code to be written to speed up queries?</p>
  
  <p>$output .= '';
  $output.='';
  $output .= '';
          $output .= ''.__('Add Event','leagueengine').'';
          $output .= '';</p>
  
  <pre><code>$output .= '&lt;/tr&gt;';
  $output.="&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;";
  $output.="&lt;tr&gt;&lt;td colspan='2'&gt;".__('Home Team','leagueengine')."&lt;/td&gt;";
  
           $events = $wpdb-&gt;get_results("SELECT * FROM wp_leagueengine_data WHERE data_type = 'event' ORDER BY sort_order ASC, data_value ASC");
           //$events_count=0;
          foreach($events as $event) {
                  $output.="&lt;th&gt;&lt;center&gt;&lt;small&gt;$event-&gt;data_value&lt;/small&gt;&lt;/center&gt;&lt;/th&gt;"; 
                  // $events_count++;
          }
              $output.="&lt;/tr&gt;";   
          $t_p=count ($homeplayers2);
          if($t_p){
                  foreach($homeplayers2 as $homeplayer){
  
                      $output.="&lt;tr&gt;";
                          $output.="&lt;td colspan='2'&gt;".leagueengine_fetch_data_from_id($homeplayer-&gt;player_id,'data_value')."&lt;/td&gt;";
                           foreach($events as $event){
                               $evcount = $wpdb-&gt;get_results("SELECT count(*) as total_events FROM $table2 WHERE match_id='$mid' AND player_id='$homeplayer-&gt;player_id' AND event_id='$event-&gt;id'" );
                               $total= $evcount[0]-&gt;total_events;
  
                                  $output.="&lt;td&gt;&lt;input type='text' name=hm-$homeplayer-&gt;player_id-$event-&gt;id value='$total'/&gt;&lt;/td&gt;";
                           }
                      $output.="&lt;/tr&gt;";
                  }
          }
          else{ $output.="&lt;tr&gt;&lt;td colspan='12'&gt;No player attending this match&lt;/td&gt;&lt;/tr&gt;";}
  
  
  $output.="&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;";
  </code></pre>
  
  <p>$output.='';</p></li>
  </ol>