在 laravel/lumen 中创建临时 table 并插入数据

Creating Temporary table in laravel/lumen and insert data

我想在 Laravel/Lumen 中创建临时 table 并且我制作了这样的架构。

Schema::create('temp_image', function (Blueprint $table) {
    $table->increments('id');
    $table->string('link');
    $table->timestamps();
    $table->temporary();
});

当我运行php artisan migrate我看到...

Migrating: 2017_11_25_165640_create_temp_table
Migrated:  2017_11_25_165640_create_temp_table

...但它没有创建任何 table。发生了什么?

Temporary tables 是基于会话的。它不是在 SQL Server 上创建的。您可以查看 laracast 中的 this 文章。

临时 table 也可以在 lumen 中使用。我们可以使用 Schema Buildercreate table 和 drop table。 假设,我们有一个用于简单请求的函数。我们可以像下面这样使用 temporary table-

public function temporary_check()
{
    Schema::create('temp_message', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('sender_id');
        $table->integer('receiver_id');
        $table->string('message');
        $table->timestamps();
        $table->temporary();
    });

    DB::table('temp_message')->insert(['sender_id'=>2,'receiver_id'=>3,'message'=>'message temp check']);

    $data = DB::table('temp_message')->get();

    Schema::drop('temp_message');

    return $data;
}

由于 Temporary Table 是基于 session 的,因此您应该始终在 dropping table 之前 free-up memory 完成工作.

A​​ temporary table 是一种特殊的 table 类型,它允许您存储一个临时结果集,您可以在单个会话。因此,如果您试图找到它们,您可能无法找到它们,因为它们是基于会话的。

MySQL 在会话结束或连接终止时自动删除 临时 table

您应该记住,这些 table 是在没有索引的情况下创建的,因此如果您的目标是提高查询速度,通常需要在创建 table 之后添加索引。

您可以阅读更多相关信息:http://www.mysqltutorial.org/mysql-temporary-table/