SlideShare a Scribd company logo
MySQL总结
Schema优化
 varchar的长度。影响临时表排序消耗内存,性能。
 Memory引擎是不支持blob和text类型的。临时表?
 5.0.3版本之前的mysql,varchar最大长度255. 之后的版
本没有限制,但是受max row size 65535 bytes的限制。
对于utf8,最大可能的长度是varchar(21844)。
最左匹配原则
B-Tree类型的联合索引 index1(a, b, c)
 全值匹配: where a = a1 and b = b1 and c = c1
 最左匹配: where a = a1
 范围匹配: where a > a1
 Where a = a1 and b > b1
 Where a > a1 and b = b1 会使用索引,但只使用索引中
的a部分。
使用索引排序
 在order by字句中,必须和where条件一样,最左匹配。
 排序方向必须一致。不能有升有降序。
 唯一的例外,是索引前缀字段都是常量时。例如:
联合索引 index1(a, b, c)
order by a,b,c;
Where a = a1 order by b,c;
Where a = a1 and b = b1 order by c;
Where a > a1 order by a, b;
where a = a1 and b > b1 order by b;
可以使用索引排序。
导致索引无效的sql写法
 Where a + 1 = 5; (等式的字段一段为表达式)
 where year(create_time) = 2013; (函数操作)
 Where id = 345; (隐式类型转换,id是varchar类型)
 Where name like ‘%abc’ (非左前缀查询)
聚簇索引
数据分布:
 MyISAM:按数据插入的顺序存储在磁盘上。
 InnoDB:聚簇索引。数据行存储在主索引的叶子页中。
InnoDB聚簇索引带来影响:
 主索引顺序写入性能最好。Uuid主键性能差。
 对主索引值的update开销很大。(强制移动被更新的行)
 二级索引不存储数据行的物理地址,而存储主键值。使
用二级索引查询,需要两次索引查找。
定位问题sql的工具
 Explain
 Show full processlist
 Mysqldumpslow
 SQL_NO_CACHE, force index关键字有时候也是有用
的。
explain
Explain输出
特别关注:
Possible_keys, key, key_len
rows: mysql认为执行这个query它必须要examine的数据行
数。
Extra: using index的出现,索引覆盖查询。
Using filesort
Using temporary
mysqldumpslow
mysqldumpslow -s c -t 10 ./slow_queries.log
Count: 56 Time=2.15s (120s) Lock=0.00s (0s) Rows=0.0 (0),
quad[quad]@[10.58.16.14]
insert ignore into quad_mobile_contact_match_item (uploader_id,
contact_phone_number, contact_qd_user_id, contact_name, is_followed,
create_time,update_time) values(N,'S', N, 'S', N, N,N)
Count: 16 Time=2.43s (38s) Lock=0.18s (2s) Rows=0.0 (0), quad[quad]@2hosts
update quad_user_anchor set anchor=N where user_id=N and anchor<=N
备份和恢复
 数据安全的保障:定期snapshot备份+主从同步
 推荐工具: xtrabackup
 Snapshot备份要同时保存主从同步的偏移量。
 误操作发生后的数据恢复:恢复到最近的snapshot,分
析binlog,执行除误操作以外的其它sql,恢复数据。
 mysql/bin/mysqlbinlog
 总结一下quad中曾遇到的
Insert操作的batch优化
 Mysql jdbc驱动中默认的做法是每次addBatch放进一个
List,executeBatch时依次取出,顺序发送。相当于没有
批量操作。
 开启批量操作的方式:连接参数增加
rewriteBatchedStatements=true. Jdbc驱动版本大于
5.1.13
 写法要正确。 循环addBatch,最后executeBatch.
正确使用utf8mb4
启用utf8mb4的正确方式:
 服务端配置character-set-server=utf8mb4
 Connector/J的连接参数中不要加characterEncoding默
认值是autodetect。
 字段类型设置:
`user_name` varchar(100) CHARACTER SET utf8mb4 COLLATE
utf8mb4_unicode_ci
http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-charsets.html
诡异的主键冲突错误
什么情况下,insert的时候发生发生duplicate key错误,错
误日志中说主键123已存在。 但去数据库中select却发现并
不存在key为123的记录?
Insert into t_table values (123,’’), (123, ‘’);
主从延时问题
写程序的时候考虑主从的问题。
避免写完主库立即读从库。
加群弱提示消息: 写入群成员表, 读群成员表查到所有
群成员,发送弱提示推送。
改写为:
读群成员表,写入群成员表。 读出的成员,加上新加入的
成员,发送弱提示推送。
Online sharding
Sharding大家都懂。但是不长时间停服怎么做。
迁数据。
工程师更新一版中间程序,双写。
补数据。
更新程序,sharding完成。
存储过程?
利用mysql更新的原子性
Update tb set status = 1 where id = $id and status = 0;
异步任务调度
Quad用户注册取id?
仅适用于并发竞争比较少的情况。
很多时候update语句的where条件中要带有对被update字
段值的判定。例如 quad_user_anchor中anchor的update。

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Quad项目mysql总结

Editor's Notes

  1. Sort_buffer_size.. Mysql中有2种filesort算法。 1. 读取所有的数据行。生成k-v对,key是排序所用的key, value是数据行的指针。Kv存储在一个buffer。当buffer达到sort_buffer_size时,执行quicksort,结果存入temporary file。保存这个block的指针。重复至所有行完成。 Merge排序所有的block。 最后根据排好序列的kv对,读取对应数据行。 第2种算法,是第一种的改进。Buffer中存储了需要的数据,无需最后回表取数据。 默认采用第二种算法。涉及text和blob时,只采用第一种算法。