成都创新互联网站制作重庆分公司

SQL语句优化

一、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: 0
(4)重新查询
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

名称栏目:SQL语句优化
文章源于:http://cxhlcq.com/article/jojhjp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部