<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="313745" data-post-id="313745">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sodapopcan" data-post="10" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>Contrary to popular belief, running a single query isn’t guaranteed to be more efficient than running multiple. Not only is this true of SQL its especially true in Ecto since a single query will return a single, denormalized table which must be reduced in Elixir.</p>
</blockquote>
</aside>
<p>This is true in the broader context.  When considering just the database, the naïve rule of thumb is to ask your question once if you can: ideally, this means a single query which gives the database query planner the most information it can act on to most efficiently retrieve the information you want (so, from the Ecto perspective, do JOINs as needed).  But as you point out, more broadly, this isn’t always the most efficient. from the application point of view.</p>
<p>The bigger trade-off we’re dealing with, end-to-end, is really between bandwidth and latency.  A JOIN query, like a common <code>header</code>/<code>detail</code> query that includes both header data and detail data, will duplicate the header data coming from the database… and in application we only need the header data once.  So our bandwidth gets consumed transferring duplicated data which we’re just going to discard at the application.  BUT… when we’re doing a <code>preload</code>, without JOINs, we’re producing multiple queries in the application: this means multiple round trips from the application to the database, the database often is increasing its planning time because it’s initializing and planning multiple queries (PostgreSQL anyway), and we may lose the benefits of the query planner being able to more efficiently retrieve the complete dataset.  These issues all contribute to increased latency in getting results you can use.</p>
<p>Ultimately there is an inflection point where the latency penalties from running multiple queries become smaller than the time it takes to transfer extraneous data from a single JOINed query.  Where that point sits depends heavily on the exact query and the specific environmental conditions (mostly network, but DB server capability as well) where the application is running.  I don’t have a great one-size-fits-all rule of thumb beyond just saying understand the principals just discussed (and maybe a little more), start with the method that feels most comfortable to code in the application, and address performance issues case by case when they make themselves known.  For myself, I’ll have hand-wavy, intuition for where the breakpoint is and code accordingly; I expect I’m only marginally better than the just do it one way and deal with issues approach.  If you’re going to err, err on the side of preloading since I’d expect the latencies to be small over more kinds of queries.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313745" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-313745" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313745"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="313747" data-post-id="313747">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sbuttgereit" data-post="12" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/48/30162_2.png" class="avatar"> sbuttgereit:</div>
<blockquote>
<p>I don’t have a great one-size-fits-all rule of thumb</p>
</blockquote>
</aside>
<p>Ya, that’s the crux of it.  You have to benchmark.  Although I mostly don’t worry about it I’m just doing a couple of queries in a page.</p>
<p>In a particularly egregious case at my old job, there was a very hairy query with I think 9 joins.  Converting all of those into separate queries took the total time down from over 30 seconds to under 1 second.  Now, most of that 30 seconds was on network and Elixir, not in the db.  But the db was returning a massive denormalize table which had to be sent over the network then Elixir had to load into memory and reduce.</p>
<p>But ya again, there is no rule of thumb.  To dig into the case of Posts+Replies more, if you are going to be paginating replies in a LiveView, then it’s wasteful to preload them.  If your only way of loading replies is through <code>get_post</code> this means you have to refetch the post each time you fetch a new page of replies.  Not to mention you’ve now complicated your getter even further by having to add a <code>:replies_page</code> option.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313747" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-313747" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313747"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="313748" data-post-id="313748">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="adw632" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  adw632
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It should be possible with Postgres json functions to  return data in a nested shape rather than fanned out with data repitition. Functions like json_build_object(), json_agg() and row_to_json() stand out.</p>
<p>I haven’t looked into it, but may be something to explore and I just googled and found a <a href="https://johnatten.com/2015/04/22/use-postgres-json-type-and-aggregate-functions-to-map-relational-data-to-json/" rel="noopener nofollow ugc">2015 post that does this for avoiding 1+N queries.</a></p>
<p>Postgres documentation here:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://www.postgresql.org/docs/current/functions-json.html">
  <header class="source">
      <img src="https://www.postgresql.org/favicon.ico" class="site-icon" alt="" width="48" height="48">

      <a href="https://www.postgresql.org/docs/current/functions-json.html" target="_blank" rel="noopener nofollow ugc" title="01:09PM - 14 May 2026">PostgreSQL Documentation – 14 May 26</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:540/557;"><img src="https://www.postgresql.org/media/img/about/press/elephant.png" class="thumbnail" alt="" width="540" height="557"></div>

