title: Very heavy wordpress plugin and customization
body: <p>i have a wordpress website and with customization (cache/minify/database), is really quick in frontend, and in backend, with one exception. I use a plugin where it stores and retrieves players stats, carreers, teams etc.
  When saving the team's match player's statistics, it needs 5 minutes to show the page reloaded with the data.
  Also when retrieving for example team's season data, it also needs a lot of time to show them.
  From what i ve seen, it is related with two of my files that do all the work. tournament_match.php, and functions.php. From hosting they said that it gets all tha database and thats why it is so slow
  Here is the tournament_match file</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
   global $wpdb;
   global $msg;
   $page = $_GET['page'];
   $tournament_id = $_GET['tid'];
   $match_id = $_GET['mid'];
   $league_type = leagueengine_fetch_data_from_id($tournament_id,'league_type');
  
   if(isset($_POST['home_team_bonus']) or isset($_POST['away_team_bonus'])) {
    if(isset($_POST['save_tournament_match'])) { leagueengine_save_tournament_match($tournament_id,$match_id,$_POST['date_alt'],$_POST['time_alt'],$_POST['home_team_id'],$_POST['away_team_id'],$_POST['home_team_score'],$_POST['away_team_score'],$_POST['home_team_bonus'],$_POST['away_team_bonus']); } 
   } else {
    if(isset($_POST['save_tournament_match'])) { leagueengine_save_tournament_match($tournament_id,$match_id,$_POST['date_alt'],$_POST['time_alt'],$_POST['home_team_id'],$_POST['away_team_id'],$_POST['home_team_score'],$_POST['away_team_score']); }
   }
   
   if(isset($_POST['save_attributes'])) { leagueengine_save_attribute_values('tournament_match',NULL,NULL,$match_id,NULL,NULL,$tournament_id); }
  
   if(isset($_POST['add_event_to_match'])) { error_reporting(0); leagueengine_add_event_to_tournament_match($tournament_id,$match_id,$_POST['new_event_id'],$_POST['new_event_time'],$_POST['timeline_text'],$_POST['new_event_count'],$_POST['new_event_player_id']); error_reporting(1); }
   if(isset($_POST['save_events'])) { leagueengine_save_event_times('tournament_match',$_POST['event_time_id'],$_POST['event_time'],$_POST['event_text']); }
   if(isset($_POST['add_home_event_to_match'])){
       error_reporting(0);
       foreach($_POST as $key=&gt;$val){
           $val=intval($val);
             if(is_int($val)&amp;&amp;$val&gt;0){
          $data=explode('-',$key);
           
          $playerid=$data[1];
          $eventid=$data[2];
          leagueengine_add_event_to_tournament_match2($tournament_id,$match_id,$eventid,$_POST['new_event_time'],$_POST['timeline_text'],$val,$playerid);
    
          }
       }
      error_reporting(1); 
   }
   
   
   if(isset($_POST['add_away_event_to_match'])){
       error_reporting(0);
       foreach($_POST as $key=&gt;$val){
           $val=intval($val);
             if(is_int($val)&amp;&amp;$val&gt;0){
          $data=explode('-',$key);
           
          $playerid=$data[1];
          $eventid=$data[2];
          leagueengine_add_event_to_tournament_match2($tournament_id,$match_id,$eventid,$_POST['new_event_time'],$_POST['timeline_text'],$val,$playerid);
    
          }
       }
      error_reporting(1); 
   }
   
   if(isset($_POST['save_tournament_match_lineups'])) {
    if(isset($_POST['homeplayers'])) { $homeplayers = $_POST['homeplayers']; } else { $homeplayers = ''; }
    if(isset($_POST['awayplayers'])) { $awayplayers = $_POST['awayplayers']; } else { $awayplayers = ''; }
    if(isset($_POST['homesubs'])) { $homesubs = $_POST['homesubs']; } else { $homesubs = ''; }
    if(isset($_POST['awaysubs'])) { $awaysubs = $_POST['awaysubs']; } else { $awaysubs = ''; }
    leagueengine_save_tournament_match_lineups($tournament_id,$match_id,$homeplayers,$awayplayers,$homesubs,$awaysubs);
   }
   if(isset($_POST['delete_events'])) { leagueengine_delete_data('tournament_match_event',$_POST['delete_id'],'tournament',NULL,NULL,$tournament_id,$match_id); }
   
   if(isset($_POST['save_match_statistics'])) { leagueengine_save_tournament_match_statistics($tournament_id,$match_id,$_POST['tournament_match_statistic'],$_POST['home_value'],$_POST['away_value'],$_POST['att_type']); }
  
   if(isset($_POST['save_tournament_match_preview'])) { leagueengine_save_tournament_match_preview($tournament_id,$match_id,stripslashes_deep($_POST['match_preview'])); }
   if(isset($_POST['save_tournament_match_report'])) { leagueengine_save_tournament_match_report($tournament_id,$match_id,stripslashes_deep($_POST['match_report'])); }
  
   if(isset($_POST['tournament_match_swap'])) { leagueengine_tournament_match_swap($tournament_id,$match_id); }
  
   $table = $wpdb-&gt;prefix . 'leagueengine_tournament_matches';
   $match = $wpdb-&gt;get_row("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND id = '$match_id'");
   $home_team_id = $match-&gt;home_team_id;
   $away_team_id = $match-&gt;away_team_id;
   $tournament = leagueengine_fetch_data_row('tournament',$tournament_id);
   $table2 = $wpdb-&gt;prefix . 'leagueengine_tournaments';
   $tournament_row = $wpdb-&gt;get_row("SELECT * FROM $table2 WHERE data_id = '$tournament_id'");
  
  ?&gt;
  
  &lt;div id="leagueengine_admin" class="&lt;?php echo $page; ?&gt;"&gt;
  
   &lt;?php echo leagueengine_admin_header(); ?&gt;
  
   &lt;div id="leagueengine_admin_content"&gt;
    &lt;?php if($msg) { echo $msg; } ?&gt;
  
    &lt;ul class="breadcrumbs"&gt;
     &lt;li&gt;&lt;a href="admin.php?page=leagueengine_data&amp;data=competitions"&gt;&lt;?php _e('Competitions','leagueengine');?&gt;&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
     &lt;li&gt;&lt;a href="admin.php?page=leagueengine_tournament&amp;id=&lt;?php echo $tournament_id; ?&gt;"&gt;&lt;?php echo leagueengine_fetch_data_from_id($tournament_id,'data_value') ?&gt;&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
     &lt;?php if($match-&gt;round == 'GROUP') { ?&gt;
      &lt;li&gt;&lt;a href="admin.php?page=leagueengine_tournament_teams&amp;id=&lt;?php echo $tournament_id; ?&gt;"&gt;&lt;?php _e('Groups','leagueengine');?&gt;&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
     &lt;?php } else { ?&gt;
      &lt;li&gt;&lt;a href="admin.php?page=leagueengine_tournament_knockout&amp;id=&lt;?php echo $tournament_id; ?&gt;"&gt;&lt;?php _e('Knockout','leagueengine');?&gt;&lt;/a&gt; &lt;span class="divider"&gt;/&lt;/span&gt;&lt;/li&gt;
     &lt;?php } ?&gt;
     &lt;li&gt;&lt;?php _e('Match','leagueengine');?&gt;&lt;/li&gt;
    &lt;/ul&gt;
    
    &lt;?php
     if($league_type == 'players') {
      $home_emblem = leagueengine_fetch_player_emblem($match-&gt;home_team_id,20);
      $away_emblem = leagueengine_fetch_player_emblem($match-&gt;away_team_id,20,'right');
     } else {
      $home_emblem = leagueengine_fetch_team_emblem($match-&gt;home_team_id,20);
      $away_emblem = leagueengine_fetch_team_emblem($match-&gt;away_team_id,20,'right');
     }
    ?&gt;
    
    &lt;div class="match_masthead"&gt;
    &lt;table&gt;
     &lt;tr&gt;
      &lt;td class="home_team" style="border-top: 5px solid &lt;?php echo leagueengine_fetch_team_colour($match-&gt;home_team_id,'primary');?&gt;; text-align:left;width:40%;"&gt;&lt;?php echo $home_emblem . leagueengine_fetch_data_from_id($match-&gt;home_team_id,'data_value');?&gt;&lt;/td&gt;
      &lt;td class="score" style="text-align:center;width:20%;"&gt;&lt;span&gt;&lt;?php echo $match-&gt;home_team_score;?&gt; &amp;dash; &lt;?php echo $match-&gt;away_team_score;?&gt;&lt;/span&gt;&lt;/td&gt;
      &lt;td class="away_team" style="border-top: 5px solid &lt;?php echo leagueengine_fetch_team_colour($match-&gt;away_team_id,'primary');?&gt;; text-align:right;width:40%;"&gt;&lt;?php echo leagueengine_fetch_data_from_id($match-&gt;away_team_id,'data_value') . $away_emblem;?&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td colspan="3" style="text-align:center;"&gt;&lt;?php echo date(leagueengine_fetch_settings('date_format_php'),strtotime($match-&gt;match_date)) . ' ' . date(leagueengine_fetch_settings('time_format_php'),strtotime($match-&gt;match_time)); ?&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td class="competition" colspan="3" style="text-align:center;"&gt;&lt;?php echo '&lt;a href="admin.php?page=leagueengine_tournament&amp;id='.$tournament_id.'"&gt;' . leagueengine_fetch_data_from_id($tournament_id,'data_value') . '&lt;/a&gt;'; ?&gt;&lt;/td&gt;
     &lt;/tr&gt;
     
     &lt;tr&gt;&lt;td colspan="100%" style="text-align:center;padding-bottom:20px;"&gt;&lt;?php echo leagueengine_link('tournament_match&amp;tid='.$tournament_id.'&amp;mid='.$match-&gt;id,__('Go To Match','leagueengine'),'','','button-primary'); ?&gt;&lt;/td&gt;&lt;/tr&gt;
     
    &lt;/table&gt;
    &lt;/div&gt;
  
    &lt;div id="leagueengine_tabs"&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;a href="#score"&gt;&lt;?php _e('Score','leagueengine');?&gt;&lt;/a&gt;&lt;/li&gt;
    
    
    &lt;?php
     $table = $wpdb-&gt;prefix . 'leagueengine_player_careers'; 
     $homeplayers = $wpdb-&gt;get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$home_team_id'");
     $awayplayers = $wpdb-&gt;get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$away_team_id'");
     if($homeplayers or $awayplayers &amp;&amp; $league_type != 'players') {
    ?&gt;
    &lt;li&gt;&lt;a href="#lineups"&gt;&lt;?php _e('Lineups','leagueengine');?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;?php } ?&gt;
    &lt;?php if(leagueengine_data_exists('event')) { echo '&lt;li&gt;&lt;a href="#events"&gt;' . __('Events','leagueengine') . '&lt;/a&gt;&lt;/li&gt;'; } ?&gt;
    &lt;?php if(leagueengine_statistics_exists('tournament_match')) { echo '&lt;li&gt;&lt;a href="#statistics"&gt;' . __('Statistics','leagueengine') . '&lt;/a&gt;&lt;/li&gt;'; } ?&gt;
    &lt;?php if(leagueengine_h2h_history($match-&gt;match_date,$home_team_id,$away_team_id)) { echo '&lt;li&gt;&lt;a href="#history"&gt;' . __('History','leagueengine') . '&lt;/a&gt;&lt;/li&gt;'; } ?&gt;
    &lt;li&gt;&lt;a href="#report"&gt;&lt;?php _e('Report','leagueengine');?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    
    &lt;div id="score"&gt;
  
     &lt;form action="" method="POST"&gt;
   
     &lt;table class="form"&gt;
      &lt;tr&gt;
       &lt;th style="width:20%;"&gt;&lt;?php _e('Date/Time','leagueengine');?&gt;&lt;/th&gt;
       &lt;th style="width:30%;text-align:center;"&gt;&lt;?php _e('Home','leagueengine');?&gt;&lt;/th&gt;
       &lt;th style="width:20%;text-align:center;"&gt;&lt;?php _e('Score','leagueengine');?&gt;&lt;/th&gt;
       &lt;th style="width:30%;text-align:center;"&gt;&lt;?php _e('Away','leagueengine');?&gt;&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr class="date"&gt;
       &lt;td&gt;&lt;input type="text" class="leagueengine_datepicker" name="match_date" value="&lt;?php echo date(leagueengine_fetch_settings('date_format_php'),strtotime($match-&gt;match_date));?&gt;"&gt;&lt;/td&gt;
       &lt;td colspan="3"&gt;&lt;/td&gt;
      &lt;/tr&gt;
         
         &lt;input type="hidden" name="tournament_match_id" value="&lt;?php echo $match-&gt;id;?&gt;"&gt;
         &lt;input type="hidden" name="home_team_id" value="&lt;?php echo $match-&gt;home_team_id;?&gt;"&gt;
         &lt;input type="hidden" name="away_team_id" value="&lt;?php echo $match-&gt;away_team_id;?&gt;"&gt;
         &lt;input type="hidden" name="date_alt" class="leagueengine_datepicker_alt" value="&lt;?php echo $match-&gt;match_date;?&gt;"&gt;
         &lt;input type="hidden" name="time_alt" class="leagueengine_timepicker_alt" value="&lt;?php echo $match-&gt;match_time;?&gt;"&gt;
         
         &lt;tr&gt;
         &lt;td&gt;&lt;input type="text" class="leagueengine_timepicker" name="match_time" value="&lt;?php echo date(leagueengine_fetch_settings('time_format_php'),strtotime($match-&gt;match_time));?&gt;"&gt;&lt;/td&gt;
         &lt;td style="text-align:center;"&gt;&lt;?php echo leagueengine_fetch_data_from_id($match-&gt;home_team_id,'data_value') ;?&gt;&lt;/td&gt;
         &lt;td style="text-align:center;"&gt;
          &lt;input style="width:48%;text-align:center;" type="text" name="home_team_score" value="&lt;?php echo $match-&gt;home_team_score;?&gt;"&gt;
          &lt;input style="width:48%;text-align:center;" type="text" name="away_team_score" value="&lt;?php echo $match-&gt;away_team_score;?&gt;"&gt; 
         &lt;/td&gt;      
         &lt;td style="text-align:center;"&gt;&lt;?php echo leagueengine_fetch_data_from_id($match-&gt;away_team_id,'data_value') ;?&gt;&lt;/td&gt;
         &lt;/tr&gt;
         
         &lt;?php if($tournament_row-&gt;pts_bonus == 'on') { ?&gt;
         
         &lt;tr&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td style="text-align:center;"&gt;&lt;?php _e('Bonus Points','leagueengine');?&gt;&lt;/td&gt;
         &lt;td style="text-align:center;"&gt;
          &lt;input style="width:48%;text-align:center;" type="text" name="home_team_bonus" value="&lt;?php echo $match-&gt;home_team_bonus;?&gt;"&gt;
          &lt;input style="width:48%;text-align:center;" type="text" name="away_team_bonus" value="&lt;?php echo $match-&gt;away_team_bonus;?&gt;"&gt; 
         &lt;/td&gt;      
         &lt;td style="text-align:center;"&gt;&lt;?php _e('Bonus Points','leagueengine');?&gt;&lt;/td&gt;
         &lt;/tr&gt;      
         
         &lt;?php } ?&gt;
         
     &lt;/table&gt;
     &lt;input style="margin-top:20px;" type="submit" name="save_tournament_match" class="button-primary" value="&lt;?php _e('Save','leagueengine');?&gt;" /&gt;
     &lt;input style="margin:20px 0 0 10px;" type="submit" name="tournament_match_swap" class="button" value="&lt;?php _e('Swap Teams','leagueengine');?&gt;" style="float:right;margin-right:10px;" /&gt;
     &lt;/form&gt;
  
    
    &lt;/div&gt;
    
    &lt;?php
     $table = $wpdb-&gt;prefix . 'leagueengine_player_careers'; 
     $homeplayers = $wpdb-&gt;get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$home_team_id'");
     $awayplayers = $wpdb-&gt;get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND  team_id = '$away_team_id'");
     if($homeplayers or $awayplayers &amp;&amp; $league_type != 'players') {
    ?&gt;
    &lt;div id="lineups"&gt;
     &lt;?php echo leagueengine_tournament_match_lineups($tournament_id,$match_id);?&gt;   
    &lt;/div&gt;
    &lt;?php } ?&gt;
    
    &lt;?php if(leagueengine_data_exists('event')) { ?&gt;
     &lt;div id="events"&gt;
      &lt;?php echo leagueengine_tournament_match_events($tournament_id,$match_id);?&gt;   
     &lt;/div&gt;
    &lt;?php } ?&gt; 
    
    &lt;?php if(leagueengine_statistics_exists('tournament_match')) { ?&gt;
    &lt;div id="statistics"&gt; 
     &lt;?php echo leagueengine_fetch_statistics('tournament_match',NULL,NULL,$tournament_id,$match_id);?&gt; 
    &lt;/div&gt;
    &lt;?php } ?&gt;
  
    &lt;?php if(leagueengine_h2h_history($match-&gt;match_date,$home_team_id,$away_team_id)) { ?&gt;
    &lt;div id="history"&gt; 
     &lt;?php echo leagueengine_fetch_h2h_history($match-&gt;match_date,$home_team_id,$away_team_id);?&gt; 
    &lt;/div&gt;
    &lt;?php } ?&gt;
    
    &lt;div id="report"&gt; 
     &lt;form action="" method="post"&gt;
      &lt;div class="setting"&gt;
      &lt;table class="form"&gt;
       &lt;tr&gt;&lt;th&gt;&lt;?php _e('Match Report','leagueengine');?&gt;&lt;/th&gt;&lt;/tr&gt;
      &lt;/table&gt;
      &lt;?php wp_editor( stripslashes_deep($match-&gt;report), 'match_report', array( 'media_buttons' =&gt; true, 'tinymce' =&gt; true, 'quicktags' =&gt; true, 'textarea_rows' =&gt; 20 )); ?&gt;
      &lt;/div&gt;
      &lt;input style="margin-top:20px;" type="submit" name="save_tournament_match_report" class="button-primary" value="&lt;?php _e('Save','leagueengine'); ?&gt;"&gt;
     &lt;/form&gt;
    &lt;/div&gt;
    
    &lt;/div&gt; 
  
    &lt;/div&gt;  
  
   &lt;/div&gt;
  
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>