If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

SQL Queries and VPS Overload with 50 Active Visitors

Started by AaronJacobson, Aug 23, 2023, 12:06 AM

Previous topic - Next topic

AaronJacobsonTopic starter

The website used to be hosted on a regular server and everything was working fine. The response time was never more than a second. However, due to an increase in traffic, it was decided to switch to a VPS.

But then things started going wrong. With just 3 requests per second and 50 active users, the response time of the site skyrocketed to over 30 seconds! I contacted the hosting support, and they informed me that the problem lies in the database queries (mysql) consuming 97% of the server resources, and that the issue is with optimizing the VPS itself. They claim that their hosting servers are already highly optimized, which is why this problem didn't occur there.

Now I'm trying to figure out how to optimize the VPS in order to handle at least 20 users. It's amusing and frustrating at the same time.

Here's an example of the queries executed by the site:

$res = mysql_query("SELECT * FROM content WHERE category = 'new' AND title LIKE '%".mysql_real_escape_string($search)."%' GROUP BY thumb ORDER BY id DESC LIMIT $p, $limit");

These queries are straightforward and not particularly frequent, even with 50 users.

I would appreciate any guidance on where to look and which steps to take. Perhaps there are some well-known pitfalls that I have stumbled upon now.

If you need any further information, please let me know.

Thank you in advance!
  •  


Lettutfetwoni

Here are a few suggestions to optimize your VPS for better performance:

1. Analyze the Queries: Start by examining the database queries in your code to identify any potential bottlenecks or inefficiencies. You can use MySQL's EXPLAIN statement to analyze the query execution plan and identify areas that can be optimized.

2. Indexing: Ensure that the necessary indexes are added to the relevant columns used in your queries. Indexing can significantly improve query performance by allowing the database to retrieve data more efficiently.

3. Database Caching: Implementing a caching layer can help reduce the load on your database. Consider using tools like Memcached or Redis to cache frequently accessed data and avoid unnecessary database queries.

4. Query Optimization: Review the structure of your queries and consider whether they can be rewritten to provide better performance. For example, evaluate whether there are any unnecessary joins or redundant conditions in your WHERE clauses.

5. Connection Pooling: Implement connection pooling to manage your database connections effectively. This can help minimize the overhead of establishing new connections for each user request.

6. Resource Allocation: Make sure that your VPS is allocated enough resources to handle the increased traffic. Check if the CPU, memory, and disk I/O limits are properly configured based on your requirements.

7. Web Server Configuration: Optimize your web server's configuration to handle the increased traffic efficiently. For example, tweak the number of concurrent connections, enable compression, and leverage caching mechanisms.

8. Load Testing and Monitoring: Conduct load testing to simulate high-traffic scenarios and identify the breaking points of your VPS. Monitor the system's resource usage during these tests to pinpoint specific areas that need improvement.

9. Database Configuration: Review and fine-tune your MySQL server configuration to ensure optimal performance. This includes adjusting parameters such as cache sizes, buffer sizes, and query cache settings based on your VPS resources and workload.

10. Query Caching: Enable query caching in MySQL to cache commonly executed queries and their results. This can help reduce the load on the database server and improve response times for frequently requested data.

11. Database Normalization: Ensure your database is properly normalized by eliminating redundant data and organizing it into logical tables. Normalization can improve query efficiency and reduce storage requirements.

12. Database Sharding or Replication: If your website experiences high traffic and the database becomes a bottleneck, consider implementing database sharding or replication. Sharding distributes the data across multiple databases, while replication creates multiple copies of the database for increased read scalability.

13. Scaling: If optimizing your VPS doesn't provide sufficient performance improvements, consider scaling horizontally by adding more servers to your infrastructure. Load balancing techniques can distribute the traffic across multiple VPS instances to handle increased user load.

14. Profiling and Monitoring: Use profiling tools and monitoring solutions to identify performance bottlenecks in your application code and database queries. These tools can help pinpoint specific areas that need optimization and provide insights into resource usage patterns.

15. Code Optimization: Review your application code and look for opportunities to optimize it. This may involve optimizing loops, minimizing unnecessary computations, using efficient data structures, and improving algorithmic efficiency.

16. Security Considerations: Ensure that your VPS and application are adequately secured to prevent any potential attacks or exploits that could impact performance. Implement proper security measures such as firewalls, secure coding practices, and regular security audits.
  •  

heenamajeed

1. Regular hosting offers greater strength compared to many VPS options, as it typically provides nearly the full power of the physical server at the time of the request. Hosters rarely impose restrictions in this regard.

2. Although your query may seem simple, it lacks the crucial information regarding the number of data entries in this table. Is it around 100 or a staggering 10,000,000?

3. Do you only have one table? Why are you specifically concerned about this particular query? Additionally, please provide details on the amount of data stored in the database and its structure, including any indexes.

4. In the past, there were instances where VPS performance would slow down until the system was transferred to a different piece of hardware. This was mainly due to unfavorable neighbors who heavily burdened the hard drive.
  •  

weeaysmwy

The fact that VPS performs worse than "regular hosting" suggests that there might be an issue with VPS. Firstly, consider the type of MySQL you are using. Give MariaDB a try and optimize its settings. Aim to allocate minimal RAM to it (look up "mysql low memory" for an example like my-small.cnf). Are you using InnoDB or MyISAM as the storage engine? If not Inno, experiment with it, or disable it if irrelevant.

Check if general_log is enabled in MySQL, which logs all DBMS requests. Turning it off can relieve strain on the machine, especially if IO speed is slow!

Additionally, consider implementing a caching mechanism using software to reduce reliance on the DBMS for every request.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...