TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stored Procedures as a Back End

209 pointsby steve-chavezover 4 years ago

50 comments

Raidionover 4 years ago
If I make the change in code, I can roll it out, test it in prod, and then slowly migrate traffic over in very standard and well defined ways.<p>If I deploy to the database and something goes wrong, I need to really trust my rollback scripts. If the rollback scripts go haywire, you&#x27;re in a tough spot. If that happens to code, you can literally just move all the traffic to the same thing that was working before, you don&#x27;t quite have that luxury with the database.<p>You can have a bunch of servers, you really only can have one database. This means the database should be very sensitive to changes because it&#x27;s a single point of failure. I don&#x27;t like modifying or changing single points of failure without very good and well tested reasons.<p>Could you version your procs and have the new version of the code call the new procs? Sure, but now you have to manage deployment of both a service, and the database, and have to handle rollover and&#x2F;or A&#x2F;B for both. If my logic is in there service, I only have to worry about rolling back the service.<p>Database logic saves you a little bit of pain in development for a ton of resilience and maintenance costs, and it&#x27;s not worth it in the long run IMO. Maybe it&#x27;s because the tooling isn&#x27;t that mature, but until I can A&#x2F;B test an atomic database, this doesn&#x27;t work for a lot of applications.
评论 #25296498 未加载
评论 #25296878 未加载
评论 #25296270 未加载
评论 #25298579 未加载
评论 #25297083 未加载
评论 #25299075 未加载
评论 #25297858 未加载
评论 #25303612 未加载
评论 #25296234 未加载
评论 #25299708 未加载
评论 #25297349 未加载
mdomsover 4 years ago
I have worked on an application that heavily used sprocs for business logic. I would not wish it on anyone. It will seem fine (and actually it will seem better - faster and easier to manage) while your project is small.<p>In a few years when you need to make some major changes you will see why it was a bad idea, and it will be too late. Have fun modifying hundreds or thousands of sprocs when you need to make a large-scale change to the structure of your data, because SQL won&#x27;t compose. Have fun modifying dozens of sprocs for each change in business logic, because SQL won&#x27;t compose. I guarantee you will have mountains of duplicated code because SQL won&#x27;t compose.
评论 #25292502 未加载
评论 #25295069 未加载
评论 #25296087 未加载
评论 #25292401 未加载
评论 #25297377 未加载
评论 #25294732 未加载
评论 #25297091 未加载
jandrewrogersover 4 years ago
Back in the mid-90s, this was how many of the high-end web apps were built, using Oracle. Despite its limitations, and Oracle license fees (but back then you were paying for Sun servers anyway), it worked surprisingly well and was a reasonable model for many database-driven web applications. Some of these systems were used for mission-critical global operations handling enormous amounts of money and complex business. It had a lot of advantages versus other web app stacks at the time (commonly a mountain of Perl).<p>Oracle was happy to sell database licenses but made zero effort to develop this way of using the database as a first-class capability. They were astonished that it even worked as well as it did, they did not expect people to take it that far.<p>For some types of applications, something along these lines is likely still a very ergonomic way of writing web apps, if the tooling was polished for this use case.
评论 #25292335 未加载
评论 #25292417 未加载
FlyingSnakeover 4 years ago
Using a database as a backend comes up time and again and developers are sharpely divided over this concept. I feel that developers who started their careers before the advent of NoSQL DBs&#x2F;MEAN stack&#x2F;etc had to go through the rigor of fine tuning their SQL queries and witness the powers (and quirks) of a performant SQL DB. These devs are usually a fan of using DB-as-a-backend. Personally, I am a big fan of PostgREST and I apply it whenever I find a use case that suits it. I find it quite pragmatic and it just shines when it plays to it&#x27;s strength. Pragmatic developers should took a pragmatic look at such approaches and try to use the powerhouse that are SQL DB engines.<p>Shameless plug: I&#x27;ve written an article to showcase it&#x27;s philosophy and how easy it is to get started with PostgREST.<p>[^1]: <a href="https:&#x2F;&#x2F;samkhawase.com&#x2F;blog&#x2F;postgrest&#x2F;postgrest_introduction&#x2F;" rel="nofollow">https:&#x2F;&#x2F;samkhawase.com&#x2F;blog&#x2F;postgrest&#x2F;postgrest_introduction...</a>
评论 #25292376 未加载
评论 #25297704 未加载
astineover 4 years ago
<i>&quot;“Database should store data not logic!” — well, no. This is called data-base, not data-bag. Databases are specialized engines to deal with, you guessed it, data. And they are extremely efficient at it. Not to mention - majority of the job backends do, is dealing with data. This is good for prototyping. Unfortunately more often than not — prototypes lands on production with updated connection string and debug=False.&quot;</i><p>Or, as I like frame the issue, there&#x27;s a difference between a datastore and a database. Datastores&#x2F;persistence engines store your data in manner that you can get it back later. Think of a key-value store for example. A database, however, assists you with the management of your data, including helping to ensure correctness of the data, tracking changes, among other things. For most systems lot of work is already managed in-database whether you like it or not.
评论 #25297005 未加载
评论 #25297936 未加载
jrochkind1over 4 years ago
Everything old is new again. This was a common way to write client-server software in the pre-web world, clients talking directly to a central rdbms.<p>I guess the one true thing in software dev is the cycle&#x2F;pendulum keeps rotating&#x2F;swinging. Often without people realizing it&#x27;s swinging back instead of brand new!
评论 #25297726 未加载
评论 #25297194 未加载
kemiller2002over 4 years ago
Really anything can be a good or a bad idea depending on the problem domain and how it&#x27;s implemented. He doesn&#x27;t really go into how he made it work too closely, so it&#x27;s easy to say it was a success. The real interesting thing to understand would be, &quot;Why was it a success versus other approaches?&quot; What really made it a better choice vs other approaches? What was the team make up? He says that it made the code easy to understand. To whom? Interesting the team made it work, but not enough to say, &quot;Sure let&#x27;s try it without a lot more forethought.&quot;
评论 #25293309 未加载
tacticaldevover 4 years ago
I&#x27;m currently part of a team trying to UNDO this very concept. It sounded great in the beginning, but after years of headaches and wasted cash; we&#x27;re building the backend in something else. I wasn&#x27;t part of the original team. Nor was I part of the decision to migrate off of the system. I just know that for my employer, it was a bad decision those many years ago...
评论 #25292186 未加载
评论 #25292360 未加载
loldot_over 4 years ago
What I&#x27;ve found being the problem with this approach is that sql does not lend itself very well to composability. Sure, you can make functions, views and stored procedures and compose them. But when you start actually composing queries of these parts the could lead to different execution plans having wildly different performance characteristics.<p>Also tooling around SQL, i.e. refactoring tools and debuggers, is not great - if even available at all.
评论 #25297693 未加载
Brentwardover 4 years ago
Not that this is an important part of the post, but in general parsing the output of ls like the author does in the for loop is a bad idea[1].<p>I think the one-liner would be better as<p><pre><code> for i in [0-8]*&#x2F;*.sql; do psql -U &lt;user&gt; -h localhost -d &lt;dbname&gt; -f $i ; done </code></pre> or even better as something like<p><pre><code> find . -name &quot;*.sql&quot; -exec psql -U &lt;user&gt; -h localhost -d &lt;dbname&gt; -f {} \; </code></pre> [1] <a href="http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;ParsingLs" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;ParsingLs</a>
RussianCowover 4 years ago
For those who looked at list item #9 (&quot;no code versioning&quot;) and thought the author&#x27;s makeshift solution to migrations seems awful, I highly recommend sqitch[0], an extremely simple but well thought out migrations tool that is platform- and database-agnostic. I&#x27;m using it on a side project with success, and haven&#x27;t run into any major hurdles yet.<p>[0]: <a href="https:&#x2F;&#x2F;sqitch.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sqitch.org&#x2F;</a>
评论 #25297304 未加载
marcosdumayover 4 years ago
The one problem this technique has is that data and software normally have two very different speed and correctness requirements. That means that data and software should evolve following different procedures, what heavily implies that you want different people working at them, or at least you want people to look at them at different times.<p>For that, you really want independent access controls and CI tooling.<p>Of course, you can&#x27;t separate much if you are a 1 or 2 people team at early stages of some project. And it may help you move faster.<p>But:<p>&gt; &quot;Rebuilding the database is as simple as executing one line bash loop (I do it every few minutes)&quot;<p>This denounces a very development-centric worldview where operations and maintenance don&#x27;t even appear. You can never rebuild a database, and starting with the delusion that you will harm you on the future.
评论 #25292735 未加载
评论 #25291524 未加载
kevincoxover 4 years ago
The primary issue I have seen with stored procedures is how you update them. I would be curious how they manage that.<p>Generally when releasing new code you want to do a gradual release so that a bad release is mitigated. It would be possible by creating multiple functions during the migration and somehow dispatching between them in PostgREST but I would be interested to see what they do.<p>The other obvious concern is scaling which was only briefly mentioned. In general the database is the hardest component of a stack to scale, and if you start doing it do more of the computation you are just adding more load. Not to mention that you may have trouble scaling CPU+RAM+Disk separately with them all being on a single machine.
评论 #25291932 未加载
评论 #25292851 未加载
评论 #25291556 未加载
评论 #25292456 未加载
dventimiover 4 years ago
Many commentators seem to be getting hung up on &quot;stored procedures.&quot; That&#x27;s understandable given the title of the original post, but it&#x27;s also unfortunate because it mixes independent concerns: business-logic in the database, and using stored procedures. You can do the former without the latter, and in my experience it works better:<p>* custom data types and domains<p>* use schema and namespaces<p>* foreign key constraints<p>* check constraints<p>* default values<p>* views<p>* functions (not stored procedures)<p>* triggers<p>In my experience, you can get a clean, efficient, easily-understood data model and application with those ingredients, without ever having to touch a stored procedure, a looping construct, or a conditional statement.
stmwover 4 years ago
Is it just me or is this literally the worst architecture idea that&#x27;s popped up here? I&#x27;ve seen this done in a few large projects, including some extremely famous products, and it produced endless grief for the engineering team - and ultimately the organization. Among other things, it makes performance scaling much harder to achieve, makes software deployments really scary, and makes it almost impossible to migrate to a different database vendor. It is true that it can be done. But I really don&#x27;t think you want to.
aszenover 4 years ago
I&#x27;m very curious about this pattern, but does the tooling exist to make this feasible across a team. One of the big problems with SQL views is their lack of discoverability. The lack of tooling also plagues SQL based systems, I would miss goto definition, find all references and sensible autocompletion. I am curious why no one has built suitable tooling around all this, because it is a great idea for lot&#x27;s of scenarios
评论 #25297103 未加载
throwaway_pdp09over 4 years ago
&gt; Also typical database uses 96% of the time for logging, locking etc. and 4% percent for actual data operations<p>That&#x27;s a strange claim, in my work it&#x27;s always been 100% data ops.<p>&gt; “Letting people connect directly to the database is a madness” — yes it is.<p>why?<p>&gt; Killing database by multiple connections from single network port is simply impossible in real life scenario. You will long run out of free ports<p>Depends entirely on the workload. And it&#x27;s very possible. All too entirely so.
评论 #25293373 未加载
_benjover 4 years ago
In my experience I actually got A LOT more from this approach using postgraphile&#x2F;Hasura in front of my DB and later moved to dosco&#x2F;super-graph in order to add custom functionality that was kind of a pain to do on the DB.<p>I really liked the mixed approach of have the DB do everything that it can and have a light backend that can handle http requests, api callbacks, image manipulation and whatever else.
评论 #25295004 未加载
laudable-logicover 4 years ago
Maybe sorta off-topic, but does anybody have any stories to tell about successes&#x2F;fails with the .NET CLR in MSSQL?<p>I worked on a project that made moderate use of this. Worked alright; biggest problem was convincing DBAs&#x2F;IT to enable it.<p><a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;SQL_CLR" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;SQL_CLR</a>
评论 #25297794 未加载
dangover 4 years ago
jgrahamc wrote a fascinating comment about how Cloudflare started this way: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22883548" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22883548</a>
lfluxover 4 years ago
At a previous job I was at 2007-2014, we were doing classified ads[1] we had a lot of logic in stored procedures in our Postgres database written in pl&#x2F;pgsql. It made sense at the time, but after a while these got really unwieldy and deploying things got tricky, since you we needed to update both appserver code and database stored procs at the same time. There also wasn&#x27;t a great way to update stored procs at the time - we built this into an RPM that just ran a postinstall script to do an atomic swap.<p>I wouldn&#x27;t build this into the database were I to build it today.<p>[1] blocket.se&#x2F;leboncoin.fr&#x2F;segundamano.(es,mx)
评论 #25297696 未加载
cpursleyover 4 years ago
I&#x27;m basically doing the same thing with Hasura, but instead of using their Events webhooks, listening to the WAL for changes with Elixir (I stole the listening code from Supabase realtime: <a href="https:&#x2F;&#x2F;github.com&#x2F;supabase&#x2F;realtime" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;supabase&#x2F;realtime</a>).<p>I&#x27;ve never written so little backend code for such high performance in so little time. If I were starting today, I&#x27;d probably use Supabase for the API as well (which is PostgREST under the hood).
mritsover 4 years ago
I&#x27;m not old enough to remember the first one but I know at least Oracle beat you to this idea in the early 90s.
评论 #25291783 未加载
rdsubhasover 4 years ago
This was a trend in 2002. PSP (PLSQL Server Pages), just like ASP and PHP. <a href="https:&#x2F;&#x2F;oracle-base.com&#x2F;articles&#x2F;9i&#x2F;plsql-server-pages-9i" rel="nofollow">https:&#x2F;&#x2F;oracle-base.com&#x2F;articles&#x2F;9i&#x2F;plsql-server-pages-9i</a><p>Many large Banks (including Bank of New York where I worked in) and ERPs have stored procedures as their life and blood, have invested and built huge tooling over it – namespacing, dependency tracing of who&#x27;s using what, monitoring, etc. It&#x27;s excellent at having a tightly integrated, closed system where the Database is the source for authorization, module ownership, etc.<p>But there were reasons why it was not successful:<p>* Severe vendor restrictions. e.g. For a long time, Oracle could not do distributed compile-time PL&#x2F;SQL checks. If you changed a function definition, other procedures that depended on it would break. But if you change a <i>procedure&#x27;s</i> definition, it wouldn&#x27;t tell you anything until you deploy it at runtime and something somewhere will fail, or recompile every other procedure. I don&#x27;t know if it&#x27;s fixed yet.<p>* Testing, or the severe lack of tooling that we have taken for granted – code coverage, easy local machine unit tests, etc<p>* Lack of modules (availability of an ecosystem or community of modules to solve recurring problems), and the subsequent dependency management and versioning overhead that comes with it<p>* Debugging, i.e. putting breakpoints, remote debugging, logging, good exception handling, etc<p>* Change management, graceful rollouts, rollbacks, etc. There is no so-called &quot;Router&quot; for stored procedures, they&#x27;re all tightly coupled with each other, so there is no easy way to rollout and rollback new versions quickly without recompiling each time<p>* Connection limits. There is a tradeoff on a database being good at all the above – and how many concurrent connections it can support. It was impossible to simply &quot;pass through&quot; entire web traffic over to the underlying connection, without investing in quite heavy tooling<p>Overall, if you already have put all your eggs and tooling into one giant, well designed, well governed database – then PSPs are a natural evolution forward.
njkleinerover 4 years ago
Related: <a href="https:&#x2F;&#x2F;sive.rs&#x2F;pg" rel="nofollow">https:&#x2F;&#x2F;sive.rs&#x2F;pg</a>
hartrorover 4 years ago
Everything old is new again.<p>Have a customer with a 25 y&#x2F;o Oracle PL&#x2F;SQL system that basically operates their business and they can barely iterate on it. Every year a new consultancy comes in with their flavour of microservices and tries to subsume some functionality and they choke on the monolith to end all monoliths.
dblohm7over 4 years ago
My previous employer did this, but it was fraught with problems.<p>It can be done properly, but you need your entire development team to have a good understanding of declarative programming, and you need to have as much business logic embedded in the stored procedures as possible.<p>Instead, we had developers who continued to think imperatively. Whenever they were implementing a new feature, they would write several small stored procedures to obtain data, and then process it iteratively inside the application server.<p>The ensuing result was that there was <i>far</i> too much chatter between the application server and the database server than was actually necessary; loading a new web page in the application would take <i>seconds</i> with one single concurrent user, and this was a product that was supposed to scale to <i>tens of thousands</i> of concurrent users!
LukeShuover 4 years ago
That&#x27;s the approach that <a href="https:&#x2F;&#x2F;crt.sh" rel="nofollow">https:&#x2F;&#x2F;crt.sh</a> uses.<p><a href="https:&#x2F;&#x2F;www.lukeshu.com&#x2F;blog&#x2F;crt-sh-architecture.html" rel="nofollow">https:&#x2F;&#x2F;www.lukeshu.com&#x2F;blog&#x2F;crt-sh-architecture.html</a>
gnatover 4 years ago
I&#x27;m very interested in this, as I come from the web world yet find myself in a company that has a 30-year legacy of putting business logic into the database. It served them really well, and that they run quite lean in terms of hardware. But they&#x27;re built on Microsoft tech ($$$), and moving things to a traditional database=datastore cloud architecture would require a substantial change. Does anyone have resources they can point me to on the wisdom or folly of keeping this logic-in-database approach? It seems like the tradeoffs in the different approaches are all about where the smarts are, and whether you can horizontally scale those smarts. Does that sound accurate?
评论 #25293269 未加载
评论 #25295406 未加载
malkiaover 4 years ago
Anyone remembers dBase, Clipper, FoxPro, etc.?<p>I actually had to complete a dBase project for my last year in high-school (it was math&#x2F;comp-sci oriented, so it made sense), but man I did not like it at all :) - I was all about Turbo Pascal then!
chrismsimpsonover 4 years ago
Love it. I am working on a similar but different concept for a prototype I am working on. The project requires many document&#x2F;NoSQL style micro services, and I had the idea to do this directly with the file system and JSON serialisation, with thread locking on appropriate read&#x2F;write calls (some calls that are read only don’t require locks). It is <i>extremely</i> fast and performant, and given the way I’ve delineated the block storage that the file system runs on, my hope is it will also scale reasonably well.<p>I have also implemented a message queue using the same design&#x2F;infrastructure.
revskillover 4 years ago
I&#x27;m using this pattern on my projects for my customers.<p>One of the reason is that my team knows only SQL things, they don&#x27;t have time to install other programming tools on their machine.<p>So, just install postgresql and code the logic you want !
评论 #25297043 未加载
flerchinover 4 years ago
Good luck debugging! Seriously. You can&#x27;t attach a debugger to your DB.
评论 #25291869 未加载
评论 #25291811 未加载
评论 #25291914 未加载
评论 #25292390 未加载
nwsmover 4 years ago
Nice work; PostgREST seems neat. However, the aim of this stack is simplicity, but it is incompatible with serverless products, which I now vastly prefer for ease of use and lower effort- traits I would consider part of &quot;simplicity&quot;.<p>But I&#x27;m sure there is a valid argument contrasting simplicity with a managed nature of a tech stack, not to mention a cost comparison.
评论 #25291420 未加载
评论 #25291606 未加载
notJimover 4 years ago
See also<p><a href="https:&#x2F;&#x2F;www.slideshare.net&#x2F;beamrider9&#x2F;scaling-etsy-what-went-wrong-what-went-right" rel="nofollow">https:&#x2F;&#x2F;www.slideshare.net&#x2F;beamrider9&#x2F;scaling-etsy-what-went...</a><p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=eenrfm50mXw" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=eenrfm50mXw</a>
overcastover 4 years ago
Managing business logic in applications is a nightmare to control. That&#x27;s why things like constraints and unique keys exist database side. If I have triggers that manipulate the data and update another table, you&#x27;ll know that&#x27;s always going to happen regardless of what application is writing to it.
qpioxover 4 years ago
The first web application I did, that went in production 22 years ago, was in it&#x27;s entirety based on stored procedures. This was the main idea of Oracle&#x27;s web application services at that time. But, you have a very big tradeoff - you will be stuck with that database. Hardly portable.
sdzeover 4 years ago
What about web applications without using javascript?<p>Could you send out Content-Type text&#x2F;html and react on FORM-Posts?<p>I still develop webapplications with only <i>additional</i> javascript. The webapp on itself is completely usable with javascript turned off (the way it should be!)
cafardover 4 years ago
I would add that if you have SQL Server, and you want to use it other than in SQL Injection Mode, you more or less have to use sprocs. Anyway, you used to have to--I haven&#x27;t checked in lately.
评论 #25298309 未加载
wcpinesover 4 years ago
Derek Sivers did something similar. Related discussion:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21362190" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21362190</a>
alphanumeric0over 4 years ago
I personally would not make this choice for new projects. I only use technology that is appropriate for the domain I&#x27;m using it in. Although, I am hoping this trend encourages better tooling and more modernization of SQL.<p>As others stated, my concerns are primarily that SQL lacks a lot of general-purpose language features (modules, namespacing, composability, no easy way to debug, etc.) which seem to be ideal for writing applications.<p>I would be interested to see a project written with PL&#x2F;Python + PL&#x2F;SQL stored functions in Postgres. PL&#x2F;Python functions would be the interface between API calls and the database, and SQL would do what it is currently designed to do: straight-forward data manipulation.
评论 #25296626 未加载
8noteover 4 years ago
How do you handle decoupling your API model from your database model?
评论 #25296706 未加载
评论 #25296194 未加载
aryehofover 4 years ago
Well it’s come full circle for me. 40 years ago I started as a professional programmer. The big issue at the time was how to represent business logic, other than as stored procedures.
评论 #25306117 未加载
snicker7over 4 years ago
This is basically how Q&#x2F;kdb+ applications work in finance. Data and application exists on the same server. Of course, the programming model is much richer than pure SQL.
jdmoreiraover 4 years ago
Oh I feel like it&#x27;s 2007 again and I&#x27;m a Junior developer writing a frontend in Delphi and the business logic in PL&#x2F;SQL. Those were the days.
throwaway346346over 4 years ago
Using a throwaway because I don&#x27;t want anyone to guess the product&#x2F;company.<p>I&#x27;m CTO at a company who&#x27;s product makes heavy use of stored procedures for business logic. It&#x27;s constantly the cause of all the biggest headaches. I inherited this architecture, I didn&#x27;t design it myself. I believe the original rationale behind the design boils down to &#x27;SQL is better at the type of set based operations this product does a lot&#x27;. i.e. If you&#x27;ve got an array of 100k values, and you want to perform an operation against a second array of 100k operands, you can do that much faster in SQL with a join than you can by loading them all into memory&#x2F;code, looping over to do the operations, and saving them again. This is kind of true in the simple cases, but once it grows to require lots of logic and conditionals in those operations, and then chained operations you start to lose the benefit.<p>Upgrades and versioning are generally a bit awkward, we&#x27;ve got it fairly smooth now, but it still causes pain reasonably frequently when something in the upgrade process breaks. It was worse when I started as many upgrades were a bodged together folder of scripts with lots of &#x27;if exists&#x27; type checks in them. Now at least we use a mostly automated diff based upgrade process for the database. Some types of changes still require manual upgrade scripts though. The articles solution of a folders of numbered scripts doesn&#x27;t really look viable if you need to manage upgrades from different versions to the current latest version. Re-creating customer databases doesn&#x27;t really go down well when they tend to like to keep their data.<p>Debugging is awkward. There are tools, but none of them compare to code debuggers.<p>SQL isn&#x27;t easily composable, we have repeated SQL all over the place (or nearly repeated with small changes which is kind of worse because you don&#x27;t spot the differences). Finding a bug in one of these repeated blocks means spending the next few hours hunting down any similar SQL to check over the the same bug.<p>Performance is unpredictable and all over the place. Stored procs that run fine one release will suddenly start performing like a dog the next release. We often never discover the actual &#x27;cause&#x27;, the thing that changed that made it slow down. We just end up finding a way to make it fast again by adding a new index, or changing the way a join is composed, or splitting something up. I&#x27;ve not yet got evidence, but I&#x27;m convinced that we&#x27;ve made performance improvements in one release only to reverse the exact changes several releases later also as a &#x27;performance improvement&#x27;. It feels like playing wack-a-mole. Because of query plan caching, parameter sniffing and other optimizations the DB we have had scenarios where the performance of feature x, depends on if you used feature y before hand in the same session or not. We have some exact duplicates of stored procedures that are only there to ensure that two different code paths that use them don&#x27;t share the same plan cache because when they do we get problems. Performance characteristics are often very different between dev setup and customer setup. Performance characteristics are different for customers that choose to host on cloud servers compared to customers that host on physical hardware. I don&#x27;t just mean cloud is slower, I mean it&#x27;s different. Some things are faster on cloud servers, but it&#x27;s never predictable what will be what. It makes testing for performance very hard.<p>The articles statement about &#x27;a database spends 96% of it&#x27;s time logging and locking&#x27; is totally irrelevant. So what if that&#x27;s what it spends 96% of it&#x27;s time on. It&#x27;s still spending that time. And as soon as your database has multiple users all those locks are going to start getting in the way of each other and causing delays or deadlocks.<p>It doesn&#x27;t scale at all. Our DB severs are <i>powerful</i> and we can&#x27;t realistically go much bigger (CPU &amp; RAM wise), yet better performance is probably one of our customers biggest requests.<p>Deadlocks are not uncommon, hard to defend against, hard to fix, and half the time introduce other deadlocks in other places.<p>Maybe it&#x27;s good in some scenarios, but once you have a growing evolving product being built by a team, it&#x27;s far far harder to manage if a large chunk of the logic is in SQL.
sisciaover 4 years ago
&gt; Performance hint: developers using cursors should have their hands cut off. This should do the trick.<p>What is wrong with cursor?
评论 #25295950 未加载
corytheboydover 4 years ago
How does this work with database replication? Does all “application logic” have to execute on the master database?
评论 #25291115 未加载
bengaliover 4 years ago
How do you guys unit test your business logic located in the SQL code?
g0rbonglerover 4 years ago
No.