Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312775
  • 博文数量: 103
  • 博客积分: 1590
  • 博客等级: 上尉
  • 技术积分: 1075
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 10:17
文章分类

全部博文(103)

文章存档

2013年(32)

2012年(7)

2010年(64)

我的朋友

分类: Mysql/postgreSQL

2010-11-15 00:09:46

作/译者:陶会祥 来源:http://blog.chinaunix.net/u3/107145/ 欢迎转载,请注明作者及出处~

mysql 查询中in的优化:

上周未,相册的DB CPU突然升到80%,服务down机~

检查发现主要用两种SQL慢查:

select count(1) from album_friends where photo_count > 0 and ownerid in ( '64490847','95589567','185688181',......) and control != -1;

desc select count(1) from album_friends where photo_count > 0 and ownerid in ( '64490847','95589567','185688181',......) and control != -1 order by id desc limit 0, 20;

监控如下:

qirong分析,这个值高了,是sql中有没用到索引的读飙了

上图表示应该出现不少的tmp table

原来表结构:


HOST A:
album_friends | CREATE TABLE album_friends (
id int(11) NOT NULL,
ownerId int(11) NOT NULL,
ownerName varchar(255) collate utf8_bin NOT NULL,
title varchar(255) collate utf8_bin NOT NULL,
photo_count int(11) NOT NULL,
uptime datetime NOT NULL,
headurl varchar(255) collate utf8_bin NOT NULL,
univId int(11) NOT NULL,
univName varchar(255) collate utf8_bin NOT NULL,
tinyurl varchar(255) collate utf8_bin NOT NULL,
control int(11) NOT NULL default '0',
stage int(11) NOT NULL default '20',
time datetime NOT NULL default '1970-01-01 00:00:00',
mainurl varchar(255) collate utf8_bin NOT NULL default '',
PRIMARY KEY (id),
KEY album_fresh_uptime_index (uptime),
KEY ownerid (ownerId,uptime),
KEY univ (univId),
KEY stage (stage,uptime),
KEY univId (univId,time,photo_count)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
sql

处理:

alter table album_friends drop index univId, drop index stage ,drop index univ,drop index album_fresh_uptime_index ,drop index ownerid,drop primary key ,add primary key (ownerId,id), add unique id (id),add index (uptime)

HOST B:
album_friends | CREATE TABLE album_friends (
id int(11) NOT NULL,
ownerId int(11) NOT NULL,
ownerName varchar(255) collate utf8_bin NOT NULL,
title varchar(255) collate utf8_bin NOT NULL,
photo_count int(11) NOT NULL,
uptime datetime NOT NULL,
headurl varchar(255) collate utf8_bin NOT NULL,
univId int(11) NOT NULL,
univName varchar(255) collate utf8_bin NOT NULL,
tinyurl varchar(255) collate utf8_bin NOT NULL,
control int(11) NOT NULL default '0',
stage int(11) NOT NULL default '20',
time datetime NOT NULL default '1970-01-01 00:00:00',
mainurl varchar(255) collate utf8_bin NOT NULL default '',
PRIMARY KEY (ownerId,id),
UNIQUE KEY uid (id),
KEY uptime (uptime)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin


加索引并重启mysql后,目前看来效果不错~



阅读(2771) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-15 16:14:49

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com