A Note on Performance Degradation When Migrating from Oracle to MySQL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: MySQL databases behave quite differently from Oracle databases. Databases need to be optimized after migrating from one to the other.

Alibaba_Cloud_Whitepaper_Securing_the_Data_Center_in_a_Cloud_First_World_v2

Background

After transitioning from Oracle to MySQL, a large number of customer systems have encountered performance issues during peak traffic, when CPU usage reaches 100%, or the background CRM system becomes complex and causes a service fault due to SQL accumulation. As far as I recall, when Taobao first migrated from Oracle to MySQL, there were also lots of SQL performance problems, among which, the one that left the deepest impression on me is the subquery problem. The original version was MySQL 5.1, which is poorly optimized for subqueries, resulting in many systems that migrated from Oracle to MySQL encountering performance issues. As a result, our development regulations have since specified that no complex table shall be joined in the foreground transaction system. In this article, I will list a few common operations that may cause problems during migration from Oracle to MySQL:

1) When the customer executes a 0 data migration, there are some SQL statements that need to be changed, some that can be changed but don't need to be, and some that should not be changed. The SQL statements that need to be changed depend on the situation. I will try to give you some guidance to help avoid problems in the future.

2) Database developers and administrators must be guided on how to reasonably use MySQL to its full potential.

1. Parallel Processing

1.1 Background

Oracle can physically divide a large serial task (any DML and common DDLs) into multiple smaller parts. These smaller parts can be simultaneously processed, and finally combined to obtain the final result. This allows Oracle to use parallel processing technology to operate very complex SQL queries in OLAP applications.

Two methods for enabling parallel queries:

