Преглед изворни кода

remove duplicates in `stock` and `player_record_%d`

xjc пре 5 месеци
родитељ
комит
70a7fca10b
2 измењених фајлова са 35 додато и 8 уклоњено
  1. 35 0
      src/tools/remove_duplicates.py
  2. 0 8
      src/tools/sync_stock_data.py

+ 35 - 0
src/tools/remove_duplicates.py

@@ -0,0 +1,35 @@
+# -*- encoding: utf-8 -*-
+import pymysql
+
+
+def main():
+    con = pymysql.connect(host='172.29.110.52',
+                          user='wanzb',
+                          password='wanzb890*()',
+                          database='wanzb')
+    with con:
+        # remove duplicate stocks
+        with con.cursor() as cur:
+            n_rows = cur.execute('delete from stock where name in (select name from (select name,count(name) as c from stock group by name having c>1) as t0) and code is NULL order by name')
+            print('{} stocks removed'.format(n_rows))
+        con.commit()
+
+        # remove duplicate player's records
+        with con.cursor() as cur:
+            # find target table
+            n_rows = cur.execute('show tables like "player_record_%"')
+            tables = [i[0] for i in cur.fetchall()]
+            tables.sort(key=lambda x: int(x[14:]))
+            target_table = tables[-1]
+
+            # remove duplicate entries in `player_record` util not fund
+            n_rows = -1
+            while n_rows != 0:
+                n_rows = cur.execute('delete from {} where id in (select mid from (select max(id) mid,user_id,stock_date,count(user_id) c from {} group by user_id,stock_date having c > 1) t0)'.format(target_table, target_table))
+                print('{} player_records removed'.format(n_rows))
+        con.commit()
+
+
+if __name__ == "__main__":
+    main()
+

+ 0 - 8
src/tools/sync_stock_data.py

@@ -60,15 +60,7 @@ def sync_stock_data():
         else:
             cm.Stock.objects.create(code=code, name=name)
 
-    # delete duplicate entry which code is null
-    with connection.cursor() as cur:
-        cur.execute("delete from stock where name in (select name from (select name,count(name) as c from stock group by name having c>1) as t0) and code is NULL order by name")
-
 
 if __name__ == "__main__":
-    print("start sync stock...")
-    st = time.time()
     sync_stock_data()
-    #cm.Stock.objects.filter(code__isnull=False).delete()
-    print("time cost:",time.time()-st)