Back
Question
Asked

Every table have its own DB (SQLite) + php

Hi everyone,

It’s been a while—I hope everyone is doing great and shipping a lot! :D

I have a question: Recently, I’ve started creating some projects again and found that development in PHP is surprisingly fast for me. I don’t know why, but I’m enjoying it. However, I have a concern regarding database creation. I tend to create one table per database, which I suspect might not be ideal. I want to know: will this approach slow down my web app if I have 1 million users or 1 million visits per month? Just curious.

Thanks in advance. :)


Welcome back, and hell yeah welcome to PHP!

Yeah, this is really not a good idea unless you have a very specific technical reason for it. Pieter Levels does it for his projects, but I'd focus on taking product/business advice from him instead of technical advice most of the time.

One of SQLite's limitations is only having one write connection to a database at a time. So splitting into multiple different DBs allows you to have multiple write connections. However, for most projects, it's unlikely you'll hit a traffic scale where you would need to do this.

What I would recommend doing this for is when you have app functionality that can be DB-heavy and you don't want to affect your users, like a DB-drive cache or queue system.

If you aren't using a framework yet, I can highly stress that you try one out. And especially if that one is Laravel! If you think base PHP is fast to work with, once you get the hang of the ecosystem, you can go to super speed with Laravel.

If you'd like an example of this multi-SQlite DB pattern, I use it in my #toyboxforlaravel project.

Oh and PS to optimise your SQLite DB performance, have a look here:
github.com/nikspyratos/toybox…
All of those are pulled from this and other articles:
kerkour.com/sqlite-for-servers

The long and short of the multi-db usage as well is that there's separate databases for Queue, Cache, and Pulse (performance monitoring) functionality, because each of those can become very write-heavy and affect users as app traffic scales up.

This is a really useful info and I really appreciate for this. Thanks my man @nikspyratos

Ive tired rails, I’ve issue with the ORM, has many has one from orm is confusing sometime for me. Means I can understand but when I want to implement something. I actually struggle. Also, I want to add functions by myself and also want to understand how security work properly. And when I want to implement something in php it looks easy.

Also SQLite is easy to setup, jquery is easy to use. Also I can understand php easily.

I just want to know the above thing and then I want to stick. If a website have 1ml visits per month and I have like 15-20dbs. Will it make my website very slow? If not then I’ll just stick to the same stack. :)

You don't have to use the ORM if you don't want to. But that's really like 5% of the framework when we're talking about using frameworks.

Personally ORMs just make things faster to develop with than hand writing queries.

There's also plenty of learning resources online, like Laracasts.

There are projects out there scaling pretty well with just SQLite, there's no reason yours wouldn't. I wouldn't worry about trying to serve 1 million a month yet if you aren't even serving 1. Solve problems as they arise, not before.

Exactly, that’s why I want to use simple stack. So are you suggesting me to use laraval?

If your aim is to learn new things specifically for shipping, then yes.

With only the free packages in the official ecosystem, you can have user registration & auth, payment provider setup (Stripe, Paddle, or LemonSqueezy), application performance monitoring, feature flags, code quality linting, websockets, API setup (both simple and complex like OAuth) and social logins (e.g. Google), all running within a few minutes.

Then there's the free WAMP/XAMP alternative called Herd which makes the dev environment easier.

Then there's the paid ecosystem, which can take care of infrastructure for you (both normal servers with Forge and serverless with Vapor), deployment (Envoyer), have premium payment dashboards (Spark), and application admin panels (Nova, but I prefer the free Filament instead)

Also, currently the plan here is to ship ideas fast as possible. Maybe InshaAllah in future if something turn big. Then I’ll think at that time. Till then I just want to sharp my axe(skills). So I can ship something. :)

Honestly, save yourself the headache and use a proper database with nice joins and stuff. Save time on managing weird stack choices and stick to the standard, especially when there's no good reason for it or if you're just getting started.

Is totally fine if you use SQLite.

Have a look at Mongo as well, it is a really good database and surprisingly easy to run yourself. I actually recently switched all my databases from SQLite to Mongo.

