TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Do you use stored procedures for CRUD operations in your database?

8 点作者 aredirect超过 1 年前
I just stumbled on this today and apparently some people advocate to use stored procedures for select, insert, update, and delete operations with the argument it helps to reduce network traffic, increases the performance and doesn&#x27;t allow SQL injection?<p>What are your thoughts? and are you aware of any framework&#x2F;library using such approach?

10 条评论

austin-cheney超过 1 年前
About 80% of the business logic at my last job were in SQL stored procedures. This was a giant mistake because there was no automation in place for things like replication across tenants, static analysis, test automation, or anything at all.<p>It became too expensive to keep up with evolving business requirements and I was laid off as a result even though I was a late hire who did not plan any of that madness. Other people were laid off too but I am not sure how many because I was the first person on this small team they released.<p>My suggestion is to impose a tight separation of concerns and let data be just data. If you are planning architecture always remember that software is always a cost center, so always automate the shit out of it at all stages. Tools and frameworks will not save you from a lack of vision.
romanhn超过 1 年前
Worked at a company that used this pattern about 15 years ago. We had a homebuilt ORM that codegen&#x27;ed these stored procs. If I recall, stored procedures were faster than straight up SQL at one point, but that hasn&#x27;t been true for a while (same perf characteristics these days). Nowadays it feels like unnecessary overhead. I also like to keep code in the application layer, so try to avoid stored procs if I can. Any reasonable backend framework&#x2F;ORM should prevent SQL injection so I don&#x27;t find that to be a compelling argument either.
stephenr超过 1 年前
For selects I&#x27;d have though having views would be a better solution here.<p>But it sounds like a lot of overhead to maintain, presuming it&#x27;s one insert&#x2F;update&#x2F;delete proc per table.<p>IMO stored procedures help where you want to make some reasonably complex logic easier to call into from the application, OR where you want to trigger said reasonably complex logic from multiple DB events.
评论 #38664954 未加载
foobarbaz33超过 1 年前
&gt; doesn&#x27;t allow SQL injection<p>Here&#x27;s an injection vulnerable stored proc call.<p><pre><code> string badSql = &quot;exec fooProc &quot; + arg; </code></pre> User input need to be parameterized whether you are calling a stored proc or not.
icedchai超过 1 年前
I haven&#x27;t really seen the &quot;stored procedures for all DB access&quot; pattern in over 20 years. In the Oracle and Sybase days, it seemed common. In general, it feels like too much overhead during development. It&#x27;s another language you need to work with, another thing you have to update &#x2F; keep in sync, and it will complicate deploys.<p>I&#x27;d only use stored procs for specific niche uses cases. And only if it was really performance critical. If you want to avoid SQL injection, you use prepared statements (or a framework&#x2F;ORM that uses them.)
kgdiem超过 1 年前
I’m running an entire ETL pipeline with sprocs, functions and triggers. It works extremely well, has been running for several months without any downtime or interruption.<p>Snowflake introduced their marketplace and those apps &#x2F; data shares necessitate sprocs and database functions. I made a couple of those apps in my last role and versioning is definitely a challenge. Rolling back is easy enough but what do you do with lost&#x2F;corrupted data?<p>I have worked on a small CRUD app where everything was done with sprocs with a small laravel layer and that worked well enough.
kasey_junk超过 1 年前
This was a much more common practice when the architecture pattern was to have many applications accessing (and writing) to the same db. These days that’s pretty uncommon so the development overhead isn’t usually worth it.<p>That said, I have had use recently to split out which tables can be written by which applications and then abstracting the selects into views.
ensocode超过 1 年前
It probably depends on the use case. We usually try to avoid stored procedures as it is in-transparent, not really easy to debug and finally it is business logic in the data store which isn&#x27;t a good approach in my perspective.
max_hammer超过 1 年前
I work as data engineer and won&#x27;t advise using stored procedures.<p>It&#x27;s pain to debug and document.
gardenhedge超过 1 年前
It works but having DB compiles in your release pipelines is a pain