<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Reubro Q&amp;A - Recent questions and answers in Php</title>
<link>https://q2a.reubro.com/index.php?qa=qa&amp;qa_1=php</link>
<description>Powered by Question2Answer</description>
<item>
<title>Answered: High Database Query Execution Time During Concurrent Tournament Load</title>
<link>https://q2a.reubro.com/index.php?qa=6&amp;qa_1=high-database-query-execution-during-concurrent-tournament&amp;show=10#a10</link>
<description>&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;A few things worth checking and trying, roughly in order of priority:&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;First, if you haven&#039;t already, turn on the MySQL slow query log (&lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;slow_query_log = 1&lt;/code&gt;, &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;long_query_time = 2&lt;/code&gt;) so you can see the exact queries rather than just symptoms. Once you&#039;ve got the actual queries, run &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;EXPLAIN&lt;/code&gt; on each one — you&#039;re looking for &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;type: ALL&lt;/code&gt; (means it&#039;s doing a full table scan), a high number of rows examined relative to what&#039;s actually needed, and &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;Using filesort&lt;/code&gt; or &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;Using temporary&lt;/code&gt; in the Extra column, which usually means your GROUP BY / ORDER BY isn&#039;t backed by an index.&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;My guess is indexing is going to be the biggest single win here. Tournament stats queries tend to filter and sort on the same columns every time (tournament_id, player_id, status, score), so if those aren&#039;t indexed — especially as composite indexes matching the actual query pattern, like &lt;code class=&quot;bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]&quot;&gt;(tournament_id, score DESC)&lt;/code&gt; for leaderboard sorts — MySQL is scanning way more rows than it needs to. Worth checking whether these tables have any indexes beyond the primary key at all.&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;While you&#039;re in there, it&#039;s also worth checking the storage engine. If any of these tables are still MyISAM instead of InnoDB, that alone could explain the concurrency slowdown you&#039;re describing — MyISAM locks the whole table on writes, so if leaderboard reads and score updates are hitting the same table at the same time during a busy tournament, everyone queues up behind each other. That fits the &quot;only slow when multiple tournaments are running concurrently&quot; pattern really well.&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;On the query side itself, a couple of common culprits: COUNT(DISTINCT ...) getting used unnecessarily after a join (often the join duplicates rows and a plain COUNT would double up, so people reach for DISTINCT when really the query structure is the issue), and per-row subqueries used to calculate rankings — those effectively turn into N extra queries per page load. If you&#039;re on MySQL 8, window functions like RANK() OVER (PARTITION BY tournament_id ORDER BY score DESC) can replace that in a single pass.&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;But honestly, the real fix for &quot;many people looking at leaderboards during concurrent tournaments&quot; is caching — recalculating the same leaderboard from scratch on every single page view is the actual scaling bottleneck, not just query efficiency. A few options that work on shared hosting:&lt;/p&gt;&lt;ul dir=&quot;ltr&quot; class=&quot;[&amp;amp;:not(:last-child)_ol]:pb-1 [&amp;amp;:not(:last-child)_ul]:pb-1 [li_&amp;amp;]:gap-1 [li_&amp;amp;]:mb-0 [li_&amp;amp;]:mt-1 flex flex-col gap-1 list-disc mb-3 pl-8 print:block print:space-y-1&quot;&gt;&lt;li class=&quot;font-claude-response-body whitespace-normal break-words pl-2&quot;&gt;Simplest: a small leaderboard cache table that gets updated whenever a score changes, and pages read from that directly instead of running the full join/aggregate every time.&lt;/li&gt;&lt;li class=&quot;font-claude-response-body whitespace-normal break-words pl-2&quot;&gt;If APCu is enabled on the hosting (common on cPanel), cache the computed leaderboard per tournament for even 15-30 seconds. That alone would collapse a huge number of repeated identical queries during peak viewing.&lt;/li&gt;&lt;li class=&quot;font-claude-response-body whitespace-normal break-words pl-2&quot;&gt;If APCu isn&#039;t available, a simple file-based cache with a timestamp check works almost as well.&lt;/li&gt;&lt;/ul&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;One more small thing: if a single page is running the same stats query more than once (pretty common in Core PHP apps that weren&#039;t built with a shared data layer), just storing the result in a PHP variable for that request avoids hitting the DB twice for the same data.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p class=&quot;font-claude-response-body break-words whitespace-normal&quot; dir=&quot;ltr&quot;&gt;If I had to guess where to start: confirm InnoDB, add the missing composite indexes based on what EXPLAIN shows, then drop in a leaderboard cache table or APCu layer. Between those three, I&#039;d expect the 12-20 second queries to drop to well under a second without needing any hosting/infra changes.&lt;br&gt;&lt;br&gt;&lt;em&gt;Note: I don&#039;t have experience in php, but i assessed this&amp;nbsp;issue with&amp;nbsp;multiple AIs and thought this to be sensible option worth trying.&lt;/em&gt;&lt;/p&gt;</description>
<category>Php</category>
<guid isPermaLink="true">https://q2a.reubro.com/index.php?qa=6&amp;qa_1=high-database-query-execution-during-concurrent-tournament&amp;show=10#a10</guid>
<pubDate>Wed, 05 Aug 2026 07:21:55 +0000</pubDate>
</item>
</channel>
</rss>