<h3><a href="https://www.postgresql.org/docs/current/functions-json.html" target="_blank" rel="noopener nofollow ugc">9.16.&nbsp;JSON Functions and Operators</a></h3>

  <p>9.16.&nbsp;JSON Functions and Operators # 9.16.1. Processing and Creating JSON Data 9.16.2. The SQL/JSON Path Language 9.16.3. SQL/JSON Query Functions …</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313748" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-313748" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313748"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="313749" data-post-id="313749">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="D4no0" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/D4no0/120/33624_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  D4no0
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adw632" data-post="14" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>It should be possible with Postgres json functions to return data in a nested shape rather than fanned out with data repitition. Functions like json_build_object(), json_agg() and row_to_json() stand out.</p>
</blockquote>
</aside>
<p>It is, however you are also shifting a part of your functionality and business-logic to the database. Improvement, debugging and overall knowledge required to understand what is going on becomes higher.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313749" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-313749" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313749"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="313754" data-post-id="313754">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adw632" data-post="14" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>It should be possible with Postgres json functions to return data in a nested shape rather than fanned out with data repitition. Functions like json_build_object(), json_agg() and row_to_json() stand out.</p>
</blockquote>
</aside>
<p>It very much is possible and I’ve used it with success.  About 10 years ago, we wanted to take structured data pretty much directly from the database and send it to the web client to render; the data was quite complex.  So we had an admittedly overcomplicated query which would build this single, large JSON object when the source data changed (not too frequently) and cached it in a column (there was other memory caching as well, but the database was done at that point).  Overall it worked well.  And over time it worked better as this was when the JSON handling features were just starting to be added.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313754" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-313754" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313754"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="313767" data-post-id="313767">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re not wrong but from one scale and on it is very much not only worth it but also required. I’ve had a CTO step in to measure 5 different ways of slicing data from the SQL database and finally choose the one that was indeed closest to the DB because we were suffering loss of customers due to pages needing 5+ seconds to load. He made sure to thoroughly document his SQL + Ecto acrobatics, to the point the comments were 4x bigger than the query itself but IMO it was very worth it; our response times dropped to 0.6s - 0.9s only because of that (and we then reduced them further with other optimizations).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313767" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-313767" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313767"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="313805" data-post-id="313805">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="adw632" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  adw632
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="D4no0" data-post="15" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>It is, however you are also shifting a part of your functionality and business-logic to the database. Improvement, debugging and overall knowledge required to understand what is going on becomes higher.</p>
</blockquote>
</aside>
<p>Pushing data query concerns to the DB layer is almost always the best bet. I prefer systems that use stored procedures and views to simplify the app, over ones where the app cobbles the query together. Using stored procedures is also far more secure and not susceptible to SQL injection attacks.</p>
<p>In this instance I would argue that representing query response as json is nothinh to do with business logic, it is about efficient data transfer as there is no business domain logic involved here, any time you have a join you represent the result as json, any right side row is represented as json and aggregated.  We are dealing with on the wire data representation, (not business domain logic) just like Phoenix does with Liveview.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313805" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-313805" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313805"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="313833" data-post-id="313833">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="D4no0" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/D4no0/120/33624_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  D4no0
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adw632" data-post="18" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>I prefer systems that use stored procedures and views to simplify the app, over ones where the app cobbles the query together.</p>
</blockquote>
</aside>
<p>I’ve heard about this, even though I think that in some way this is an anti-pattern, as you will slowly start moving more and more logic to stored procedures. When performance is critical and there is no other way, I totally understand that. You are also limited in development and debugging tools compared to a full-fledged language (at least this is what it felt to me), not to mention that there is specific knowledge involved about potential dangers and limitations.</p>
<aside class="quote no-group" data-username="adw632" data-post="18" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>Using stored procedures is also far more secure and not susceptible to SQL injection attacks.</p>
</blockquote>
</aside>
<p>Correct me if I’m wrong, however in a modern library like Ecto, how you would do a SQL injection? IMO injections are just a sign of bad software design, where data is not clearly separated from execution.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313833" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-313833" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313833"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="313835" data-post-id="313835">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adw632" data-post="18" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>I prefer systems that use stored procedures and views to simplify the app, over ones where the app cobbles the query together.</p>
</blockquote>
</aside>
<p>Nah, I wouldn’t go so far. It’s not sustainable to have one SQL wizard per team, and they become a bottleneck too (and often command insane salaries).</p>
<p>Elixir is not amazing when it comes to having to chew through thousands of records per second coming from the database, but it’s also doing much better compared to every other dynamic language, except JS, which is in the same or slightly better ballpark of performance.</p>
<p>Throw in the mix languages like Golang and Rust – f.ex. for microservices – and processing of stuff coming from the DB is almost a non-issue.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313835" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-313835" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313835"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="313839" data-post-id="313839">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="adw632" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  adw632
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="D4no0" data-post="19" data-topic="60838">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>Ecto, how you would do a SQL injection? IMO injections are just a sign of bad software design, where data is not clearly separated from execution.</p>
</blockquote>
</aside>
<p>Ecto uses parameterized queries so is very safe unless one is careless using fragments.</p>
<p>In mixed environments this is still an issue, and I tend to be conservative and think in multiple layers of defense when it comes to these things.  It is my pessimism from 30+ years of working in infosec and cyber security.</p>
<p>Where I typically focus use of stored procedures for specific use cases, which are typically complex updates and complex queries such as RCTE’s, or where it is an important thing to get right, where many tables may be involved but I want a logical action or where I want to insulate the application from complex schema or db schema churn.</p>
<p>For example I use this <a href="https://www.alibabacloud.com/blog/postgresql-graph-search-practices---10-billion-scale-graph-with-millisecond-response_595039" rel="noopener nofollow ugc">graph querying in Postgres approach</a> which makes you wonder why things like Neo4j ever had a reason to exist. But I don’t want application layer solving the graph querying logic so it makes sense to be in the database.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313839" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-best-to-write-context-functions-for-more-complex-cases/60838/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-313839" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313839"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/60838/load_more?page=3">Load more posts (2 remaining)</a>
</div></template></turbo-stream>