You sound like you might just be getting started so I'd suggest you stick to very well known patterns and not try to overcomplicate the DB design.

In other words: you should just have 1 SQL DB, multiple tables. Design the tables and columns appropriately using the right data types, and ask ChatGPT for help if you're not sure which data type to use.

Avoid:
- Multiple DBs, NoSQL, and any kind of premature optimization. Worry about 1 million users/visits problems when you get to that level, and even at that level modern RDBMS software can handle that kind of load on a single DB server.
- MongoDB and non-relational DBs. Mongo tends to lie about what their databases can handle despite fancy "transactions" marketing on their site

If you don't believe me, note the no less than 5 separate studies (!) into how Mongo loses writes and experiences other problems you really don't want to have to think about: jepsen.io/analyses

To be fair, these studies are slightly old and it's very possible they've been addressed by now, but Mongo has been guilty many times of misleading marketing practices. I wouldn't place my trust in this kind of entity.

Instead, use and learn:
- Very well known and tested databases where experts have verified their behavior properly so that you don't have to worry about unexpected surprises in production. PostgreSQL and MySQL are both good choices for this reason.
- SQLite is also OK to stick with, but you might have to think about extra things like restoring the DB from a backup if the server dies. I'd advise just using any "database as a service" provider like Amazon RDS and picking Postgres/MySQL as I mentioned - Amazon has a free tier, handle automated backups and persisting data for you so that you don't have to worry about it.

Hey @ben, Thanks for the detailed replied. Its very useful. I've made up my mind, and I want to stick with sqlite for now. Until, I've something good. I want easy things to get started and want to convert my ideas into reality.

Anyways, thanks for the help.

That is the reason I historically stayed away from Mongo but they’ve improved and either way, I need a document store (jsonb in PG won’t do).

Newbes can use Mongo, Marc Lou has built 20 startups on Mongo and makes 300k. He was very novice when he started.

Fair play if they've fixed the issues

I'm curious what kind of use case you have where jsonb won't work considering Postgres typically benchmarks well (ironically, better than Mongo) when dealing with jsonb data

And yeah I'm not saying it's not possible. You can be successful with any DB choice really and Marc's use cases work fine with Mongo (I don't know all of his apps, but most of the ones I've seen either don't have persistence requirements or have pretty simple data models that can basically be stored in anything including Mongo), but I typically don't recommend Mongo to new people because I've seen a lot of folks shoot themselves in the foot trying to force relational data into a document format just because they don't know any better

What I mean by that is that it's really easy to look at Mongo if you don't have a lot of experience and think "oh yeah, I can represent everything I could ever hope to store in a database using a JSON doc" even if there might be relationships between entities that are better off in a typical RDBMS and not a document DB. I mean, I've seen people do wild shit like attempt to represent an entire social graph for a user in a mongodb document. That's where the danger is in my view.

Why I had to go to Mongo: I really needed something that was "json first". I got 3000 different fields/keys, but my largest documents only have 100 fields at most. So a huge number of the fields only appear in a sub-set of the documents.

True Marc's use cases have simple requirements, but that is true for most newbies. In a way I'd say Mongo is better for newbies doing simple stuff than experienced engineers building complex apps...

Lol, @marc is this a feature, or a bug? Three consequitive dashes seems to make text an MD header, or similar?

Wonder if it's just the Markdown parser doing that

Even works with two dashes, from what I can see. Maybe even one.

Yeah, seems to be there is a case of MD injection here, which if true, might be possible to lead to an MDX injection, at which point your cookies are mine ;-)

That was a Markdown feature we shouldn't support. Fixed now

Pretty interesting discussion going on here!

I’ve been using Mongo (via Mongoose) for a while and I tried SQLite recently.

I like SQLite for the standardised way of building databases. With MongoDB, there’s no real best practice and everything is open to interpretation

Yes exactly. And sometimes your data looks exactly like that, which makes it hard to retrofit into SQL.

It does. Best not to change database types midway into the project anyway