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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Starting to love RDBMS-first design. Anyone else?

6 点作者 thookipodu9 个月前
I have been working on my side project for the last 10 months. My initial focus was on language, frameworks (elixir, liveview) during the initial months. Designing tables in rdbms (postgres) was out of necessity. For the last 2 months, usage of Stored procedures and postgresql functions just clicked. The feel of working with raw data inside the database instead of running around ORM hoops felt very liberating. Just realizing loops and conditionals are made unnecessary by SQL and the fact that abstraction layers can be built using views is quite addictive. I have deleted much of my UI code that I painstakingly wrote in elixir to SQL and it feels good. Interested to hear if someone has went down this path. Would like to hear if there are any gotchas in this approach ?

2 条评论

cpburns20099 个月前
There&#x27;s a few gotchas.<p>1. Stored procedures are not automatically in an SCM. It takes good discipline to make sure they&#x27;re properly committed, and synchronized with your databases.<p>2. Don&#x27;t try to do too much with one view or query in general. This has bitten me many times. Don&#x27;t be afraid to split-up a query. That can simply be a few separate queries, or creating a temporary table that&#x27;s built up over multiple steps. It&#x27;s more work up front, but the increased performance can be necessary.<p>I&#x27;ve come to adopt creating temporary stored procedures in my processes when I needed them. I find that&#x27;s a nice balance when complex processing is more efficient and convenient to do in PostgreSQL rather than a round trip to Python.
taylodl9 个月前
Once you realize an application&#x27;s primary function is providing data-driven visualizations, creating new data, updating existing data, or deleting existing data - all of which are data management functions - then you realize your data model and its handling are the most important parts of your system architecture.<p>Once you&#x27;ve crossed that bridge, you see that an RDBMS delivers two important capabilities: durably persisting the data and managing the data. There are other ways to achieve these capabilities, an RDBMS just happens to be very good for many use cases. The point, though, isn&#x27;t the RDMS. It&#x27;s about data. You&#x27;re talking about Data-Oriented Architecture and Data-Oriented Design.<p>But let&#x27;s stick with RDBMSs, since that&#x27;s what you&#x27;re currently enamored with. As you&#x27;re proceeding building-out your application and all your tables that are going to be supporting it, what happens if you need to change those tables for any reason, like say performance? Since your application is issuing queries against those tables it now needs to be modified and tested. Not good. Especially if, as time goes on, there are a suite of applications built around that data. Now you have to coordinate schedules across several teams, etc., etc., etc.<p>The mistake being made in this scenario is the application is tied directly to the <i>physical</i> data model. One common way people resolve this problem is using APIs. Several applications can use the same API and if the physical data model changes, just change the API implementation and you&#x27;re good to go. But that&#x27;s not the route you want to take. You want to work with the RDBMS. Do we have another option?<p>Yes! Materialized views! Materialized views comprise your <i>logical</i> data model and should be thought of as the application interface to the RDBMS. The materialized views manifest all the Join logic, all the foreign keys, all that stuff. All your application has to do is Select from the materialized view. For all the application knows, a single Select from the materialized view may result in half a dozen Joins behind the scenes. The application doesn&#x27;t know, and it doesn&#x27;t care. That logic is kept out of the application and is pushed onto the RDBMS. If the underlying physical data model has to change, then the materialized view implementation needs to be modified. The application is not affected.<p>Materialized views offer other advantages as well. You have a complex query that needs a DBA to tweak the query? The materialized view has the tweak. All the applications using that view have no knowledge or care. Materialized views are far more performant than an API. Need to pull a lot of data? I mean <i>a lot?</i> APIs are a horrible way to go. An RDBMS? Easy-peasy! Especially when using a materialized view! Materialized views are also pre-compiled by your RDBMS query compiler - so they run faster and won&#x27;t blow out the cache on your query compiler. In fact, other users who may be executing ad-hoc queries against your database won&#x27;t cause the performance of your queries to suddenly start degrading in performance because their compilation has been blown out of the query cache. You don&#x27;t <i>have</i> to employ the CQRS pattern to maintain your performance!<p>But here&#x27;s the best thing: both Oracle and PostgreSQL (those are the RDBMS engines I use and am most familiar with) allow Updates&#x2F;Deletes via a materialized view! That means while the materialized view is the RDBMS&#x27; means of delivering data to applications, applications can view that logical data model as being <i>the</i> data model. They can do their Select, Update and Delete through the materialized view. Put your materialized views in a separate schema from the one containing your physical tables and only allow the applications to access the schema containing your materialized views. Boom! Applications can no longer directly access the physical data model!<p>I&#x27;ve been using this approach for the past 15 years on several mission-critical systems for Fortune 200 companies. It works well, and keeps you focused on what&#x27;s most important - the data!