mysql處理大數據合併的另外一種方法

在作項目的時候我碰到了一個這樣的場景,有兩張表,數據都在300W左右,如今要求在生產環境中合併這兩張表爲一張表,原本想用sql語句insert into select來實現,後來發現這樣操做速度很慢,由於裏面有計算邏輯。大概的表結構以下:sql

table1it

id tel credits1table

 

table2select

id tel credits2 pointssql語句

 

合併後的表爲方法

table命名

id tel credits points數據

 

其中credits = credits1 + credits2項目

 

我採用的實現方法是這樣的:tab

 

第一步、先建一個視圖。

名爲table

sql語句大體以下:

select table2.id,table2.tel,table1.credits1 + table2.credits2 as credits, table2.points from table2 left join table1 on table1.tel = table2.tel

 

上線運行一天左右,基本沒問題以後。找個運行空閒期,關閉項目。

 

第二步、導出視圖中的數據。

第三步、重命名視圖名稱爲table_copy。

第四步、新建表table,字段爲id tel credits points

第五步、把視圖中的數據導入到table中。

相關文章
相關標籤/搜索