auto_trade_sys/backend/database/cleanup_non_system_trades.sql
薇薇安 a88e114b4c 1
2026-02-14 17:20:34 +08:00

24 lines
1.1 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 清理「非交易系统下单」的交易记录(无开仓订单号的记录)
-- 本系统开仓会在成交后保存 entry_order_id无该字段或为 0 的为同步补录/其它来源,可安全删除。
-- 执行前请先备份数据库或至少备份 trades 表。
-- 若表结构较旧、没有 entry_order_id 列,请先执行 add_order_ids.sql 或跳过本脚本。
-- 1) 查看将要删除的记录数(按账号)
SELECT account_id, status, COUNT(*) AS cnt
FROM trades
WHERE entry_order_id IS NULL OR entry_order_id = 0
GROUP BY account_id, status
ORDER BY account_id, status;
-- 2) 查看将要删除的总数
SELECT COUNT(*) AS will_delete FROM trades
WHERE entry_order_id IS NULL OR entry_order_id = 0;
-- 3) 确认无误后执行删除建议先备份mysqldump -u user -p db_name trades > trades_backup.sql
-- DELETE FROM trades
-- WHERE entry_order_id IS NULL OR entry_order_id = 0;
-- 若只清理指定账号,可加上条件,例如:
-- DELETE FROM trades
-- WHERE (entry_order_id IS NULL OR entry_order_id = 0) AND account_id = 1;