innodb 二次写 :写入真实表之前,先写入共享表空间(一次随机IO,2M大小)
Buffer Pool 占到75-80% 内存空间, 很重要。
innodb 辅助索引叶子节点: 存放的是key 和 主键索引。
url字段添加索引,可以通过先hash ,然后建索引
联表查询,要遵循最左原则。
只扫描自己需要的列。
最先优化查询次数最多的sql ,而非执行时间最长的sql。
查看执行计划: profile, 例如: show profile for query 2 ;
建立索引示例
select * from test where col2 > 10 and col3 =10 ; 建索引 (col3, col2)
select * from test where col2 = 10 and col3 = 10 ; 建索引 (col2, col3)
select * from test where col2 = 10 order by col3 ; 建索引 (col2, col3)
select * from test where col2 > 10 order by col3 ; 建索引 看业务需要 (col3) 用于排序, 或者 (col2) 更注重筛选
show profiles;
主键选择原则
• 不使⽤更新频繁的列
• 尽量不要选择字符串 (构建临时表时候,需要开辟字符串最大长度空间,其次表示范围也小, 8字节整数能表示长度十七八位的组合,而8个字符表示范围较小)
• 不使⽤UUID MD5+HASH
• 建议使⽤⾃增ID