1

私は現在、SaaS タイプのアプリケーションに取り組んでおり、マルチテナンシーのために、ユーザーごとに 1 つのデータベースに落ち着きました。各データベースには、ユーザーが資格を与えられている (支払った) 機能に必要なすべてのテーブルが含まれています。このアプリケーションは、データを取得して (つまり、Web 分析など)、それをユーザーに提示するように設計されています。設計は、(最終的には)数万のユーザーにスケーリングできる必要があります。

実行可能な解決策は、テーブルが必要であることをアプリケーションが検出したときに「動的に」テーブルを作成することでしょうか? それとも、特定のユーザー データベースに最終的に必要なすべてのテーブルを、それらが必要になる可能性があることがわかったらすぐに作成する必要がありますか (ユーザーのアップグレード、新機能など)。

私の現在のアーキテクチャでは、次のようなことができます。

function insertData($table, $data) {
    mysql_query("INSERT INTO ".$table." VALUES ('".implode("', '", $data)."')");
    if ( mysql_errno() ) {
      if (mysql_errno() == 1146) { // table does not exist
        if ( $this->createTable($table) ) {
          $this->insertData($table, $data)
        }
      } else {
        // other errors
      }
    }
}

テーブルを追加するためにすべてのユーザー データベースをループすることなく機能を追加できる柔軟性が欲しいので、上記のようなセットアップがそれを達成するのに役立ちます。しかし、後で決定を後悔するような何かが欠けているかどうかはわかりませんか?

4

1 に答える 1

0

Using an insert to test whether a table exists may not be your best bet, particularly considering the size of the row and latency to the database server. A better solution might be to use 'show tables', i.e.:

SHOW TABLES LIKE 'TABLENAME';

The resultset will be something like this:

Tables_in_DATABASENAME (TABLENAME)
----------------------------------
be_groups

If the number of rows is zero, clearly the table does not exist.

As far as your process is concerned -- I think I'd take a hybrid approach. When the customer upgrades and as part of your provisioning workflow create all the tables needed to provide the new services to the customer. That way the customer doesn't take up any more resources (disk mainly, in this case) than they're paying for, and you're not causing yourself the pain of provisioning dynamically.

于 2010-08-09T21:14:15.700 に答える