MySQL 优化学习之路

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

Overview

DB Level

  • table structure: column data type | table with few or many columns
  • right indexs
  • storage engine
  • data format: compression or not
  • locking strategy
  • caching size

Hardware Level

  • Disk seeks 10ms
  • Disk reading and writing. easier to optimize than disk seeks
  • CPU cycles large tables compared to the amount of memory ???
  • Memory bandwith when CPU needs more data to fit in CPU cache ???

SQL

  • use explain
  • indexs
  • avoid full table scan
  • analyze table periodically
  • read-only transactions 5.6.4+ // had read an article on ATA about this
  • avoid transforming query hard to read, optimizer will do this

SELECT

  • cover index: In some cases, MySQL can read rows from the index without even consulting the data file. If all columns used from the index are numeric, only the index tree is used to resolve the query. // need numeric ? TODO
  • range index
    • MySQL does not support merging ranges, use union
    • eq_range_index_dive_limit To permit use of index dives for comparisons of up to N equality ranges, seteq_range_index_dive_limit to N + 1
  • index extensions: add pk after each secondary index 5.6.9
  • two kinds of filesort
  • group by: loose index scan vs tight index scan, depends on distribution of column(cardinality).

INSERT

  • use INSERT statements with multiple VALUES lists to insert several rows at a time will be faster than using separate single-row INSERT statements.
  • bulk_insert_buffer_size for large INSERT
  • insert values explicitly only when the value to be inserted differs from the default.
  • Bulk insert speed up (https://dev.mysql.com/doc/refman/5.6/en/optimizing-innodb-bulk-data-loading.html)

Update

  • same with INSERT

DELETE

  • truncate

Optimizing INFORMATION_SCHEMA Queries

  • Try to use constant lookup values for database and table names in the WHERE clause
  • Write queries that minimize the number of table files that must be opened (???)
  • Use EXPLAIN to determine whether the server can use INFORMATION_SCHEMA optimizations for a query

Index

Index can improve the speed of determining rows which match where statements. But useless indexs are waste of space and time for db to determinie whcih index to use and need more time to create indexs when insert.

How MySQL use index

  • the most seletive indexs
  • leftmost prefix of the index
  • join:
    • use same data type will be faster // varchar and char are the same if their size equal.
    • must use the same character set when compare string columns
    • comparison of dissimilar column may prevent use of indexs
  • MIN() MAX() of column key_col will be O(1) if all key_part_N before key_col in where statement is constant.
  • cover index // here not mention numeric

Primary Key

  • use numeric pk

Foreign Key

  • split low-frequently data into separate table

Column Key

  • prefix index
  • fulltext for char varchar and text

Statistic

  • expr1 = expr2 is not true when expr1 or expr2 (or both) are NULL
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
19天前
|
SQL 关系型数据库 MySQL
Mysql基础学习day02-作业
本教程介绍了数据库表的创建与管理操作,包括创建员工表、插入测试数据、删除记录、更新数据以及多种查询操作,涵盖了SQL语句的基本使用方法,适合初学者学习数据库操作基础。
49 0
|
19天前
|
SQL 关系型数据库 MySQL
Mysql基础学习day03
本课程为MySQL基础学习第三天内容,主要讲解多表关系与多表查询。内容涵盖物理外键与逻辑外键的区别、一对多、一对一及多对多关系的实现方式,以及内连接、外连接、子查询等多表查询方法,并通过具体案例演示SQL语句的编写与应用。
35 0
|
4月前
|
SQL 缓存 关系型数据库
MySQL 慢查询是怎样优化的
本文深入解析了MySQL查询速度变慢的原因及优化策略,涵盖查询缓存、执行流程、SQL优化、执行计划分析(如EXPLAIN)、查询状态查看等内容,帮助开发者快速定位并解决慢查询问题。
161 0
|
13天前
|
关系型数据库 MySQL 数据管理
Mysql基础学习day03-作业
本内容包含数据库建表语句及多表查询示例,涵盖内连接、外连接、子查询及聚合统计,适用于员工与部门数据管理场景。
25 1
|
24天前
|
SQL 关系型数据库 MySQL
Mysql基础学习day01
本课程为MySQL基础学习第一天内容,涵盖MySQL概述、安装、SQL简介及其分类(DDL、DML、DQL、DCL)、数据库操作(查询、创建、使用、删除)及表操作(创建、约束、数据类型)。适合初学者入门学习数据库基本概念和操作方法。
119 6
|
2月前
|
缓存 关系型数据库 MySQL
降低MySQL高CPU使用率的优化策略。
通过上述方法不断地迭代改进,在实际操作中需要根据具体场景做出相对合理判断。每一步改进都需谨慎评估其变动可能导致其他方面问题,在做任何变动前建议先在测试环境验证其效果后再部署到生产环境中去。
88 6
|
22天前
|
SQL 关系型数据库 MySQL
Mysql基础学习day01-作业
本教程包含三个数据库表的创建练习:学生表(student)要求具备主键、自增长、非空、默认值及唯一约束;课程表(course)定义主键、非空唯一字段及数值精度限制;员工表(employee)包含自增主键、非空字段、默认值、唯一电话号及日期时间类型字段。每个表的结构设计均附有详细SQL代码示例。
48 0
|
23天前
|
SQL 关系型数据库 MySQL
Mysql基础学习day02
本课程为MySQL基础学习第二天内容,涵盖数据定义语言(DDL)的表查询、修改与删除操作,以及数据操作语言(DML)的增删改查功能。通过具体SQL语句与实例演示,帮助学习者掌握MySQL表结构操作及数据管理技巧。
75 0
|
3月前
|
存储 SQL 关系型数据库
MySQL 核心知识与索引优化全解析
本文系统梳理了 MySQL 的核心知识与索引优化策略。在基础概念部分,阐述了 char 与 varchar 在存储方式和性能上的差异,以及事务的 ACID 特性、并发事务问题及对应的隔离级别(MySQL 默认 REPEATABLE READ)。 索引基础部分,详解了 InnoDB 默认的 B+tree 索引结构(多路平衡树、叶子节点存数据、双向链表支持区间查询),区分了聚簇索引(数据与索引共存,唯一)和二级索引(数据与索引分离,多个),解释了回表查询的概念及优化方法,并分析了 B+tree 作为索引结构的优势(树高低、效率稳、支持区间查询)。 索引优化部分,列出了索引创建的六大原则
|
3月前
|
存储 SQL 关系型数据库
MySQL 动态分区管理:自动化与优化实践
本文介绍了如何利用 MySQL 的存储过程与事件调度器实现动态分区管理,自动化应对数据增长,提升查询性能与数据管理效率,并详细解析了分区创建、冲突避免及实际应用中的关键注意事项。
128 0

推荐镜像

更多