标题: slow.log中记录的全是一条语句,请教优化方法 [打印本页] 作者: scc_166 时间: 2008-10-16 14:12 标题: slow.log中记录的全是一条语句,请教优化方法 发现全是
select id,title,content,uid,url,count(uid) from data where statue=1 group by uid order by sr desc
表结构:
create table data (...);
alter table data add key (uid)
alter table data add key (sr)
alter table data add key (statue)
tmp_table_size = 96M
加上上面的索引后还是没什么用.
还有没有别的查询语句可以比这个更快的呢?作者: yueliangdao0608 时间: 2008-10-16 14:16
你的这个索引加的不对吧?作者: scc_166 时间: 2008-10-16 14:27
用
alter table data add key(statue,uid,sr)
替换也是一样的慢作者: ruochen 时间: 2008-10-16 14:33
status字段有哪些可选值?作者: ruochen 时间: 2008-10-16 14:34
group by order by的很难优化作者: ruochen 时间: 2008-10-16 14:35
考虑在uid和status上做index作者: 猪知猪之道 时间: 2008-10-16 19:17
select A.id,title,content,A.uid,url,B.CC From
data A,
(select uid,count(*) as CC , min(id) mid
From data where status =1
group by uid ) B
where A.id= B.mid
Order by A.sr ;
另外 试试下面哪个索引 起作用,
create index stall on data(status,uid,id) ;
create index uidsta on data(uid,id,status) ;
mysql>
mysql>
mysql> select A.id,title,content,A.uid,url,B.CC From
-> data A,
-> (select uid,count(*) as CC , min(id) mid
-> From data where status =1
-> group by uid ) B
-> where A.id= B.mid
-> Order by A.sr desc ;
+----+-------+-----------+------+-------+--------+
| id | title | content | uid | url | CC |
+----+-------+-----------+------+-------+--------+
| 17 | t5 | content15 | us2 | url41 | 49152 |
| 49 | t13 | content23 | us3 | url14 | 16384 |
| 1 | t1 | content11 | us1 | url11 | 131072 |
+----+-------+-----------+------+-------+--------+
3 rows in set (0.38 sec)
mysql>
mysql>
mysql>
mysql>
mysql> explain select id,title,content,uid,url,count(uid)
-> from data where status=1
-> group by uid order by sr desc ;
+----+-------------+-------+-------+---------------+--------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+--------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | data | index | stall | uidsta | 54 | NULL | 213204 | Using where; Using temporary; Using filesort |
+----+-------------+-------+-------+---------------+--------+---------+------+--------+----------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql> explain select A.id,title,content,A.uid,url,B.CC From
-> data A,
-> (select uid,count(*) as CC , min(id) mid
-> From data where status =1
-> group by uid ) B
-> where A.id= B.mid
-> Order by A.sr desc ;
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3 | Using temporary; Using filesort |
| 1 | PRIMARY | A | eq_ref | PRIMARY | PRIMARY | 8 | B.mid | 1 | |
| 2 | DERIVED | data | index | stall | uidsta | 54 | NULL | 213204 | Using where; Using index |
+----+-------------+------------+--------+---------------+---------+---------+-------+--------+---------------------------------+
3 rows in set (0.32 sec)