MySQL 优化学习之路

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 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
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
21天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
16 0
|
27天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
|
21天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
87 1
|
22天前
|
SQL 存储 关系型数据库
6本值得推荐的MySQL学习书籍
本文是关于MySQL学习书籍的推荐,作者在DotNetGuide技术社区和微信公众号收到读者请求后,精选了6本值得阅读的MySQL书籍,包括《SQL学习指南(第3版)》、《MySQL是怎样使用的:快速入门MySQL》、《MySQL是怎样运行的:从根儿上理解MySQL》、《深入浅出MySQL:数据库开发、优化与管理维护(第3版)》以及《高性能MySQL(第4版)》和《MySQL技术内幕InnoDB存储引擎(第2版)》。此外,还有12本免费书籍的赠送活动,涵盖《SQL学习指南》、《MySQL是怎样使用的》等,赠书活动有效期至2024年4月9日。
|
1天前
|
SQL 监控 关系型数据库
【MySQL学习】MySQL的慢查询日志和错误日志
【MySQL学习】MySQL的慢查询日志和错误日志
|
4天前
|
SQL 关系型数据库 MySQL
【MySQL】SQL优化
【MySQL】SQL优化
|
5天前
|
SQL 关系型数据库 MySQL
不允许你不知道的 MySQL 优化实战(一)
不允许你不知道的 MySQL 优化实战(一)
11 2
|
6天前
|
存储 缓存 关系型数据库
掌握MySQL数据库这些优化技巧,事半功倍!
掌握MySQL数据库这些优化技巧,事半功倍!
|
6天前
|
缓存 关系型数据库 MySQL
MySQL数据库优化技巧:提升性能的关键策略
索引是提高查询效率的关键。根据查询频率和条件,创建合适的索引能够加快查询速度。但要注意,过多的索引可能会增加写操作的开销,因此需要权衡。
|
7天前
|
SQL Oracle 关系型数据库
下次老板问你MySQL如何优化时,你可以这样说,老板默默给你加工资
现在进入国企或者事业单位做技术的网友越来越多了,随着去O的力度越来越大,很多国企单位都开始从Oracle向MySQL转移,相对于Oracle而言,MySQL最大的问题就是性能,所以,这个时候,在公司如果能够处理好MySQL的性能瓶颈,那么你也就很容易从人群中脱颖而出,受到老板的青睐。
27 1