Migration process
Program transformation and database migration
Migration type
Online migration and offline migration
Migration tool
You can use SQL Server Management Studio, which adds an ODBC driver to operate Mysql.
SQL OPENQUERY usage
OPENQUERY function, remotely execute database addition, deletion, modification, and query.
eg:
-
OPENQUERY([MYSQL-STAGE-STAGE], 'select * from etl_migrate_log where table_name= ''notes'';')
The first parameter is the configured linked server name, and the second parameter is the MySQL command to be executed
Add, delete, modify
--1.SELECT语句
SELECT * FROM OPENQUERY([MYSQL-STAGE-STAGE], 'select * from etl_migrate_log where table_name= ''notes'';');
--或
SELECT * FROM OPENQUERY([MYSQL-STAGE-STAGE], 'select * from etl_migrate_log ') WHERE table_name= 'notes'
--2.INSERT语句,INTO可省略
INSERT INTO OPENQUERY([MYSQL-STAGE-STAGE],'select table_name,endtime from etl_migrate_log ;') VALUES( 'notes', now())
--3.UPDATE语句
UPDATE OPENQUERY([MYSQL-STAGE-STAGE], 'select table_name, endtime from etl_migrate_log where table_name= ''notes''') SET endtime = now()
--4.DELETE语句,FROM可省略
DELETE FROM OPENQUERY([MYSQL-STAGE-STAGE], 'select * from etl_migrate_log where table_name= ''notes''') SET @ tablename = 'ls'
- or
DELETE FROM OPENQUERY([MYSQL-STAGE-STAGE], 'select * from etl_migrate_log') WHERE table_name= @tablename
Structure and syntax difference
-
, Timestamp data type
Both sqlserver and mysql have a timestamp data type, but their implementation is quite different.
In SQL Server, this type indicates the relative order of data modification in the database. Its value is essentially an increasing number of type bigint, independent of time and date, and the number will not be reread at the database instance level.
In mysql, this type indicates the relative order in which data modification occurs in the database. Its essence is a time, which may be repeated at the table level.
The biggest pitfall is that this type of field is the key field of our business, used for users to pull data from the server.
The final solution is to migrate the data of this field from sql server to mysql according to the value of bigint, mysql adds a timastamp data type, and the program adds a judgment mechanism.
2. Character set and emoji expressions
By default, emoji expressions can be saved directly in sqlserver. The migration process did not notice the problem. The problem was discovered after the historical data was migrated.
Need to use utf8mb4 to save this data in mysql.
3. The index cannot exceed the size
The index length in mysql cannot exceed 767 bytes! ! !
This can only be an optimized data type.
For example, some fields save the device's macid, the data type is varchar(50), after investigation, it is found that macid has only 12 bits, which can be modified to varchar(12).
And whether int can be changed to tinyint or smallint, can latin1 be used instead of utf8 (of course it is best to use utf8 uniformly), etc.
Here is mainly the optimization of data types, and there will be a special article to introduce data type optimization later.
4. Varchar(max) type
MySQL does not have this data type, and the text type is required in MySQL.
5. Common incompatible syntax
One of sqlserver uses top to fetch the first few rows of data, and mysql uses limit to complete this function. This is relatively easy to modify.
The two methods of using temporary tables are different.
6, CTE recursion
In operations where there is a recursive query, sqlserver has a great feature that is CTE recursive query.
7, over window query
The over window function in sqlserver is also a feature that I like very much, such as grouping sorting, grouping range and other queries.