search
HomeDatabaseMysql Tutorial用perl做数据库迁移,从MSSQL到MYSQL(三)

用perl做数据库迁移,从MSSQL到MYSQL(三)--V1.1版~多线程+handlerSocket 从前边的程序的运行情况来看,程序是可以运行的,但速度太扯了,在读写1000W条之前速度还是可以的(大概2000条/秒左右),但过了1000W之后(变成400条/秒左右),当然这个与SQL SERV

用perl做数据库迁移,从MSSQL到MYSQL(三)--V1.1版~多线程+handlerSocket

从前边的程序的运行情况来看,程序是可以运行的,但速度太扯了,在读写1000W条之前速度还是可以的(大概2000条/秒左右),但过了1000W之后(变成400条/秒左右),香港虚拟主机,当然这个与SQL SERVER读取,网络还有服务器等性能都是有关系的,但,这速度,不晓得有测试过的朋友受不受不了,我反正是受不了的,于是想了下,单线程慢,咱得改吧。改成多线程,多进程嘛。

另外再啰嗦一句,经小弟实测,改之后,效率真是快很多。。。。

不再啰嗦,直接上代码吧。

DBI; 3 use Switch; 4 use strict; 5 use Net::HandlerSocket; 6 use threads; ::HiRes ; = ; = ; = ; =; = ; = 9999; =DBI->,$source_user_name,$source_user_psd); 19 #获取所有的用户表,不导有地理字段的表 =$dbh->prepare("select name,object_id from sys.all_objects ao where type='U' and not exists( ); 23 $sth->execute(); #线程数。。。这个很纠结,小弟的服务器,在导的时候,美国服务器,5个线程以上,服务器会挂起~~~~ =(not defined $ARGV[0])?5:$ARGV[0]; =(not defined $ARGV[1])?3000:$ARGV[1]; ; 32 while (@data=$sth->fetchrow_array()) 33 { 34 ##测试时用 ($select_columns,$insert_columns,$column_count,$sort_column,$column_types); 37 #获取某个表的列,并构建 查询,插入,列总数,列类型 38 ##输入参数如下: 39 ###data[0]:表名,data[1]:对像ID 40 ##返回参数描述如下: 41 ###$select_columns:构建SELECT的时候,列字符串 42 ###$insert_columns:构建insert的时候,列字符串。之所以要把这两分开,因为有些类型在select的时候,会用到列属性方法,例如geometry.STAsText() 43 ###$column_count:列数,其实可以从@$column_types得到,但@$columns_types是后边加的,此参数也就没有去掉 44 ###$sort_column:用来排序的字段,因为总结了一下,一般第一个字段都是标识字段,主键,因此,这里只取的第一个字段 45 ###$columns_types:列的类型列表,一个数组。因为sql server里边的某些类型的值,在进mysql的时候,需要做处理,例如geometry ($select_columns,$insert_columns,$column_count,$sort_column,$column_types)=get_columns($data[0],$data[1]); 48 #查询结果。如果是导入失败,会返回False,否则为空 = export_data_in ($select_columns,$insert_columns,$column_count,$sort_column,$data[0],$column_types); 51 52 } ->disconnect; export_data_in 61 { ($select_columns,$insert_columns,$columns_count,$sort_column,$table_name,$column_types) = @_; =0; =DBI->,$source_user_name,$source_user_psd); =); 67 $sth_sc->execute(); =$sth_sc->fetchrow_array(); = 0; = $per_records - 1; 72 while($begin_cnt @data_count[0]) 73 { ; (my $count=1;$count$threads_cnt;$count++) 77 { 78 ##基本,香港空间,下边的SQL语句成了本程序最大的性能瓶颈了。小弟的测试中,前1000W条数据还好,但,在1000W条之后,此SQL语句的查询性能急剧下降,当然,小弟是在远程测试上边导的,(当然,我的表没分区的,有MSSQL优化经验的知道,表是可以分区的) ="select * 81 FROM 82 ( 83 SELECT $select_columns,ROW_NUMBER() OVER (ORDER BY $sort_column) AS RowNum 84 FROM $table_name 85 ) as t ; ; =threads->new(\&export_data, $table_name,$sql_select,$insert_columns,$columns_count,$column_types); 91 push(@threads,$res0); 92 $begin_cnt = $begin_cnt + $per_records; 93 $end_cnt = $end_cnt + $per_records; 94 } (@threads) 97 { 98 $_->join; 99 } 100 } } export_data 105 { =time; 107 my ($table_name,$sql_select,$insert_columns,$columns_count,$column_types)=@_; =DBI->,$source_user_name,$source_user_psd); =$dbh_mssql->prepare($sql_select); 111 $sth_select->execute(); 112 $sth_select->{LongTruncOk}=1; =rand(3200); =""; ; 118 ##还是改成fetchrow_arrayref(),小弟测试了下,这个的速度,真不是之前fetchrow_array能比的 ($select_data=$sth_select->fetchrow_arrayref()) 121 { 122 if($data_str ne "") 123 { ; 125 } =.,@{; 128 129 } ,time-$startTime); 131 $startTime=time; 132 ##测试的时候,查看数据的语句。 ($data_str ne "") 135 { ; = { host => $aim_ip, port => $hs_port }; = new Net::HandlerSocket($args); = , ); ->get_error() if $res != 0; = $hs->execute_multi(eval($data_str)); ->get_error() if $hs->get_error() != 0; 144 $hs->close(); 145 } ; ,time-$startTime); #这里啰嗦一下,也给大家展示一下我的结果 ^-^ 150 # exporting data t_p_areagroup_plate_userdiy_l;total:42758121;now:12825000 151 # exporting data t_p_areagroup_plate_userdiy_l;total:42758121;now:12830000 152 # exporting data t_p_areagroup_plate_userdiy_l;total:42758121;now:12835000 153 # exporting data t_p_areagroup_plate_userdiy_l;total:42758121;now:12840000 154 # exporting data t_p_areagroup_plate_userdiy_l;total:42758121;now:12845000 155 # 读出时间18.9 seconds. 156 # 写入时间1.3 seconds. 157 # 读出时间23.3 seconds. 158 # 写入时间1.4 seconds. 159 # 读出时间23.7 seconds. 160 # 写入时间1.1 seconds. 161 # 读出时间25.6 seconds. 162 # 写入时间0.6 seconds. 163 # 读出时间25.6 seconds. 164 # 写入时间0.9 seconds. } get_columns 169 { ; ="select col.name,tp.name from sys.all_columns col 172 inner join sys.types tp on col.system_type_id=tp.system_type_id and col.user_type_id=tp.user_type_id ; =DBI->,$source_user_name,$source_user_psd); =$dbh2 -> prepare($sql); 176 $cols->execute(); = ""; = ""; = 0; =""; ; ; 183 while(@col= $cols->fetchrow_array()) 184 { 185 my ($col_name,$type_name)=@col; 186 @cols_types[$cols_count]=$type_name; 187 if($cols_count>0) 188 { ; ; 191 } { ; 195 } ) 197 { ; ; 200 } { ; ; 205 } 206 $cols_count++; 207 } 208 $dbh2->disconnect; 209 ($cols_select,$cols_insert,$cols_count,$sort_column,\@cols_types); 210 } 211 212

调用方法(将运行结果放到out.log):

1 nohup perl export_data_muti_thread_v0.5.pl 10 5000 > out.log &

 

 

另外再啰嗦一句。。。cnblogs的回复真不多,哪怕是拍砖也好呀。别这么死气沉沉的。

posted on

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

Is MySQL Beginner-Friendly? Assessing the Learning CurveIs MySQL Beginner-Friendly? Assessing the Learning CurveApr 17, 2025 am 12:19 AM

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Is SQL a Programming Language? Clarifying the TerminologyIs SQL a Programming Language? Clarifying the TerminologyApr 17, 2025 am 12:17 AM

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools