mysql数据库优化
1. sql执行级别
(8)select (9)distinct
(1)from<left_table>
(3)<join_type> join <right_table>
(2) on<join_condition>
(4)where<where_condition>
(5)group by<group_by_list>
(6)with{cube|rollup}
(7)having<having_condition>
(10)order by<order_by_list>
(11)limit<limit_number>
2. 存储引擎
不需要支持事务的表使用mysiam引擎存储。具有更高的效率
3. 表结构定义
1、选取最适用的字段属性
2、字段最好给一个默认值,查询效率会更高
4. sql语句优化
采用explain分析sql语句,以此为依据优化sql
使用连接(JOIN)或者exists 来代替子查询(Sub-Queries)
5. 索引优化
查询频繁的字段添加索引
6. 慢查询日志分析
7. 采用redis进行加锁,避免过度频繁访问数据库
$lock_key = 'order_create_lock_' . $user->id;
$hot_cache = $this->redis;
if (!$hot_cache->setnx($lock_key, $user->id)) {
info('Exce', $user->id, '请求多次,lock', $lock_key);
return [ERROR_CODE_FAIL, '您发起支付太快啦,请稍后.', null];
}
$hot_cache->expire($lock_key, 5);
8. 数据缓存
$user = $this->cache->get('user'.$id);
if($cache){
return $user;
}
$user = User::find($cond);
$this->cache->set('user'.$user->id,$user);
9. 配置优化
参照:http://blog.csdn.net/orichisonic/article/details/48026031
#采用socket代替ip监听
socket = /data/3306/mysql.sock