<span style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">
<blockquote>
<p dir="auto">Did you try doing this via normal ActiveModel queries before resorting to raw SQL queries?</p>
<p dir="auto">I certainly don't like the idea of interpolating arguments into the queries - the fact that you called the argument <code class="notranslate">quoted_user_id</code> immediately hints at the risks of doing so. Is it not possible to use bound parameters with these queries?</p>
<p dir="auto">What analysis have you done of the execution plan(s) and the likely performance? Is everything using indexes or are there things which are doing table scans?</p>
</blockquote>
<p dir="auto">Thank you for the feedback Tom. I've completely refactored the code to address your concerns:</p>
<ol dir="auto">
<li>
<p dir="auto"><strong>ActiveRecord Usage</strong>: Now using ActiveRecord for all activity fetching. Only kept raw SQL for the <code class="notranslate">activity_days</code> method since the UNION ALL across multiple tables is complex to express in pure ActiveRecord.</p>
</li>
<li>
<p dir="auto"><strong>SQL Injection Protection</strong>: Eliminated all string interpolation and replaced <code class="notranslate">quoted_user_id</code> with proper parameter binding:</p>
<div class="highlight highlight-source-ruby" dir="auto"><pre class="notranslate"><span class="pl-v">ActiveRecord</span>::<span class="pl-v">Base</span><span class="pl-kos">.</span><span class="pl-en">sanitize_sql</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-en">sql</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-pds">:user_id</span> <span class="pl-c1">=></span> <span class="pl-en">user_id</span><span class="pl-kos">,</span> <span class="pl-pds">:limit</span> <span class="pl-c1">=></span> <span class="pl-en">limit</span><span class="pl-kos">,</span> <span class="pl-pds">:offset</span> <span class="pl-c1">=></span> <span class="pl-en">offset</span> <span class="pl-kos">}</span><span class="pl-kos">]</span><span class="pl-kos">)</span></pre></div>
</li>
<li>
<p dir="auto"><strong>Performance Analysis</strong>: Conducted thorough testing with multiple users:</p>
<ul dir="auto">
<li>Execution time: 9-14ms for users with ~25 activity days</li>
<li>Query planning: ~0.15-0.20ms, execution: ~0.15-0.19ms</li>
<li>All tables have appropriate indexes except <code class="notranslate">gpx_files</code> which has <code class="notranslate">[user_id,id]</code> index as agreed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2892924564" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/5747" data-hovercard-type="pull_request" data-hovercard-url="/openstreetmap/openstreetmap-website/pull/5747/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/pull/5747">#5747</a></li>
</ul>
</li>
</ol>
<p dir="auto">This was just the initial implementation. Let me know if further adjustments are needed. Thank you.</p><p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/5761#issuecomment-2719470105">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLP2R4T7EOYXDE6NMRD2UDJSPAVCNFSM6AAAAABYQQXBLGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJZGQ3TAMJQGU">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLNMDYFYNK2T6ODGLXD2UDJSPA5CNFSM6AAAAABYQQXBLGWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTVCC7JBS.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/5761/c2719470105</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
</span>
<div style="display: flex; flex-wrap: wrap; white-space: pre-wrap; align-items: center; "><img alt="kcne" height="20" width="20" style="border-radius:50%; margin-right: 4px;" decoding="async" src="https://avatars.githubusercontent.com/u/76796906?s=20&v=4" /><strong>kcne</strong> left a comment <a href="https://github.com/openstreetmap/openstreetmap-website/pull/5761#issuecomment-2719470105">(openstreetmap/openstreetmap-website#5761)</a></div>
<blockquote>
<p dir="auto">Did you try doing this via normal ActiveModel queries before resorting to raw SQL queries?</p>
<p dir="auto">I certainly don't like the idea of interpolating arguments into the queries - the fact that you called the argument <code class="notranslate">quoted_user_id</code> immediately hints at the risks of doing so. Is it not possible to use bound parameters with these queries?</p>
<p dir="auto">What analysis have you done of the execution plan(s) and the likely performance? Is everything using indexes or are there things which are doing table scans?</p>
</blockquote>
<p dir="auto">Thank you for the feedback Tom. I've completely refactored the code to address your concerns:</p>
<ol dir="auto">
<li>
<p dir="auto"><strong>ActiveRecord Usage</strong>: Now using ActiveRecord for all activity fetching. Only kept raw SQL for the <code class="notranslate">activity_days</code> method since the UNION ALL across multiple tables is complex to express in pure ActiveRecord.</p>
</li>
<li>
<p dir="auto"><strong>SQL Injection Protection</strong>: Eliminated all string interpolation and replaced <code class="notranslate">quoted_user_id</code> with proper parameter binding:</p>
<div class="highlight highlight-source-ruby" dir="auto"><pre class="notranslate"><span class="pl-v">ActiveRecord</span>::<span class="pl-v">Base</span><span class="pl-kos">.</span><span class="pl-en">sanitize_sql</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-en">sql</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-pds">:user_id</span> <span class="pl-c1">=></span> <span class="pl-en">user_id</span><span class="pl-kos">,</span> <span class="pl-pds">:limit</span> <span class="pl-c1">=></span> <span class="pl-en">limit</span><span class="pl-kos">,</span> <span class="pl-pds">:offset</span> <span class="pl-c1">=></span> <span class="pl-en">offset</span> <span class="pl-kos">}</span><span class="pl-kos">]</span><span class="pl-kos">)</span></pre></div>
</li>
<li>
<p dir="auto"><strong>Performance Analysis</strong>: Conducted thorough testing with multiple users:</p>
<ul dir="auto">
<li>Execution time: 9-14ms for users with ~25 activity days</li>
<li>Query planning: ~0.15-0.20ms, execution: ~0.15-0.19ms</li>
<li>All tables have appropriate indexes except <code class="notranslate">gpx_files</code> which has <code class="notranslate">[user_id,id]</code> index as agreed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2892924564" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/5747" data-hovercard-type="pull_request" data-hovercard-url="/openstreetmap/openstreetmap-website/pull/5747/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/pull/5747">#5747</a></li>
</ul>
</li>
</ol>
<p dir="auto">This was just the initial implementation. Let me know if further adjustments are needed. Thank you.</p>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/5761#issuecomment-2719470105">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLP2R4T7EOYXDE6NMRD2UDJSPAVCNFSM6AAAAABYQQXBLGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJZGQ3TAMJQGU">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLNMDYFYNK2T6ODGLXD2UDJSPA5CNFSM6AAAAABYQQXBLGWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTVCC7JBS.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/5761/c2719470105</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/pull/5761#issuecomment-2719470105",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/5761#issuecomment-2719470105",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>