MariaDB / Laravel 5 存储标签的最佳引擎

MariaDB / Laravel 5 best engine for storing tags

您好,我想在数据库中存储标签。

搜索帖子将基于标签。 然后我正在寻找一些东西:一致性和性能 不幸的是,有外键或一些替代品会很好。 我尝试使用 Aria 引擎,但这不支持外键。

迁移文件如下所示:

    Schema::create('tags', function (Blueprint $table) {
        $table->engine = 'Aria';
        $table->increments('id')->nullable();
        $table->string('tag',100);
        $table->unique('tag');
    });

    Schema::create('post_tag', function (Blueprint $table) {
        $table->engine = 'Aria';
        $table->integer('post_id')->unsigned()->index();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        $table->integer('tag_id')->unsigned()->index();
        $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
        $table->primary(['post_id', 'tag_id']);
    });

我建议"tags"不需要FOREIGN KEYS。您将有一个地方可以插入标签;您将获得正确的代码,并且不需要一直进行昂贵的检查。

不用 CASCADING DELETE,只需在 Tags 中保留多余的行。请拼出你设想的SHOW CREATE TABLESELECTs。从这些,我们可以更好地讨论 first 架构的优点,并且仅 second 使用哪个引擎。

使用 INSERT IGNOREINSERT .. ON DUPLICATE KEY UPDATE .. 插入标签,不重复。

删除不再需要的标签更具挑战性; FKs 无法做到这一点。 (因此,另一个理由是简单地将它们留在原地。)

如果只有一个标签,可能不值得规范化标签。