시스템은 원격 데이터베이스를 사용합니다. 위치가 미국이라 연결속도가 엄청 느립니다. DBA는 데이터베이스를 안내하지 않습니다
데이터베이스 테이블 마이그레이션 방법을 직접 작성해야 했습니다. 하지만 한 번에 한 테이블씩 천천히 가져올 수 있습니다. PHP
<code><span><?php</span><span>//原数据库</span><span>$databaseOri</span> = <span>'原数据库名称'</span>; <span>$hostnameOri</span> = <span>'IP地址'</span>; <span>$portOri</span> = <span>'端口号'</span>; <span>$userOri</span> = <span>'用户名'</span>; <span>$passwordOri</span> = <span>'密码'</span>; <span>$csOri</span>=<span>"DATABASE=$databaseOri;HOSTNAME=$hostnameOri;PORT=$portOri;PROTOCOL=TCPIP;UID=$userOri;PWD=$passwordOri;"</span>; <span>$dbOri</span> = db2_connect (<span>$csOri</span>, <span>$userOri</span>, <span>$passwordOri</span>); <span>//目标数据库</span><span>$databaseDes</span> = <span>'要转移到的数据库的名称'</span>; <span>$hostnameDes</span> = <span>'IP地址'</span>; <span>$portDes</span> = <span>'端口号'</span>; <span>$userDes</span> = <span>'用户名'</span>; <span>$passwordDes</span> = <span>'密码'</span>; <span>$csDes</span>=<span>"DATABASE=$databaseDes;HOSTNAME=$hostnameDes;PORT=$portDes;PROTOCOL=TCPIP;UID=$userDes;PWD=$passwordDes;"</span>; <span>$dbDes</span> = db2_connect (<span>$csDes</span>, <span>$userDes</span>, <span>$passwordDes</span>); <span>//执行sql的方法</span><span><span>function</span><span>fetcher</span><span>(<span>$db</span>, <span>$query</span>,<span>$par</span> = array<span>()</span>)</span>{</span><span>$stmt</span> = db2_prepare(<span>$db</span>, <span>$query</span>); <span>$res</span>=<span>array</span>(); <span>if</span>(<span>$stmt</span>) { <span>//print_r($stmt);</span><span>$ex</span> = db2_execute(<span>$stmt</span>,<span>$par</span>); <span>if</span>(<span>$ex</span>) { <span>try</span>{ <span>while</span>(<span>$row</span> = db2_fetch_assoc(<span>$stmt</span>)) { array_push(<span>$res</span>, <span>$row</span>); } }<span>catch</span>(<span>Exception</span><span>$e</span>){} }<span>else</span>{ print_r(<span>$query</span>); } } <span>return</span><span>$res</span>; } <span>//插入数据库的方法</span><span><span>function</span><span>insertIntoDes</span><span>(<span>$db</span>, <span>$query</span>,<span>$par</span> = array<span>()</span>)</span>{</span><span>$stmt</span> = db2_prepare(<span>$db</span>, <span>$query</span>); <span>$res</span>=<span>array</span>(); <span>if</span>(<span>$stmt</span>) { <span>$ex</span> = db2_execute(<span>$stmt</span>,<span>$par</span>); <span>if</span>(!<span>$ex</span>){ print_r(<span>$query</span>); } } <span>return</span><span>$res</span>; } <span><span>function</span><span>transferDB</span><span>(<span>$tableName</span>,<span>$dbOri</span>,<span>$dbDes</span>)</span> {</span><span>//拼出要转移的table的所有字段名</span><span>$tableCloums</span> = fetcher( <span>$dbOri</span>, <span>"select NAME from sysibm.syscolumns where tbname='"</span>.<span>$tableName</span>.<span>"'"</span>, <span>array</span>() ); <span>$tableParams</span> = <span>""</span>; <span>$insertParamsLength</span> = count(<span>$tableCloums</span>); <span>for</span>(<span>$temp</span>=<span>0</span>;<span>$temp</span><<span>$insertParamsLength</span>;<span>$temp</span>++){ <span>$tmpName</span> = <span>$tableCloums</span>[<span>$temp</span>][<span>"NAME"</span>]; <span>$tableParams</span> = <span>$tableParams</span>.<span>$tmpName</span>; <span>//print_r($tableCloums[$temp]["NAME"]);</span><span>if</span>(<span>$temp</span> < <span>$insertParamsLength</span> - <span>1</span> ){ <span>$tableParams</span> = <span>$tableParams</span>.<span>","</span>; } } <span>//找出table的主键</span><span>$resultKeyArray</span> = fetcher( <span>$dbOri</span>, <span>"select NAME from sysibm.syscolumns where tbname='"</span>.<span>$tableName</span>.<span>"' and keyseq = '1'"</span>, <span>array</span>() ); <span>if</span>(<span>$resultKeyArray</span>[<span>0</span>][<span>"NAME"</span>]){ <span>$keyCloum</span> = <span>$resultKeyArray</span>[<span>0</span>][<span>"NAME"</span>]; }<span>else</span>{ <span>$keyCloum</span> = <span>"ID"</span>; } <span>//获取表的所有行数,因为如果表太大有几百万行的话,一下全导入到内存中会照成内存溢出</span><span>$count</span> = fetcher( <span>$dbOri</span>, <span>"select COUNT(*) from public."</span>.<span>$tableName</span>, <span>array</span>() ); <span>$dataCount</span> = <span>$count</span>[<span>0</span>][<span>1</span>]; <span>//确定要执行几次</span><span>$executeloops</span> = floor(<span>$dataCount</span> / <span>10000</span> + <span>1</span>); <span>//$executeloops = 1;</span><span>//循环执行</span><span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span><<span>$executeloops</span>;<span>$i</span>++){ <span>$pageIndex</span> = <span>$i</span>; <span>$beginIndex</span> = <span>$i</span>*<span>10000</span>; <span>$endIndex</span> = (<span>$i</span>+<span>1</span>) * <span>10000</span>; <span>//通过分页查询,每次从原表中获取1万条数据</span><span>$getDataFromOriTab</span> = <span>"select "</span>.<span>$tableParams</span>.<span>" from ( select ROW_NUMBER() OVER(ORDER BY "</span>.<span>$keyCloum</span>.<span>" DESC) AS ROWNUM, "</span>.<span>$tableParams</span>.<span>" from public."</span>.<span>$tableName</span>.<span>" ) a where ROWNUM > "</span>.<span>$beginIndex</span>.<span>" and ROWNUM <= "</span>.<span>$endIndex</span> ; <span>//fwrite($myfile, $getDataFromOriTab);</span><span>$tmpData</span> = fetcher(<span>$dbOri</span>,<span>$getDataFromOriTab</span>,<span>array</span>()); <span>//拼接出insert语句中字段的value的值</span><span>$valueStr</span> = <span>""</span>; <span>foreach</span>(<span>$tmpData</span><span>as</span><span>$index</span> => <span>$row</span>){ <span>$valueStr</span> = <span>""</span>; <span>foreach</span> (<span>$row</span><span>as</span><span>$cloumName</span> => <span>$value</span>) { <span>if</span>(<span>empty</span>(<span>$value</span>)){ <span>$valueStr</span> = <span>$valueStr</span> . <span>"'',"</span>; }<span>else</span><span>if</span> (is_null(<span>$value</span>)) { <span>$valueStr</span> = <span>$valueStr</span> . <span>"null,"</span>; }<span>else</span>{ <span>$valueStr</span> = <span>$valueStr</span> . <span>"$value,"</span>; } } <span>$valueStr</span> = substr(<span>$valueStr</span>, <span>0</span>, -<span>1</span>); <span>$insertSql</span> = <span>"INSERT INTO public."</span>.<span>$tableName</span>.<span>" ("</span>.<span>$tableParams</span>.<span>") VALUES ("</span>.<span>$valueStr</span>.<span>");"</span>; insertIntoDes(<span>$dbDes</span>,<span>$insertSql</span>,<span>array</span>()); } } } <span>//参数为要导的表的表名</span> transferDB(<span>"表名"</span>,<span>$dbOri</span>,<span>$dbDes</span>); <span>?></span></span></code>').addClass('pre-numbering').hide();를 사용하여 작성되었습니다. $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
위에서는 db2 및 마이그레이션 측면을 포함하여 DB2 테이블 데이터 마이그레이션을 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.