14. 其它 Top : Ps : ps aux|awk '{ if ($6 >(1024*15)){print $2}}'|grep -v PID ………………
15. 分析服务器 Mysqlreport Mysqladmin mysqladmin extended -r -i 10| grep –v “| 0 ” -uroot –p Innotop …. Show global status Show innodb status
16. 分析查询 Show processlist Flush status Show session status like ‘Select%’ Show session status like ‘Handler%’ Show session status like ‘Sort%’ Show session status like ‘Create%’ Set profiling
20. Mysql 配置 Mysiam 缓存 key_buffer_size key_buffer_1.key_buffer_size=1G show variables like 'key_buffer_size'; show global status like 'key_read%'; key_cache_miss_rate = Key_reads / Key_read_requests * 100% du –sch `find /mysqldatapath/ -name “*.MYI”` Innodb 缓冲池 innodb_bufer_pool_size 80% show status like 'Innodb_buffer_pool_read%' Innodb_buffer_pool_read_requests/Innodb_buffer_pool_reads show innodb status\G BUFFER POOL AND MEMORY Total memory allocated 4668764894;
21. 查询缓存 show global status like 'Qcache%'; show variables ‘like query_cache%'; 查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100% 查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100% http://www.day32.com/MySQL/
25. 数据类型选择 更小通常更好 , 慷慨并不明智 简单就好 使用 mysql 内建的类型保存日期和时间,使用 timestamp 保存,空间是 datetime 一半 使用整数保存 ip 15 bytes for char(15) vs. 4 bytes for the integer ip2long() 和 long2ip() inet_aton 3 尽量避免 null 4 Char /varcha 的选择 对于 MyISAM 而言,如果没有 VARCHAR , TEXT 等变长类型,那么每行数据所占用的空间都是定长的( Fixed ),俗称静态表,相对应的就是动态表。当执行一次查询时, MySQL 可以通过索引文件找到所需内容的实际行号,此时,由于每行数据所占用的空间都是定长的( Fixed ),所以可以通过查询到的实际行号直接定位到数据文件的具体位置, 对于 InnoDB 而言,数据行是没有所谓定长与否的概念的,这是由其结构所决定的:在 InnoDB 中,数据就位于 Primary Key 的 B-Tree 叶节点之上而除 Primary Key 之外的索引被称为 Secondary Index ,它们直接指向 Primary Key 。 用 char 来代替 varchar , MyISAM 是这样, InnoDB 则相反 5 使用 enum 代替字符串类型 select internet + 0 from hotel_info group by internet;
26. 索引 隔离列 select * from tablename where id+1=5 Select * where TO_DAYS(CURRENT_DATE) – TO_DAYS(data_col) < =10 Select * where data_col >=date_sub(current_date,interval 10 day) Select * where data_col >= date_sum(‘2010-04-12’,interval 10 day) EXPLAIN SELECT * FROM film WHERE title LIKE 'Tr%'\G EXPLAIN SELECT * FROM film WHERE LEFT(title,2) = 'Tr' \G 组合索引 前缀索引 覆盖索引 合并索引 去除多余索引和重复索引 create table test (id int not null primary key, unique(id), index(id) )
27. 合并索引 索引合并 方法用于通过 range 扫描搜索行并将结果合成一个。合并会产 生并集、交集或者正在进行的扫描的交集的并集。 在 EXPLAIN 输出中,该方法表现为 type 列内的 index_merge 。 在这种情况下, key 列包含一列使用的索引, key_len 包含这些索引的最长的关键元素 SELECT * FROM tbl_name WHERE key_part1 = 10 OR key_part2 = 20;
28. 前缀索引 Key(a,b,c) Order by a ,order by a,b order by a,b,c order by a desc ,b desc,c desc Where a = const order by b,c,where a=const and b =const order by c Where a = const order by b,c where a = const and b > const order by,c Order by a asc,b desc,c desc Where g = const oder by b ,c Where a = const order by c Where a = const order by a ,d Where a>’xx’ order by b,c Where a>’xx’ order by a,b Where a=const order by b desc ,a asc
29. 覆盖索引 Select * from products where actor=‘sean carrey’ and and title like ‘%apollo%’ Select * from products join (select prod_id from products where actor= ‘sean carrey’ and title like ‘%apollo%’) as t1 on (t1.prod_id = products.pro_id)
33. 1 explain select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10; +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ | 1 | SIMPLE | page_test | ref | idx_b_c | idx_b_c | 4 | const | 2222 | Using where | +----+-------------+-----------+------+---------------+---------+---------+-------+------+-------------+ 2 mysql> explain select SQL_NO_CACHE * from page_test, (select SQL_NO_CACHE id from page_test force index(idx_b_c) where b=1 order by c desc limit 2000,10) temp where page_test.id=temp.id; +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | | | 1 | PRIMARY | page_test | eq_ref | PRIMARY | PRIMARY | 8 | temp.id | 1 | | | 2 | DERIVED | page_test | ref | idx_b_c | idx_b_c | 4 | | 2222 | Using where; Using index | +----+-------------+------------+--------+---------------+---------+---------+---------+------+--------------------------+ 3 explain select SQL_NO_CACHE * from page_test force index(idx_b_id) where b=1 and id<187796 order by id desc limit 10; +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ | 1 | SIMPLE | page_test | range | idx_b_id | idx_b_id | 12 | NULL | 190 | Using where | +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+ 5 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c desc 870,10 select SQL_NO_CACHE * from page_test force index(idx_b_c) where b=1 order by c asc 9120,10 ./tuning-primer.sh all
34. 优化 count(*) Mysiam: select sql_no_cache count(*) from statistic_go where id > 10; select sql_no_cache (select count(*) from statistic_go) - count(*) from statistic_go where id <=10; Innodb : 在缺少 where 字句的情况下, InnoDB 不对 count(*) 查询进行优化–这个是事实。 SELECT COUNT(*) FROM sbtest1 WHERE id>=0; 建立计数器触发器 对同一个表的 select 和 update update hotel_image inner join (select count(*) as cnt from hotel_image) as der set hotel_image.size = der.cnt;
35.
36. 其它一些 Group by 不进行排序,可以 ordery by null 能够快速缩小结果集的 WHERE 条件写在前面,如果有恒量条件,也尽量放在前面 使用 UNION 来取代 IN 和 OR 定期执行 optimize / analyze table 往 innoDB 表导入数据时,先关闭 autocommit 模式,否则会实时刷新数据到磁盘 对于频繁更改的 MyISAM 表 , 应尽量避免更新所有变长字段 (VARCHAR 、 BLOB 和 TEXT) 分表 分库 汇总表 十大热门话题 create table hotel_infonew like hotel_info; rename table hotel_info to hotel_info_old ,hotel_infonew to hotel_info 放弃关系型数据库 key=>value ,计数表