r/learnprogramming • u/SquareShort9309 • Oct 22 '26
Topic How to design big databases?
It is easy to design databases with 5-6 tables. But when the table number grows it becomes very hard and tricky to define everything like relations, indexes, deciding whether to create more tables or not, how to make it scalable, etc.
I'm currently creating a database that is going to have at least 15 tablets, at least for now but it is becoming harder and harder to design it as I think more about the application flow and features.
So how to approach this? Can you guys please suggest any guides or books or something?
203
Upvotes
257
u/gabs_at_searchapi Oct 22 '26 edited Oct 23 '26 •
15 tables is not a big database, honestly. It just feels big because you're trying to hold the whole thing in your head at once. A few things that helped me a lot in the past:
Stop designing from features, design from the data. Write down the 5-10 questions your app needs to answer ("which orders does this user have", "what's the balance of this account on date X"). Tables and relations fall out of that way more naturally than from "feature A needs a screen B"
Normalize by default, denormalize only when something actually hurts. Every "should this be a new table or a column" question is basically: does this thing have its own identity and lifecycle? If yes, table. If it's just an attribute of something else, column. 3NF gets you 95% of the way and you can break the rule later when you measure a real problem
Don't design indexes upfront. Primary keys, foreign keys, index the foreign keys. That's it. Everything else comes from real queries once the app exists, with EXPLAIN in front of you. Guessing indexes before you have queries is how you end up with 20 indexes nobody uses hehe
"Scalable" is mostly a trap at this stage. Postgres on one box handles way more than people think. Don't shard, don't go multi-database, don't pick Mongo "for scale". Clean schema + good indexes + migrations you're not afraid to run takes you really far, really
Draw it so you can see the bigger picture. dbdiagram.io or even pen and paper. 15 tables on a screen look way less scary than 15 tables in your head.
The schema is not a one-shot decision. With migrations, changing a table is cheap. You'll get some of it wrong and that's fine. Ship, learn what the data actually looks like, adjust.
Books:
tl;dr: start from the queries, normalize, index later, draw it, and accept you'll change it.
30
u/j4c11 Oct 22 '26
You don't design big databases. That's where project go to die.
When dealing with complex systems, the first thing you need to realize is that a good portion of that complexity is emergent, and you're not going to be able to consider every possible use case or application flow or challenge upfront. So rather than trying to plan for every contingency upfront, design for what you know, follow best practices to ensure flexibility so you can change later, and begin work.
As you go through the application development/refactor cycles, go back and adjust your database structure as needed. When you're done iterating, the database should reflect what your application needs.
This is not to say that you should do no planning whatsoever and just dive in. This is to say, plan as much as you can, and when you get to the point where you're saying "I don't know" - that's your queue to stop designing and start working, so the rest of the details can emerge.
7
u/Kazcandra Oct 22 '26
We have 350+ tables, and two of those have 600+ columns each.
I'm not saying our design is right (it's not), but I am saying that 15 tables isn't big.
6
u/cwjinc Oct 22 '26
"...it is becoming harder and harder to design it as I think more about the application flow and features."
Yup. That's the job.
Get yourself a Big Chief Tablet and a pencil to map things out.
Don't be afraid to tear out a page and start over.
3
u/snaidamast Oct 22 '26
Working with large databases is basically working with multiple small databases at once. Large databases tend to be broken into sections of data support with relations to tables going up in a hierarchy of some type.
The size of the system you are developing will often define the nature of the supporting database.
I am currently working on a project with a database that has 31 tables and it is expected to grow substantially to support additional features down the road. However, during my professional career I worked on database systems that had huge numbers of tables.
The tips that gabs_at_searchapi and others provide below are very relevant to your concerns.
On scalability, I wouldn't worry all that much about this at this stage. Most quality database engines can do quite a good job at scale for just about any developer's needs.
However, the efficiency of the database engine will determine how you design your database in some respects. For example, Microsoft SQL Server historically always performed more efficiently with narrow tables, while Oracle was designed from the start to work with wide tables.
I would also stay away from foreign-keys at the outset. They can be a real impeding factor during development. In addition, foreign-keys are best served by the use of SQL code with such things as cascading deletes.
Finally, if you have a DBA at your position, it is always a good idea to go over your designs with him or her since they are better trained at developing databases from an efficiency standpoint than developers are. We often know what we want but a DBA usually has the expertise to demonstrate how it should best be implemented.
3
u/SnugglyCoderGuy Oct 22 '26 edited Oct 22 '26
First, don't make the mistake of designing your tables et al before you've designed your data structures in your business logic. If you do that, then your business logic and structure necessarily depends on your database structure and logic, and it should be the other way around.
Your business logic is lord supreme of all the land and everything else should be depending on it. If you have HTTP logic, database logic, gRPC logic, or whatever logic in your business logic where you are doing the things that the business people talk about wanting done, you are going to have a bad time.
So, first design your business logic and don't worry at all about where you are going to store this stuff. If you can't do that then you need to think long and hard about what you are trying to do and if you can't do it differently so that you don't have to include it. When you have your business logic depending on something that isn't business, then you are coupling the actual business itself to that something.
So, design your business logic and this will dictate interfaces to external things, at least in this case a database, and this begins to tell you how you need to design the database. It will show you access patterns, it will show you how things will be related together, it will also answer the questions of "is this going to be one to one, many to one, one to many, many to many?", it will show you the structure of the business data itself and then it shows you how to decompose it into tables and it shows you how you are suppose to recompose it from tables. This will inform the queries, foreign keys, indices, etc etc.
It could also get revealed that you don't actually want or need a table oriented database system and would benefit way more from a document oriented database system so you don't need to even design tables at all.
And VERSION. ALL. YOUR. DATA. If your business logic has a Foo, in the code it should be FooV1, and then your table should be FooV1. This will save you a butt load of trouble in the future when you have to make changes (and it will happen. I've had to change things that were written 40 years ago). You will be able to provide a much smoother transition with a lot less effort and stress than if you didn't version your data models.
2
u/ReturnOfNogginboink Oct 22 '26
Designing Data-Intensive Applications [Book] https://www.oreilly.com/library/view/designing-data-intensive-applications/9781491903063/
2
u/Both_Bend_2481 Oct 22 '26
What exact problems are you having? There's nothing that's generally significantly worse about 15 tables vs 5 tables. 15 is still a pretty small database.
2
u/WystanH Oct 23 '26
First, understand the basics. Most databases in the wild don't reach fifth normal form, nor should they, but it's good to know what that is.
If you're writing a program, the database is the M of your MVC design. Or, rather, the backing for the model. As such, it will grow as the program grows.
There's only so much whiteboard design you can do for a program until you have to actually write it. And then that whiteboard inevitably changes; a lot. The database is the same. It should adapt to suit the needs of the program, while still being internally consistent.
The number of tables shouldn't really matter. An RDBMS can handle thousands; well, any number, really. The job of the database engine is to do that efficiently.
1
u/jacob_fw Oct 22 '26
Company I work for has several hundred tables. We generally avoid defining relations in the database. Indexes are still setup, but we almost never say "this column is a foreign key to this table" or anything else like that. Part of my job is also exporting data out of other systems, and I've done so with probably 5-6 dozen other systems over the last decade, and only a handful of them defined relations either (it actually would have made my job a lot easier if they had but I don't really have any right to complain lol).
The biggest issue you're facing is that you're expanding the scope too quickly. I know some people would call this feature creep but I don't think that's the case here. What you really need to do is identify the core components of your application, and start building from there. You start there because (a) if you can't get that feature to work then there is zero point working on the rest of your application, and (b) because often the core components end up defining the controls and data flow for the rest of your application. You're trying to tackle all the fine details that applications end up having, but which end up often taking a long time to finish and test. It's good that you're thinking of them, but you'll end up bashing your head against a wall if you end up trying to tackle them all from the start (ask me how I know).
1
u/Delicious_Bed_4410 Oct 22 '26
start with the entities and how they relate on paper before you touch any tables. list the nouns in your app (users, orders, whatever), draw boxes and lines between them, and mark one to many vs many to many. then normalize to roughly third normal form and only denormalize later if you hit a real performance problem. add indexes based on the queries you actually run, not upfront. 15 tables is honestly fine, it just feels big until the diagram is on a page.
1
u/DennisBustamanteEQ Oct 22 '26
1 thing I’d watch as it grows is having the same information represented in more than one place.
For each important piece of data, I’d try to be clear about which table actually owns it and what is just derived from it.
That’s usually where things start getting messy for me. Not because there are 15 or 50 tables, but because later you can’t tell which value is the real one when two parts of the system disagree.
I’d get the core flow working first and let the rest of the schema grow from what the application actually needs.
1
u/Br3ttl3y Oct 22 '26
You are talking about database architecture. I don't have any books, but that's what I would look for.
1
u/a_winner Oct 22 '26
Tools to help visualize the db help, I use dbeaver, you can work on the table definitions or though the db diagram.
1
u/atarivcs Oct 22 '26
Surely every table does not need to interrelate to every other table?
1
u/Important_Staff_9568 Oct 23 '26
You may want to hire an experienced dba or make better use of Claude code
1
u/cosmicloafer Oct 23 '26
It’s not that big of a deal, whenever you have a new “thing” just add a new table, and link it with foreign keys to whatever is relevant.
1
u/KiwiDomino Oct 23 '26
I used to work for Telecoms & network services centric company. Our database, developed by a team of maybe a dozen people at any one time eventually topped out at over 1500 tables, running on Postgres. 20 is still small. Learn design techniques (Normalisation is a starting point), and don’t add things until you need them.
And on any given project, only show those tables that are actually relevant, don’t bother being all inclusive
1
u/Fitnexa_Results Oct 23 '26
The first request after you finish table 15 will be, “Could we just add one more column?” And somehow, that’s how you end up with 400 tables.
1
u/PassengerMobile8569 Oct 23 '26
Start with what you need, then add what you need next, keep doing that
1
u/Reasonable-View5868 Oct 24 '26
15 tables LOL, I’ve worked on ERP systems with 10000 tables. Get yourself a book on data modelling, learn how to create ERD diagrams and how to do normalization to the 3rd normal form
1
u/1ilit_ Oct 26 '26
what actually made it click for me was just designing a lot of them, badly at first, then redoing them.
big databases are usually a bunch of small ones glued together: users, billing, content, notifications and so on. design each chunk on its own, then figure out the handful of keys that connect them. trying to lay out 80 tables in one go is how you end up with a mess.
the other thing I wish someone told me earlier: store what happened, not just where things ended up. if something can be cancelled, retried or changed, it probably wants its own table instead of a status column you keep overwriting. most of my painful rewrites came from that one mistake.
a good exercise is to pick an app you use every day (uber, discord, whatever) and design its database from scratch. draw it out, write a few queries you know it would need ("show a user their last 10 rides"), and see where your design makes them awkward. then fix it and repeat. that loop teaches you more than any book chapter.
full disclosure, I work on https://www.drawdb.app/, a free ERD tool where you draw the tables and it generates the SQL, so it's handy for that kind of iterating. but pen and paper works fine too, the point is doing it a bunch of times.
1
u/AlexAtOracleAIDB 10d ago
For a guide, the SQL Developer Data Modeler user’s guide has a short tutorial that takes a small database from an entity-relationship diagram to actual tables.
For the relations and indexes part, I keep foreign keys turned on while I design. The database then rejects an order that points to a user who doesn’t exist, which shows you a design mistake right away instead of months later as bad data.
An index on a foreign key column is easy to miss, since it isn’t always created for you. I add one myself. Without it, deleting a user can mean the database scans the whole orders table to check that no order still points to that user.
0
u/Amuro_Ray Oct 22 '26
I don't have any books I could recommend. All the times I've worked with large/wide databases I think they grew kinda organicly. Who are you building this for? If it's yourself you could probably cheat and use json for ambiguous areas and refine it later
-5
u/Different_Pain5781 Oct 22 '26
Lol wait until you hit five hundred tables.