(1) Use a hint during the query: select /*+ parallel(4) / count() from test_a, and specify a parallel query with a degree of parallelism of 4.
(2) Use an alter table: alter table test_a parallel 4; and inform Oracle to use a parallel query with a degree of parallelism of 4 when creating an execution plan for the table.

1.2 Transition Suggestions

MySQL does not support parallel processing. Therefore, after applications are migrated from Oracle to MySQL, only SQL statements subject to parallel processing can be used. Processing advice:

(1) AnalyticDB products, such as ADS, can be used on the Alibaba Cloud platform to process parallel analysis queries in Oracle.
(2) Service decomposition is performed on a complex SQL statement to split it into single SQL statements, and then the computing result is put in the application for processing.

2. Logical Reads, Physical Reads, and Consumed Time during SQL Execution

2.1 Background

The Oracle optimizer contains more abundant and complete optimization algorithms than the MySQL optimizer. Just in terms of table connections, Oracle supports three algorithms, i.e., nested loop, hash join, and sort-merge join, while MySQL only supports nested loop. This means that MySQL often performs poorly when processing complex queries associated with large or multiple tables. How can we then identify which queries should not be migrated to MySQL? Well, it can be determined based on the logical reads, physical reads, and consumed time during SQL execution.

Physical reading: reads data from a data block to a buffer cache. Logical reading: refers to reading a data block from a buffer cache. Execution time: refers to the time it takes for Oracle to execute one SQL statement.

(1) The first query for table t: select * from t.
(2) The second query: select * from t.

The first query has 6 physical reads, and the second query has no physical read and 6 logical reads. A data block is cached to a buffer cache during the first read, thus the second read and modification of the data block is performed in the buffer cache.

Logical reads are an important metric to look at when optimizing Oracle performance, as it can be easily collected and provides a wealth of information concerning the workload on the database engine. The logical read refers to the number of blocks read from the buffer when executing an SQL statement.

2.2 Transition suggestions

MySQL can rapidly execute simple SQL statements, but has difficulty executing the same statements that have high logical read, physical read, or execution requirements after migration to MySQL. This means that we need to make some transitions:

(1) When executing a single table query with long logical reads, physical reads, or execution time in SQL, it's possible to encounter a full table scan (dump requirements), or poor indexing. In this case you can use a read only node to handle the dump or optimize the indexes.

(2) When executing a multiple table query with long logical reads, physical reads, or execution time, the AnalyticDB product ADS can be used for processing.

(3) When executing a multiple table query with long logical reads, physical reads, or execution time, service decomposition can be performed to split an SQL statement into single SQL statements, and then the result can be put processed in the application.

Note: An SQL query with more than 100 million logical and physical reads and an execution time of more than 5 seconds is considered a large SQL query.

3.In(…)

3.1 Background

The number of in(...) parameters in Oracle is limited to within 1000. Although MySQL has no limit in number, it has a limit in SQL length. Meanwhile, a MySQL optimizer uses binary search when optimizing in(...) queries, meaning that the more parameters in the query the worse performance will be, so the number of parameters should generally be kept within 100.

3.2 Transition Suggestions

Oracle:select * from t where id in(id1,id2…..id1000);
MySQL:select * from t where id in(id1,id2…..id100);

4. Subquery

4.1 Background

MySQL 5.6 and below may encounter performance bottlenecks when processing subqueries associated with a large table because its optimizer only supports nested loop. I once participated in a large project that involved migrating from Oracle to MySQL. The database version at the time was 5.5 and the original Oracle application had stored a large number of subqueries, so after migrating to MySQL, a significant number of SQL statements were accumulated which filled up the available connections and overloaded the database's CPU. We eventually had to modify the subqueries to recover the system.
Typical subquery
SELECT first_name
FROM employees
WHERE emp_no IN
(SELECT emp_no FROM salaries_2000 WHERE salary = 5000);
The MySQL processing logic involves traversing each record in an "employees" table and then substituting the records to a subquery

4.2 Transition Suggestions

Rewrite the subquery
SELECT first_name
FROM employees emp,
(SELECT emp_no FROM salaries_2000 WHERE salary = 5000) sal WHERE emp.emp_no = sal.emp_no;
Note: Both MySQL 5.1 and 5.5 carry a large risk of modifying the subquery to an association.
MySQL 5.6 can avoid the awkward problem of rewriting the subquery.

5. View Optimization

5.1 Background

Indexing cannot be created on a common view as they do not store substantial information and the data it operates on comes from basic tables. So what can we do when we need to perform a large number of low efficiency queries on this kind of view? Oracle has a materialized view that is physically real and allows indexing.
However, MySQL does not support materialized views. Therefore, performance may be degraded after being migrated to MySQL.

5.2 Transition Suggestions

Split the view and execute it in the application.

6. Functional Index

6.1 Background

Functional index refers to a function-based index, similar to a common index. The only difference is that a common index is created based on a column, while a functional index is created based on a function. This, of course, will partly influence data insertion because the index is generated after the function has been computed. However, inserted data is usually quite small, while a data query involves a much larger data set. Slightly lowering insertion speed can help optimize query speed.

MySQL does not support functional indexes, so after SQL statements that use functional indexes in Oracle are migrated to MySQL, full table scan performance could suffer due to the inability to use functional indexes.

For example, when executing the following SQL statement:
select * from emp where date(gmt_create) = '2017-02-20'
Even though an index has been created on gmt_create, the emp table will still be fully scanned, and then a comparison is performed on the gmt_create field after deleting the minutes and seconds. If we create a function-based index, for example, index emp_upper_idx on emp(date(gmt_create)), we only need to scan a small part of the data in intervals and then obtain rowid to access the table data. This significantly improves speed.

6.2 Transition Suggestions

Functions on fields are removed by means of SQL rewriting, such that the fields can be indexed:
select * from emp where gmt_create>='2017-01-20 00:00:00' and gmt_created<'2017-01-21 00:00:00'

7. Summary

(1) Transition is required because MySQL does not support parallel query (keyword: parallel).
(2) MySQL optimizer is relatively ineffective, so special attention should be paid to SQL statements that have long logical reads, physical reads, and execution time.
(3) For MySQL, the number of in(...) parameters should be kept within 100.
(4) MySQL is poor at subquery optimization, so it is recommended to perform subquery transition or use MySQL 5.6.
(5) View transition is suggested since MySQL does not support materialized views.
(6) SQL rewriting is suggested to avoid indexing failure due to MySQL not supporting functional indexing.

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
7天前
|
存储 Oracle 关系型数据库
Oracle和MySQL有哪些区别?从基本特性、技术选型、字段类型、事务、语句等角度详细对比Oracle和MySQL
从基本特性、技术选型、字段类型、事务提交方式、SQL语句、分页方法等方面对比Oracle和MySQL的区别。
Oracle和MySQL有哪些区别?从基本特性、技术选型、字段类型、事务、语句等角度详细对比Oracle和MySQL
|
1月前
|
Oracle 关系型数据库 MySQL
Mysql和Oracle数据库死锁查看以及解决
【8月更文挑战第11天】本文介绍了解决MySQL与Oracle数据库死锁的方法。MySQL可通过`SHOW ENGINE INNODB STATUS`查看死锁详情,并自动回滚一个事务解除死锁;也可手动KILL事务。Oracle则通过查询V$LOCK与V$SESSION视图定位死锁,并用`ALTER SYSTEM KILL SESSION`命令终止相关会话。预防措施包括遵循ACID原则、优化索引及拆分大型事务。
|
2月前
|
SQL Oracle 关系型数据库
MySQL、SQL Server和Oracle数据库安装部署教程
数据库的安装部署教程因不同的数据库管理系统(DBMS)而异,以下将以MySQL、SQL Server和Oracle为例,分别概述其安装部署的基本步骤。请注意,由于软件版本和操作系统的不同,具体步骤可能会有所变化。
141 3
|
3月前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
60 0
|
4月前
|
存储 Oracle 关系型数据库
oracle 数据库 迁移 mysql数据库
将 Oracle 数据库迁移到 MySQL 是一项复杂的任务,因为这两种数据库管理系统具有不同的架构、语法和功能。
73 0
|
4月前
|
分布式计算 关系型数据库 MySQL
oceanbase-oracle/mysql 如何导入数据
oceanbase-oracle/mysql 如何导入数据
|
4月前
|
SQL Oracle 关系型数据库
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
|
4月前
|
Oracle 关系型数据库 MySQL
数据库中对时间的操作(mySql、Oracle、pgSql)
数据库中对时间的操作(mySql、Oracle、pgSql)
|
4月前
|
Oracle 关系型数据库 MySQL
Seata常见问题之oracle 数据库 报 just support mysql如何解决
Seata 是一个开源的分布式事务解决方案,旨在提供高效且简单的事务协调机制,以解决微服务架构下跨服务调用(分布式场景)的一致性问题。以下是Seata常见问题的一个合集
134 0
|
4月前
|
Oracle 关系型数据库 MySQL
PG系、Oracle、MySQL数据库在特定场景下结果差异分析
本文主要介绍以PolarDB O引擎、ADB PG为代表的PG系数据库在某种特定事务场景下,其事务结果与Oracle、MySQL不同的现象,并分析该现象出现的原因。
170 0
PG系、Oracle、MySQL数据库在特定场景下结果差异分析

推荐镜像

更多