mysql的日志迁移到mango_mysql 向mongo迁移备忘
由于之前涉及mysql分表,把用戶所關聯的各種數據,我們暫設定為abcd,代表四種業務
比如userid 為1 ,可能屬于 a1表,a2表,b3表,d4表。
現在要統一改為mongo
實現在user表中有一個abcd字段,類型是數組,分別存儲相當應的表明
比如 abcd [0=>a1,1=>b2,2=>c3]這樣。
思路也很簡單,分表找到數據庫屬于abcd的所有表,聯合查詢,取到表名和pid(person id),然后插入更新到mongo abcd字段即可。
以下是簡單實現:
$conn = mysql_connect("14.11.1.1", "d", "d2015");
mysql_select_db("dmp", $conn);//選擇MYSQL數據庫
mysql_query("set names utf8");//
$result = mysql_query("SHOW TABLES");
while ($row = mysql_fetch_array($result)) {
if (strstr($row[0], 'd_user_aaa_')) {
//$sql['aaa'][] = $row[0];
$aaa_sql .= "select '$row[0]' tbname,$row[0].pid from $row[0] union all ";
} elseif (strstr($row[0], 'd_user_ccc_')) {
$ccc_sql .= "select '$row[0]' tbname,$row[0].pid from $row[0] union all ";
} else if (strstr($row[0], 'd_user_ddd_')) {
$ddd_sql .= "select '$row[0]' tbname,$row[0].pid from $row[0] union all ";
} else if (strstr($row[0], 'd_user_ttt_')) {
$ttt_sql .= "select '$row[0]' tbname,$row[0].pid from $row[0] union all ";
}
}
$aaa_sql = rtrim($aaa_sql, "union all");
$ccc_sql = rtrim($ccc_sql, "union all");
$ddd_sql = rtrim($ddd_sql, "union all");
$ttt_sql = rtrim($ttt_sql, "union all");
// 可以統計下數據量大小
//echo "select count(*) from "."($ccc_sql)"." as nums";
// 測試 先來100條,一般這種情況在cli執行或者放在cron中執行,數據量大不適合在瀏覽器中跑,會超時。
//$aaa_sql .= " limit 100";
$ccc_sql .= " limit 100";
$res = mysql_query($ccc_sql);
$conn=new Mongo("mongodb://192.168.1.40:10001/test");
$collection=$conn->test->d_user;
while ($row = mysql_fetch_assoc($res)) {
$collection->update(array("pid"=>(int)$row['pid']),array('$addToSet'=>array('acdt'=>$row['tbname'])));
}
ps:myslq Union要進行重復值掃描,所以效率比較低。如果合并沒有刻意要刪除重復行,那么就使用Union All兩個要聯合的SQL語句 字段個數必須一樣,而且字段類型要(一致)因為這些表里就是存了一個pid和時間,mongo的addtoset去重我猜又比mysql快,所以用了union all;
總結
以上是生活随笔為你收集整理的mysql的日志迁移到mango_mysql 向mongo迁移备忘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语言五子棋无ai程序框图_微软多语言预训
- 下一篇: linux比较两个文件命令cmp,Lin