2017-08-12 41 views
-3

我的問題是,有更新troo在GET水平,使服務器開始落後於70%的TPS損失,我不知道如何優化?MySQL的滯後(SELECT)

public static int getCoins(Player player) { 
    try { 
     PreparedStatement q = Main.sql.prepareStatement("SELECT Coins FROM Players WHERE Players_UUID = ?"); 
     q.setString(1, player.getUniqueId().toString()); 

     int coins= 0; 
     ResultSet rs = q.executeQuery(); 

     while(rs.next()){ 
      balance = rs.getInt("Coins"); 
     } 

     q.close(); 

     return coins; 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return 0; 
} 
+0

我不明白的問題。 – Shadow

+0

您的Coins表中有多少個條目? – chrki

+0

你在Players_UUID列上有唯一索引嗎? –

回答

0

你不需要每次都讓硬幣做一個SQL查詢,只需要創建一個int或HashMap的字段中的數據,並檢查每個X的時間,如果數據已更改。

,如果它已經改變了發送更新MySQL服務器和標記數據拯救像兩個HashMap中/ int字段。

hashmap1 =數據庫中的數據 hashmap2 =服務器數據

如果hashmap1和hashmap2相同不更新。

不要在像schedulerAsync或線程異步功能的更新。

這裏一個類似的例子:

package test; 

import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Map.Entry; 
import java.util.UUID; 
import java.util.concurrent.ConcurrentHashMap; 

import org.bukkit.entity.Player; 
import org.bukkit.entity.Zombie; 
import org.bukkit.event.EventHandler; 
import org.bukkit.event.Listener; 
import org.bukkit.event.entity.EntityDeathEvent; 
import org.bukkit.plugin.java.JavaPlugin; 
import org.bukkit.scheduler.BukkitRunnable; 

import com.mysql.jdbc.Connection; 

public class Event extends JavaPlugin implements Listener { 

    // Mysql update data 
    public static ConcurrentHashMap<UUID, Integer> player_zombiekills = new ConcurrentHashMap<UUID, Integer>(); 

    // Mysql connection data 
    public static Connection connection; 
    private String host = "localhost"; 
    private String database = "DB"; 
    private String username = "user"; 
    private String password = "123"; 
    private int port = 3306; 

    //Kill zombie event 
    @EventHandler 
    public void zombieDeath(EntityDeathEvent eve) { 
     if (!(eve.getEntity() instanceof Zombie)) return; // Stop if death is not zombie 
     if (!(eve.getEntity().getKiller() instanceof Player)) return; // Stop if killer is not player 
     UUID uid = eve.getEntity().getKiller().getUniqueId(); // Save player uuid 
     if (player_zombiekills.contains(uid)) player_zombiekills.put(uid, 0); // Set kills 0 if not exist in hashmap 
     player_zombiekills.put(uid, player_zombiekills.get(uid) + 1); // plus 1 kills value 
    } 

    //On enable connection 
    public void onEnable() { 
     try { 
      if (connection != null && !connection.isClosed()) return; 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password); 
      if (!connection.isClosed()) System.out.println("[MySQL] " + "Connected to '" + database + "'"); 
     } catch (Exception e) { 
      System.err.println("[MySQL] " + e.getMessage()); 
     } 

     new BukkitRunnable() { 
      public void run() { 
       // for updates 
       for (Entry<UUID, Integer> ent : player_zombiekills.entrySet()) { 
        try { 
         // send update 
         connection.createStatement().executeUpdate("UPDATE `tablename` SET `zombiekills`=`zombiekills`+" + ent.getValue() + " WHERE `uuid`='" + ent.getKey().toString() + "'"); 
         // Sucess remove entry from hashmap to reset kills 
         player_zombiekills.remove(ent.getKey()); 
        } catch (SQLException e) { // If error 
         System.err.println("[MySQL] " + e.getMessage()); // Print error in console 
        } 
       } 
      } 
     }.runTaskTimerAsynchronously(this, 20, 20 * 60 * 10); // Run async loop every 10 minutes 
    } 
} 
-1

我不知道該怎麼辦,因爲如果你改變在BDD中的數據不會更改服務器上,所以如果我沒有得到每1秒鐘其不會工作讓我有一個異步但是如何優化沒有回報要獲得金幣...

+0

添加球員列表像「pending_get_list」,並在下一個異步任務做mysql選擇並保存數據在任何領域。 如果修改添加到另一個列表,如「pending_sync_list」,並在下一個異步任務做mysql更新。 – xXNurioXx

+0

可能舉個例子嗎? – Mael

+0

我已經發送了一個示例https://stackoverflow.com/a/45652166/7343009 – xXNurioXx