MySQL日志类的数据表,实测MyISAM到底比InnoDB查询快多少

目录

简单介绍下两种引擎

开始实测

从数据占用的容量对比看MyISAM占用空间更小

从查询效率对比看MyISAM也确实更快

 MYISAM表结构

 Laravel的建表语句

 Laravel定时任务-清理日志,只保留最新300万条记录


简单介绍下两种引擎

  • InnoDB支持事务功能、外键、行级锁等一些关系数据库的高级功能。
  • MYISAM的性能更优,占用的存储空间少。

两种存储引擎各有优劣,怎么选择要视具体应用而定。

日志类的表只有添加和查询两种操作,业务简单,当然是要用MyISAM更合适。

下面测试下查询和存储方面具体相差多少

开始实测

数据准备:

  1. InnoDB和MyISAM各创建一张表,分别导入十万条数据  
  2. 给`sql`字段创建全文索引

MYISAM表结构: 

1CREATE TABLE `api_request_log` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned DEFAULT '0' COMMENT 'admin_id',
  `user_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT 'admin_name',
  `method` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '接口请求方式',
  `path` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '接口地址',
  `params` varchar(5000) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '请求参数',
  `result` varchar(5000) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '返回结果',
  `exec_time` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '接口用时,单位秒',
  `sql` text COLLATE utf8mb4_unicode_ci COMMENT '执行的sql',
  `ip` varchar(15) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '客户端IP',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `api_request_log_method_index` (`path`),
  FULLTEXT KEY `api_request_log_params_fulltext` (`params`),
  FULLTEXT KEY `api_request_log_sql_fulltext` (`sql`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='接口请求记录';

 innodbdb表的实际数据和myisam表的数据是一样的,下图中显示的不准确

  1. 从数据占用的容量对比看MyISAM占用空间更小
  2. 从查询效率对比看MyISAM也确实更快

 Laravel的建表语句

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateApiRequestLogTable extends Migration
{
    public function up()
    {
        Schema::create('api_request_log', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable()->default(0)->comment('admin_id');
            $table->string('user_name',50)->nullable()->default(0)->comment('admin_name');
            $table->string('method',10)->nullable()->default('')->comment('接口请求方式');
            $table->string('path')->nullable()->default('')->comment('接口地址')->index();
            $table->string('params',5000)->nullable()->default('')->comment('请求参数')->fulltext();
            $table->string('result',5000)->nullable()->default('')->comment('返回结果');
            $table->string('exec_time',10)->nullable()->comment('接口用时,单位秒');
            $table->text('sql')->default('')->nullable()->comment('执行的sql')->fulltext();
            $table->string('ip',15)->default('')->nullable()->comment('客户端IP');
            $table->timestamp('created_at')->useCurrent()->comment('创建时间');

            // $table->index('path');
            $table->engine = 'MyISAM';
        });
        DB::statement("alter table `api_request_log` comment '接口请求记录'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('api_request_log');
    }
}

Laravel定时任务-清理日志,只保留最新30万条记录

<?php

namespace App\Console\Commands;

use App\Models\ApiRequestLog;
use Illuminate\Console\Command;

class CleanApiLog extends Command
{
    protected $signature = 'api-log:clean {limit=300000}';
    
    protected $description = '清理接口请求记录表数据 可选参数limit来设置保留最新多少条数据(默认值是30万) ';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle(){
        // 获取参数 保留多少条数据
        $limit = $this->argument('limit');

        $delete_count = $last_id = 0;

        // 获取最新数据的ID
        $last_id = ApiRequestLog::query()->latest()->value('id');
        if ($last_id){
            $end_id = $last_id - $limit; // 获取截止的ID
            if ($end_id > 0){
                $delete_count = ApiRequestLog::query()->where('id', '<', $end_id)->delete();
            }
        }
        $msg = '清理接口请求记录表数据:执行完成 limit='.$limit.',last_id='.$last_id.',delete_count='.$delete_count;
        info($msg);
        return $this->info($msg);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值