FXServer & MySQL Tuning: Optimizing Queries for 100+ Active Players
# FXServer & MySQL Tuning: Optimizing Queries for 100+ Active Players
When a FiveM server lags, administrators immediately blame the scripts or host CPU. However, the true culprit is often a bottlenecked **MySQL database**. As players connect, load inventories, buy vehicles, and trigger character saves, database read/write requests escalate rapidly. If queries aren't structured efficiently, they stall the main thread, resulting in client desync and overall server lag. ## 1. Implement Proper Database Indexing
Without indexes, MySQL must perform a full-table scan on every search. If your `players` table has 10,000 character rows, querying character data by `citizenid` without an index will freeze queries.
-- Add an index to citizenid if it isn't already set
ALTER TABLE `players` ADD INDEX `idx_citizenid` (`citizenid`);
## 2. Eliminate Recursive Querying inside Tick LoopsNever run SQL queries inside Lua loops, especially threads that run every few seconds. - **Cache Data in Memory**: Fetch database data once when the player joins. Modify it in memory (Lua variables) and save it back to the database in batch intervals (e.g., every 5-10 minutes) or only when the player disconnects. Optimize your server database architecture today. Consult our developers at [Code X Art Contact](/contact).
