1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Should I execute mysql queries in an async task?

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

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Should I execute mysql queries in an async task? Will it affect anything performance wise?
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yes you should execute MySQL queries asynchronously. There might be a few exceptions though.
    The time it takes to execute a query is usually just a very small fraction of a second, so it's fine if you're executing queries synchronously on startup.
    I know of some servers that run MySQL queries synchronously and still maintain a good TPS because the MySQL server is hosted on the same system that runs PMMP. Maybe you too could run queries synchronously if you fall in the same category, but any slow queries will get you!

    Async tasks for each query might solve this issue but by using async tasks, you might worsen the overall execution time. You'll have to create a new MySQL connection for each async task besides executing the query(ies). You can solve the issue of recreating mysql connections by creating a Thread dedicated to handle your queries. This might be somewhat tricky but luckily there's a virion which does just that: https://github.com/poggit/libasynql.
     
    HimbeersaftLP and SleepSpace9 like this.
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    With this library, I can just query right on the spot?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yes, but if you want to fetch the query result, you need to specify a callback which should get called after the query has executed.
     
  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.