From 92cca8a4203ad193faad92661aeb33a6dacf8c8f Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 25 Nov 2026 08:49:06 +0800 Subject: [PATCH] =?UTF-8?q?perf(db):=20bep=5Forder=20=E5=8A=A0=20(status,t?= =?UTF-8?q?rade=5Ftype)=20=E5=A4=8D=E5=90=88=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 动机:转账/回调任务反复跑 WHERE status=? AND trade_type IN (...),原来只有单列索引。 10 万行实测(同机 mysql:8.0,1 核): IN 两个值: 21.5ms → 0.9ms IN 单值: 25.4ms → 16.0ms COUNT: 11.4ms → 1.6ms 空表/小表无感,订单积累后差距明显。AutoMigrate 会在新库上自动建同名索引。 顺带:示例 compose 的小机器参数注释更新(buffer pool 256M、慢日志 1s) --- app/model/order.go | 4 ++-- docker-compose.mysql.example.yml | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/model/order.go b/app/model/order.go index 460a24d..7766e3d 100644 --- a/app/model/order.go +++ b/app/model/order.go @@ -65,7 +65,7 @@ type Order struct { Id OrderId string `gorm:"column:order_id;type:varchar(128);not null;index;comment:商户ID" json:"order_id"` TradeId string `gorm:"column:trade_id;type:varchar(128);not null;uniqueIndex;comment:本地ID" json:"trade_id"` - TradeType TradeType `gorm:"column:trade_type;type:varchar(20);not null;index;comment:交易类型" json:"trade_type"` + TradeType TradeType `gorm:"column:trade_type;type:varchar(20);not null;index;index:idx_order_status_trade,priority:2;comment:交易类型" json:"trade_type"` Fiat Fiat `gorm:"column:fiat;type:varchar(16);not null;index;default:CNY;comment:法定货币" json:"fiat"` Crypto Crypto `gorm:"column:crypto;type:varchar(16);not null;index;default:USDT;comment:加密货币" json:"crypto"` CurrencyLimit string `gorm:"column:currency_limit;type:varchar(255);not null;default:'';comment:限定币种" json:"currency_limit"` @@ -76,7 +76,7 @@ type Order struct { FromAddress string `gorm:"column:from_address;type:varchar(128);not null;default:'';comment:支付地址" json:"from_address"` MatchAddress string `gorm:"column:match_address;type:varchar(128);not null;default:'';comment:校验地址" json:"match_address"` AddressLocked bool `gorm:"column:address_locked;not null;default:false;comment:地址锁定 1:独占 0:共享" json:"address_locked"` - Status int `gorm:"column:status;not null;default:1;index;index:idx_order_notify_retry,priority:1;comment:交易状态" json:"status"` + Status int `gorm:"column:status;not null;default:1;index;index:idx_order_notify_retry,priority:1;index:idx_order_status_trade,priority:1;comment:交易状态" json:"status"` Name string `gorm:"column:name;type:varchar(64);not null;default:'';comment:商品名称" json:"name"` ApiType string `gorm:"column:api_type;type:varchar(20);not null;default:'epusdt';comment:API类型" json:"api_type"` ReturnUrl string `gorm:"column:return_url;type:varchar(1024);not null;default:'';comment:同步地址" json:"return_url"` diff --git a/docker-compose.mysql.example.yml b/docker-compose.mysql.example.yml index 60ba9f7..aa29919 100644 --- a/docker-compose.mysql.example.yml +++ b/docker-compose.mysql.example.yml @@ -26,9 +26,11 @@ services: - --collation-server=utf8mb4_unicode_ci - --default-time-zone=+08:00 # 小机器(1~2G 内存)建议打开下面三行,省内存也省 CPU - # - --innodb-buffer-pool-size=96M + # - --innodb-buffer-pool-size=256M # - --performance-schema=OFF # - --disable-log-bin + # - --slow-query-log=ON # 慢查询落 ./mysqldata/-slow.log + # - --long-query-time=1 environment: MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:?先把 DB_ROOT_PASSWORD 写进 .env} MYSQL_DATABASE: bepusdt