PHP - Application with multiple databases - php

I am currently working on a large application that will be used by many people each having a large amount of data. I thought to manage these multiple users through a single database but I am asked to create a separate database for each new user that is registered. Now what I am wondering is : is it a good idea to do so i.e. having the app create a separate database for each new user that gets registered and manage it's data through it? What will be the performance issues, if any?

Separate DB for each new registered user might be bad idea. You can do it like this;
Put 100 users in each separate db. 1-100 => DB1, 101-200 => DB2, n, n+100 => DBn
You can keep a table for which id interval will be connect to which db. By doing this, you can lower db load. 100 users for each db is just an example. You need to use such a structure for a system that has lots of users.

My answer is create a separate database for each new user is a wonderfool idea.you must set appropiate indexes over the table and you will get good performance

Related

Laravel: get records from multiple schemas

I am building a SaaS application that has a set of requirements. One of these requirements is that the application has to be multi-tenant with a seperate schema for every tenant. But there has to be a single instance (admin interface) that needs to access all those schemas for data validation.
Example:
We have x customers who will access their application through customerx.our-company.com. Every customer gets its own schema with multiple tables. These tables contain users, invoices, contracts, timesheets,... The customer has to be able to view/modify these records.
We as a company providing the service have to be able to get all the timesheets from all those customers to be able to verify them. Once verified these records will be updated. 'verified' => true
My first idea was to store a tenant_id in a customers table in the admin database to be able to change the database connection in eloquent and merge the records before returning them. This is doable, but I have some questions about performance. What if we grow to 1000+ customers. getting all those records could take a long time.
Dumping all the records in a single schema without providing a separate schema per customer cannot be done because of GDPR. The data of the customer has to be separated.
What would be the best approach for this design wise? Are there some good articles about this subject?
What I began to think while writing this question: It is possible to leverage c++, I can create a custom PHP extension that takes multiple tenant database info to get the records via async tasks std::async, merge those records and return them. std::async creates multiple threads in a pool and I can wait until it is finished. The only big deal will be relations that need to be returned.
Updating those records is a simpler task as we know where it is stored (customer_id) so we can change the DB connection on the fly while updating.
EDIT: I created a simple c++ program that creates async MySQL connections. I've tested this with 10 schemas at once retrieving 1000 records from each. This looks promising, will keep this updated.

PHP / MySQL Database Synchronization

Scenario: I have a project which reads data from multiple different databases on different hosts with read-only access. All of them store the same type of information (for demo: Users).
Every database stores the information in their own way (different table names, column names, types etc.)
Now I want to get every User from every database and insert them into my own table 'User'.
Currently I am doing this by having 2 cronjobs - 1 imports Users & 1 updates Users (Select from Read-Only DB and insert into own DB the way I want).
Now I don't really like the idea of having multiple cronjobs running, just to import/update Users - plus I want to have the newest data in my database all the time - which means I need to run the cronjobs atleast every minute.
This with multiple tables are way too many minute-cronjobs IMO.
Question:
Is there any way to achieve the same thing easier? Or is this already the correct way to do so?
Next issue is, that there is in most case no information which data is updated or not - meaning I need to import the same data over and over just to have the newest.
Any help will be gladly appreciated.
Using PHP 7+, Mysql with Symfony & Doctrine - if that helps.

Must I have a common DB for all users active sessions?

Here I come again ;)
I am doing an application where each user will have their own DB.
Is it ok if I store session for each user in their individual DB? Or is it for some reason convenient to have active sessions in a common DB for all users?
Sorry about my question, I am kind of new to this level. :) I am working with PHP and MySQL, if that makes any difference, although I thik the question is language independent.
In a typical application, there will only be one database with several tables, where each table can have several records.
Sessions
You can just save sessions the same way you would add a record to database.
Profile Details / Friendship
This is where relationships take place.
Consider the image below. Credits to the owner on w3stack(dot)org.
Focus and try to study on the three tables above: Users, Friendships, Friends(virtual table). Ignore the virtual table concept for now, so you will not be much confused.
It is really a BAD, and I mean BAD approach to create individual databases for each users. What if you thought of adding a "following" and "follower" feature to your application? You would need to add another table, and re-add all those friends from another db. If UserA will have 100 friends with each database, you wouldn't want to query all those 100 databases.
To end, just use a single DB, and identify relationships according to your application features. It is important to plan your structure before you actually apply it on hands-on. Happy coding!

