Fetching data from a database

Discussion in 'Development' started by Levi, Feb 19, 2019.

  1. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    I use mysql database for my coins plugin.. and i query "SELECT..." every time a player run /coins...

    Is this a bad practice? What would be a better one?
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,800
    GitHub:
    sof3
    Player wouldn't run /coins too often, while MySQL queries are usually fast (less than 0.1s most of the time). What makes you think it's bad?
    However, if you have to check it really frequently (e.g. in PlayerMoveEvent), you may want to cache the result in memory for a minute or so.
    No matter you're storing in MySQL or in config files, you're still querying external resources, which is slow by definition. You should not abuse MySQL or config files as a temporary storage.
    To be precise, you should always store data in ram when they are used frequently (e.g. if the relevant players are online), and persist them using a database when the data are not used frequently.
    How does JSON make any difference? Stop posting irrelevant replies everywhere.
     
    Last edited: Mar 22, 2019 at 3:12 AM
    jasonwynn10 likes this.
  3. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    how do i store in ram?
     
  4. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,483
    GitHub:
    muqsit
    Cache the query result for as long as you think is a significant time.

    You could write your code in an efficient way to deal with the problem of frequent database calls. But again, it all points back to cache manipulation.
     
    jasonwynn10 likes this.
  5. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    Like caching into a json or txt file? Should I do this when the plugin enables?
     
  6. wolfdale

    wolfdale Zombie

    Messages:
    252
    You can query once and store it in a variable, update it when you change the value and save to db when player quits
     
    jasonwynn10 and Levi like this.
  7. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    a variable as in an array?
     
  8. wolfdale

    wolfdale Zombie

    Messages:
    252
    That's one way to do it, you can also create your own class and store it like sessions
     
    jasonwynn10 likes this.
  9. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    I see... how about adding data to a variable when a player joins?
     
  10. wolfdale

    wolfdale Zombie

    Messages:
    252
    You can load it when player join or only when you need it, either one works
     
    jasonwynn10 likes this.
  11. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    What if the server crash? Will it still save to db when players quit?
     
  12. wolfdale

    wolfdale Zombie

    Messages:
    252
    You can do that by saving all online players' data in onDisable and optionally do a check if server is running before saving if thats what you wanted
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,800
    GitHub:
    sof3
    If the server crashes really hard, onDisable might not be called.
     
  14. wolfdale

    wolfdale Zombie

    Messages:
    252
    Then i guess the only way would be to save whenever there is a modification? Is there a better way?
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,800
    GitHub:
    sof3
    I think saving every few minutes is sufficient. Crashing shouldn't happen often anyway.
     
  16. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    should i use uuid or player's name as key?
     
  17. radondev

    radondev Spider

    Messages:
    11
    GitHub:
    radondev
    Since names can be changed, I would choose the UUID or XUID because it is (usually) a unique identifier for a player.
     
  18. Levi

    Levi Zombie Pigman

    Messages:
    678
    GitHub:
    captainleviftw
    OKay so right now I save players data from MySQL into memory on PlayerLoginEvent. Something worries me though... what if multiple players join simultaneously? Will MySQL be able to keep up with querying, and not let a player join the server without their data saved in the memory?
     
  19. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,800
    GitHub:
    sof3
    If you are querying asynchronously, yes, there is the chance. You might want to check if the data are ready before doing anything on the player (and block the player from interacting if you are paranoid).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.