一、SQL语句优化

1)查看表结构
MariaDB [oldboy]> desc test1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | NO   |     | NULL    |       |
| name  | char(16) | NO   |     | NULL    |       |
| age   | int(2)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
(2)explain 查看是否含有建立索引的语句
MariaDB [oldboy]> explain select * from test1 where name="kaka"\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: test1
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 5   #查询行数,表示当前查询了5行
        Extra: Using where
1 row in set (0.00 sec)
(3)创建索引
MariaDB [oldboy]> create index index_name on test1(name);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 04)重新查询
MariaDB [oldboy]> explain select * from test1 where name="kaka"\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: test1
         type: ref
possible_keys: index_name
          key: index_name
      key_len: 48
          ref: const
         rows: 1   #查询行数,表示当前只查询了1行
        Extra: Using index condition
1 row in set (0.00 sec)

#从以上例子可以看到,使用索引,可以更快的查询所需要的信息。

二、使用explain命令优化SQL语句(select语句)的基本流程

1、抓慢查询SQL语法方法

每隔2,秒输入:SHOW FULL PROCESSLIST; 如果出现2次说明存在慢查询
MariaDB [oldboy]> show full processlist;
+----+------+-----------+--------+---------+------+-------+-----------------------+----------+
| Id | User | Host      | db     | Command | Time | State | Info                  | Progress |
+----+------+-----------+--------+---------+------+-------+-----------------------+----------+
|  9 | root | localhost | oldboy | Query   |    0 | NULL  | show full processlist |    0.000 |
+----+------+-----------+--------+---------+------+-------+-----------------------+----------+
1 row in set (0.00 sec)

2、分析慢查询日志

配置参数记录慢查询语句
log_query_time = 2     #执行超过2s记录到log中
log_queries_not_using_indexes    #没有走索引的语句,记录log中
log-slow-queries = /data/3306/slow.log    #log的位置

explain select * from test from where name='oldboy'\G  #查看是否走索引
explain select SQL_NO_CACHE * from test where name='oldboy'\G #去除缓存

3、对需要建索引的条件列建立索引

生产场景,大表不能高峰期建立索引,例如:300万记录

4、分析慢查询SQL的工具mysqlsla(每天早晨发邮件)

切割慢查询日志,去重分析后发给大家
1)mv,reload进程 2)cp,>清空
2)定时任务
mv /data/3306/slow.log /opt/$(date +%F)_slow.log
mysqladmin -uroot -p123456 flush-logs 

mysqlsla分析:http://blog.itpub.net/7607759/viewspace-692828/

优化起因:
1)网站出了问题,访问很慢。
    a.web服务器的负载、存储、db(负载、io、cpu)
    登录db:show full processlist
2)慢查询语句(日志文件)
long_query_time=2    #执行超过2s记录到log中
log_queries_not_using_indexs  #没有走索引的语句,记录log中
log-slow-queries=/data/3306/slow.log   #log的位置
切割,分析,发给管理员

案例分析:
1.查看是否db存在慢查询:show full processlist;
2.explain分析:explain 慢查询的语句
3.查看表结构:desc test1;
4.定位在哪列建立索引,哪张表
5.查看条件字段列的唯一值的数量  select count(distinct ader) from ad_oldboy_detail
6.建立索引  create index ....

三、使用profile优化SQL语句优化

了解内容,高级DBA使用
help show profile;
select @@profiling;
set profiling = 1;
select @@profiling;
show profile;
show profile for query 2;

http://www.cnblogs.com/adforce/archive/2012/06/02/2532287.html