Add user registration and subscription to a PHP MySQL web app

I've been working on a web app for a few months now. It's a PHP and MySQL driven database app which relates objects between each other.
I'd like to add functionality so that someone could register to use it and set up a monthly subscription. When they log in the app would simply populate with data from their own database.
I've done some scouring online but I'm struggling to find a starting point for adding this sort of feature.
The app relies on half a dozen tables within a database. So I'm also not sure if creating an individual database per user is practical.
Creating a db per user is very rarely the way to go - it's complicated and has very few benefits with lots of drawbacks (eg maintaining multiple simultaneous Db connections as most libraries only connect to a single Db). What you really need to do is create a user table and then tag various records with a UserId.
When a user registers, create a new User record, then create their new entries in other tables all referencing that Id. (Actually, only the "root" of each relational object needs a UserId eg If you have Order and OrderItems only the Order table needs a UserId)
Without more detail, it's impossible to help further.
Incidentally, the only time you should consider db-per-user is if each user requires unique tables (in which case your app would have to be very complex to actually use the data) or if there are security concerns re: storing data together (more likely with multiple clients than multiple users).

Dynamically-naming databases with PHP/SQL

Hey there guys and gals. I'm very new to php and am following various tutorials, reading books, watching videos etc.
The reason I'm learning is to create one specific web application, as well as to make that jump from simple geek to proper nerd, of course.
So far I've managed to learn most of what I need to create this web app.
The key part that has thus far eluded me is creating a dynamically-named, pre-defined(structure) database.
Essentially the application is a giant booking system. When a user registers I want the system to create a new database and link it to their account.
Whilst I know that I could easily have a php file that could run some SQL to create a database with all the right tables and columns, I don't know how to give that new database a unique name.
That name also needs to be written into the main users table so that whenever a user name connected to that client's account logs in, it uses that database name in the connection string and pulls up their data. Though, I'm sure that part wouldn't be as complicated.
If it matters, I'm using MySQL. Any help would be greatly-appreciated.
Edit: I should've made clear why I need more than one database in the first place, my apologies.
Essentially, it's going to be a private calendar(of sorts) system for businesses. Because of that, there will be an 'owner' of each database and all employees of that person will be utilising their employer's database.
If you need to create a new database for each user, I'd suggest radically rethinking your approach to the problem.
Very few problems require going that far.
For a booking system, for instance, I would imagine you would need one database with the following tables:
A user table with the user information for each user
A hotel table (if it's hotel booking, substitute what you need) with information on the hotels-
A booking table that links each booking to a user, a hotel and a time.
Edit:
An example of a problem for which it would be suited would be a meta-booking system; as in a system where you could set up a booking system for your own site or whatever.
If that is what you need, ignore this.
Find something unique about the user, like its username, and prefix the new database name with it. You can concatenate the number of databases already assigned to that user when creating a new one, so for example my first database would be inerte_0, my second one, inerte_1. Don't forget to sanitize whatever you'll use to prefix the database name to check if it's actually composed of allowed characters in Mysql's database names!
This is certainly possible. You could, for example, call uniqid() and then check for an existing database by that name in case you happen upon a duplicate (though that's unlikely).
However, I'm extremely wary of your overall approach. In general, you should not have to create tables (excluding TEMPORARY tables) at runtime. Instead, you could put data for all organizations in a single database, but with a simple column to distinguish which records are associated with which users.
It sounds like you're using a centralized database to store some of the information - the database wherein the "main users table" you mention resides. You also mention that you can identify which client a user is associated with, which implies a clients table in that centralized database.
That clients table very likely has a primary key field. There is your unique identifier for each client's database. You can use that, or you can generate a hash of some sort using a combination of information from that row, something like:
$unique = md5( $client_id . $client_name . $date_created );
You can also make sure that the column holding the client database names is set to be unique.

Categories