English 中文(简体)
Multiple application instances on the same database
原标题:

I m writing an application that that I m going to provide as a service and also as a standalone application. It s written in Zend Framework and uses MySQL.

When providing it as a service I want users to register on my site and have subdomains like customer1.mysite.com, customer2.mysite.com.

I want to have everything in one database, not creating new database for each user.

But now I wonder how to do it better. I came up with two solutions: 1. Have user id in each table and just add it to WHERE clause on each database request. 2. Recreate tables with unique prefix like customer1_tablename , customer2_tablename .

Which approach is better? Pros and cons? Is there another way to separate users on the same database?

Leonti

最佳回答

I would stick to keeping all the tables together, otherwise there s barely any point to using a single database. It also means that you could feasibly allow some sort of cross-site interaction down the track. Just make sure you put indexes on the differentiating field (customer_number or whatever), and you should be ok.

If the tables are getting really large and slow, look at table partitioning.

问题回答

It depends on what you intend to do with the data. If the clients don t share data, segmenting by customer might be better; also, you may get better performance.

On the other hand, having many tables with an identical structure can be a nightmare when you want to alter the structure.

I d recommend using separate databases for each user. This makes your application easier to code for, and makes MySQL maintenance (migration of single account, account removal and so on.)

The only exception to this rule would be if you need to access data across accounts or share data.

This is called a multi-tenant application and lots of people run them; see

multi tenant tag

For some other peoples questions





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签