diff --git a/backend/futures_trade_record/sql/03_create_deal_table.sql b/backend/futures_trade_record/sql/03_create_deal_table.sql index 5c57599..165da42 100644 --- a/backend/futures_trade_record/sql/03_create_deal_table.sql +++ b/backend/futures_trade_record/sql/03_create_deal_table.sql @@ -11,15 +11,30 @@ $$ LANGUAGE plpgsql VOLATILE; DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_deal = 'deal') THEN + IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'deal') THEN CREATE TABLE deal ( id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, record_id UUID NOT NULL, variety_id VARCHAR(50) NOT NULL, -- 品种id + variety_name VARCHAR(50) NOT NULL, -- 品种 contract VARCHAR(50) NOT NULL, -- 合约(如:2401) direction VARCHAR(20) NOT NULL CHECK (direction IN ('多', '空')), -- 限制合法方向 + open_year INT NOT NULL, + open_month INT NOT NULL, + open_day INT NOT NULL, + open_price NUMERIC(12, 6), + open_commission NUMERIC(10, 6) DEFAULT 0.00, + close_year INT NOT NULL DEFAULT 0, + close_month INT NOT NULL DEFAULT 0, + close_day INT NOT NULL DEFAULT 0, + close_price NUMERIC(12, 6) DEFAULT 0.00, + close_commission NUMERIC(10, 6) DEFAULT 0.00, variety_tick NUMERIC(12, 6), + close_tick NUMERIC(12, 6) DEFAULT 0.00, -- 平仓跳点 variety_tick_price NUMERIC(12, 6), + close_tick_profit NUMERIC(12, 6) DEFAULT 0.00, + total_commission NUMERIC(10, 6) DEFAULT 0.00, + close_profit NUMERIC(12, 6) DEFAULT 0.00, deleted BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP @@ -28,7 +43,22 @@ BEGIN BEFORE UPDATE ON "deal" FOR EACH ROW EXECUTE FUNCTION update_deal_modified_column(); - + + -- 1. 开仓日期索引(支持「未删除+开仓日期」查询) + CREATE INDEX idx_trade_open_date ON deal (deleted, open_year, open_month, open_day); + + -- 2. 品种+合约+方向组合索引(支持「未删除+品种+合约+方向」高频查询) + CREATE INDEX idx_trade_variety_contract_dir ON deal (deleted, variety_name, contract, direction); + + -- 3. 未平仓记录索引(支持「未删除+未平仓」查询) + CREATE INDEX idx_trade_unclosed ON deal (deleted, variety_name, contract) WHERE close_year IS NULL; + + -- 4. 平仓日期索引(支持「未删除+已平仓+平仓日期」查询) + CREATE INDEX idx_trade_close_date ON deal (deleted, close_year, close_month, close_day) WHERE close_year IS NOT NULL; + + -- 5. 盈亏排序索引(支持「未删除+已平仓+盈亏排序」查询) + CREATE INDEX idx_trade_profit ON deal (deleted, close_profit DESC) WHERE close_year IS NOT NULL; + RAISE NOTICE 'created deal table and trigger'; ELSE RAISE NOTICE 'deal table already exists'; diff --git a/backend/futures_trade_record/sql/04_create_open_table.sql b/backend/futures_trade_record/sql/04_create_open_table.sql deleted file mode 100644 index 6dc9a67..0000000 --- a/backend/futures_trade_record/sql/04_create_open_table.sql +++ /dev/null @@ -1,36 +0,0 @@ --- 切换到目标数据库 -\c postgres; - -CREATE OR REPLACE FUNCTION update_open_modified_column() -RETURNS TRIGGER AS $$ -BEGIN - NEW.updated_at = CURRENT_TIMESTAMP; - RETURN NEW; -END; -$$ LANGUAGE plpgsql VOLATILE; - -DO $$ -BEGIN - IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_open = 'open') THEN - CREATE TABLE open ( - id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, - record_id UUID NOT NULL, - year INT NOT NULL, - month INT NOT NULL, - day INT NOT NULL, - open_price NUMERIC(12, 6), - open_commission NUMERIC(10, 6) DEFAULT 0.00, - deleted BOOLEAN NOT NULL DEFAULT FALSE, - created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP - ); - CREATE TRIGGER update_open_updated_at - BEFORE UPDATE ON "open" - FOR EACH ROW - EXECUTE FUNCTION update_open_modified_column(); - - RAISE NOTICE 'created open table and trigger'; - ELSE - RAISE NOTICE 'open table already exists'; - END IF; -END $$; \ No newline at end of file diff --git a/backend/futures_trade_record/sql/05_create_close_table.sql b/backend/futures_trade_record/sql/05_create_close_table.sql deleted file mode 100644 index dcb4ada..0000000 --- a/backend/futures_trade_record/sql/05_create_close_table.sql +++ /dev/null @@ -1,36 +0,0 @@ --- 切换到目标数据库 -\c postgres; - -CREATE OR REPLACE FUNCTION update_close_modified_column() -RETURNS TRIGGER AS $$ -BEGIN - NEW.updated_at = CURRENT_TIMESTAMP; - RETURN NEW; -END; -$$ LANGUAGE plpgsql VOLATILE; - -DO $$ -BEGIN - IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_close = 'close') THEN - CREATE TABLE close ( - id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, - record_id UUID NOT NULL, - year INT NOT NULL, - month INT NOT NULL, - day INT NOT NULL, - close_price NUMERIC(12, 6), - close_commission NUMERIC(10, 6) DEFAULT 0.00, - deleted BOOLEAN NOT NULL DEFAULT FALSE, - created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP - ); - CREATE TRIGGER update_close_updated_at - BEFORE UPDATE ON "close" - FOR EACH ROW - EXECUTE FUNCTION update_close_modified_column(); - - RAISE NOTICE 'created close table and trigger'; - ELSE - RAISE NOTICE 'close table already exists'; - END IF; -END $$; \ No newline at end of file