refactor: enhance Optuna backtesting framework, optimize orderbook filtering, and update database management utilities.
This commit is contained in:
68
scratch/delete_old_data_pymysql.py
Normal file
68
scratch/delete_old_data_pymysql.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import sys
|
||||
import time
|
||||
import pymysql
|
||||
|
||||
conn = pymysql.connect(
|
||||
host='192.168.0.141',
|
||||
port=3306,
|
||||
user='jae',
|
||||
password='1234',
|
||||
database='kis_quant_db',
|
||||
cursorclass=pymysql.cursors.DictCursor,
|
||||
autocommit=True
|
||||
)
|
||||
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SHOW TABLES")
|
||||
tables = [list(r.values())[0] for r in cur.fetchall()]
|
||||
|
||||
target_prefixes = (
|
||||
'ws_ticks', 'ws_orderbook', 'ws_candles',
|
||||
'ls_ws_ticks', 'ls_ws_orderbook', 'ls_ws_candles',
|
||||
'ws_price_validation'
|
||||
)
|
||||
|
||||
for table in tables:
|
||||
if not table.startswith(target_prefixes):
|
||||
continue
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(f"SHOW COLUMNS FROM {table}")
|
||||
cols = [r["Field"] for r in cur.fetchall()]
|
||||
|
||||
time_col = None
|
||||
for candidate in ('tick_time', 'ob_time', 'candle_time', 'timestamp', 'recv_ts', 'datetime'):
|
||||
if candidate in cols:
|
||||
time_col = candidate
|
||||
break
|
||||
|
||||
if time_col:
|
||||
cutoff = '20260808'
|
||||
|
||||
with conn.cursor() as cur:
|
||||
count_q = f"SELECT COUNT(*) as cnt FROM {table} WHERE {time_col} < %s"
|
||||
cur.execute(count_q, (cutoff,))
|
||||
count = cur.fetchone()['cnt']
|
||||
|
||||
if count > 0:
|
||||
print(f"Deleting {count} rows from {table} (using column {time_col})")
|
||||
deleted = 0
|
||||
while True:
|
||||
with conn.cursor() as cur:
|
||||
del_q = f"DELETE FROM {table} WHERE {time_col} < %s LIMIT 100000"
|
||||
cur.execute(del_q, (cutoff,))
|
||||
rows_affected = cur.rowcount
|
||||
|
||||
if rows_affected <= 0:
|
||||
break
|
||||
deleted += rows_affected
|
||||
print(f" ... deleted {deleted}/{count} rows in {table}")
|
||||
time.sleep(0.01)
|
||||
print(f"Finished deleting from {table}.")
|
||||
else:
|
||||
print(f"No rows to delete in {table} before {cutoff}")
|
||||
else:
|
||||
print(f"Warning: No time column found for {table}, columns: {cols}")
|
||||
finally:
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user