Laravel - Multiple Databases at Once [duplicate] - php

This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed 5 years ago.
I am creating a Laravel/latest-version application which requires several hundreds of sql table to be created to serve the job. There will again be hundreds of clients who's data will be inserted multiple times (1000 rows per client or more in some tables) in these table. To eliminate confusion, I want a general/common-database for common information and separate Database for each client for specific information and would like to create new DB when a client is added. Can this be done in Laravel (dealing with multiple Databases at once) or is there any other PHP framework which can serve this purpose better? Much obliged for your help.

Well most of the times, this is an architectural flaw because the db isn't designed well. Normally this kind of issue is faced due to the reason that all your tables are normalized. Normalization is good for small applications but for data rich applications you have to judge which fields need to be normalized and which doesn't need normalization. You can save lots of tables and fields with denormalization. As a rule of thumb anything that you dont have to query directly or via join can be denormalized.
Anyways it is possible to have multiple databases attached to a single laravel application. I have done this once on a huge application.
So the idea is that before every eloquent query you will have to set the db connection.
$someModel->setConnection('mysql2');
You can find more information about it here

Related

What are the benefits of using separate hosts for read and write in mysql (laravel)? [duplicate]

This question already has an answer here:
Master/Slave Mysql Architecture vs Server/Read DB and Separate DB for writes only [closed]
(1 answer)
Closed 3 years ago.
As laravel document says:
Sometimes you may wish to use one database connection for SELECT statements, and another for INSERT, UPDATE, and DELETE statements. Laravel makes this a breeze, and the proper connections will always be used whether you are using raw queries, the query builder, or the Eloquent ORM.
What is the benefit of doing this and what are the tradeoffs?
As far as I'm aware of, this is normally done if you have a very read-heavy application. In such a case you can have a master-slave database system which performs internal replication in order to provide more hardware power to feed the application. The slave can be a machine optimized for reading while the master is optimized for writing, although I've to admit that this goes far beyond my expertise.
There are also alternatives for load balancing SQL connections, for example using HAProxy.

what is the best approach for designing a php mysql user login system with three different access level? [duplicate]

This question already has answers here:
How to manage User Roles in a Database?
(5 answers)
Closed 7 years ago.
I am a beginner with php and mysql. I am going to develop a web application which has three types of users, namely admins, staff and customers. Each have restricted access to differe parts of the application. Should I create a table for each group of users or just one table with a column specifying user type? Which approach is better? Is there other options? And at last, I really prefer to have one single login page for all three types of users.
Personally I would prefer different tables and use also different login fields on your login page (or check every login in every table until you find the right one). This is easier if you have to connect tables and I.e. admins can do things, that causes links between different tables than those staff members could cause.
On the other hand the login process would be a little bit easier if it's in one table.
I've done a project, that sounds very similar to yours. Maybe it could help you sometimes if you have a look at it. You can find it at GitHub.
Good luck with your project :)

Web app - one db per customer or one for all of them? [duplicate]

This question already has answers here:
Single or multiple databases
(5 answers)
Closed 7 years ago.
I have to make an email marketing app for one of my clients and in this app each customer will need to manage around 100k average for their list of contacts.
So, for each customer i should create a db and replicate the schema or combine all customers in a single db. For example: All the contacts in one table and difference them with the customer_id
I want to know what is the best recommendation to make a web app where each customer will need to manage a lot of registers.
Also is possible that some customers (big companies) require a private installation of the app.
Please tell me your experience in this kind of app.
Thank you.
My recommendation is to use one fast, relational database so the data is centralized and ready to query. I would further recommend Postgres, as it has nice features like UID's, audit triggers, and table partitioning.
Keeping all clients and customers in the same database will also give you the opportunity to easily perform big data analysis on trends throughout your clients. Compare that to running analytics for each customer on an individual database..that would be a nightmare.
Postgres also has a convenient way of organizing your tables into "schemas". You could have a schema for your clients and all client related information, a schema for your customers and all customer related information, a schema for billing information, a schema for you data analytics results, etc.
A shared centralized database is almost always the best route. It is a nightmare to try to handle even a moderate number of customers separately. Think of code updates, patches, fixes, etc.. Databases these days are powerhouses and can easily handle the number of records you are referring to so don't worry about that. I have been building these types of systems since the late '90s and have pondered this exact situation more than once. Single, shared DB for sure. Best of luck!

Multiple SQL tables for each user? [duplicate]

This question already has answers here:
Is it unreasonable to assign a MySQL database to each user on my site?
(10 answers)
Closed 8 years ago.
I have a small file hosting website that I am trying to store the files that users upload in a database. The issue that I am having is that I cannot decide which method would be better:
To store all users in one table
create a new table for each user.
I understand that the second method will slow performance but by how much? I am planning on having 1000+ users eventually. The issue with the first method is listing the files back to the user. What method should I go with and which one would be the most efficient?
Short answer:
No. Use the simplest thing that works: A single table.
Long answer:
You'll know what kind of scaling problems when you have a production system under production loads, and then you can analyze where your bottlenecks are and develop a sharding strategy based on real-world use cases and not hypotheticals.
Right now you're just guessing, and you'll probably guess wrong. Then you're stuck with an awful database structure you'll find impossible to undo.
Try not to store actual files in the MySQL database, this almost always leads to horrible disaster, but instead store them on the filesystem and keep references to them in the database. If you're going to be managing a lot of files, heaps and tons of them, you may want to look at document store database like Riak to help with that.
I suggest creating a table for each entity and having a correct relationship between them.
For example:
Users table will have user_id, user_name, etc.
Files table will have id, url, user_id
In this case, the relationship is created by having the same user_id. So when you upload a file, you attach the user_id to the image.
That means - no, don't create a separate table for each user. Go with method 1 and make sure each important entitiy has its own table.
Down the road, you will probably have more entities and more tables such as Permission, Products, etc etc. By using SQL queries you will be able to get all the data you want.
Hope this helps!
Having 1000 ish users is not a problem for MySQL. But tadman is rigth, save the files on the filesystems instead of the database.
If you know that you will endup with millions of users, I suggested that you read on how Facebook or others big users related sites handle this scalling problems.

Advice on database structure and performance needed [duplicate]

This question already has answers here:
Should I use a single or multiple database setup for a multi-client application? [closed]
(9 answers)
Closed 9 years ago.
I'm currently working on a site which will offer a blog for users to write.
My question relates to how to structure the database.
So all of you know how blogs are working. Some user posts an entry and the system puts it in the database. Now to the problem:
Is it better to have thousands of databases for each user and each blog? e.g. User A has a blog with its' entries and photos and got its own database "UserABlog" for it. User B, too, with the database "UserBBlog".
Or is it better to have a single database for all users which has a table for "entries" and "photos", etc. linked to each user via foreign keys? e.g. User A and B got their blogs and their entries and photos are saved in the database "blogs" in the tables "entries" and "photos" with foreign keys linked to User A, B and C.
Use one database.
TL;DR:
blogs do not generate much data
Blogs are simple
You can partition large tables
Compared to other applications which have to deal with real time data, workflows and similar stuff blogs produce only tiny amounts of data. Most databases are built to handle big amounts of data and will happily handle hundreds or thousands of blogs.
Blogs usually do not have to deal with complicated security settings and access rules so you can create a pretty good implementation even without dedicated databases.
If you database grows to big you can still partition the tables. http://www.mssqltips.com/sqlservertip/2888/how-to-partition-an-existing-sql-server-table/
I'd say go with the 1 database, it's all the same application. It's much harder to control and modify schema of tables if you have to manage 1 database per user.

Categories