mysql 对百万数据进行排序问题

lzj0470 2011-06-06 10:22:48
模拟环境:
往表中插入100万条相同的数据。该表有6个索引。
查询的时候,会有where,也有order by。目前暂且排序一个字段。
结果发现,查询很慢。查询效率以分钟级别计算。
在用explain查看select语句的时候

id select_type table type possible_keys key key_len ref rows Extra
------ ----------- ------ ------ --------------------------- --------- ------- ------ ------ ---------------------------
1 SIMPLE btitle ref BoardID_1,BoardID_2,deleted BoardID_2 1002 const 73468 Using where; Using filesort

从语句中看,好像不能在优化了。
对这样的情况,该怎么处理?
...全文
848 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
WWWWA 2011-06-07
  • 打赏
  • 举报
回复
索引情况是什么,SQL语句是什么
ACMAIN_CHM 2011-06-07
  • 打赏
  • 举报
回复
[Quote]会有where,也有order by[/Quote]你需要创建复合索引。
lzj0470 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 acmain_chm 的回复:]
引用另外,我昨天想到把需要排序的字段抽取出来,然后放到另外一张表A.
查询的时候,先查询表A,对表A,进行排序.然后在查询具体表B.是否行得通?数据库中的索引的本质就是这个,把索引中的字段取出另外按序放在另一个数据集合中。
[/Quote]
这样呀,我还不知道这点呢,这样的话,我是在做多余的工作咯.
ACMAIN_CHM 2011-06-07
  • 打赏
  • 举报
回复
[Quote]另外,我昨天想到把需要排序的字段抽取出来,然后放到另外一张表A.
查询的时候,先查询表A,对表A,进行排序.然后在查询具体表B.是否行得通?[/Quote]数据库中的索引的本质就是这个,把索引中的字段取出另外按序放在另一个数据集合中。
lzj0470 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 acmain_chm 的回复:]
创建索引如下

create index xx on bbstitle(BoardID_1,BoardID_2,deleted,top,date)


另外你的语句写错了吧。建议贴问题出来 时严谨一些,否则别人分析反而是浪费时间。

select * from bbstitle
where BoardID_1=?
and BoardID_2=?
and deleted=0……
[/Quote]
是的,搞错了.谢谢啊.不是分组.是排序才对.group by 修改成 order by
select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;

另外,我昨天想到把需要排序的字段抽取出来,然后放到另外一张表A.
查询的时候,先查询表A,对表A,进行排序.然后在查询具体表B.是否行得通?
这样带来一个问题.连接数据库过多.
lzj0470 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wwwwb 的回复:]
select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;

这个是SQL语句?
不是标准的SQL语句
[/Quote]
?表示任意的.这里不填写具体参数而已.
ACMAIN_CHM 2011-06-07
  • 打赏
  • 举报
回复
创建索引如下

create index xx on bbstitle(BoardID_1,BoardID_2,deleted,top,date)


另外你的语句写错了吧。建议贴问题出来 时严谨一些,否则别人分析反而是浪费时间。

select * from bbstitle
where BoardID_1=?
and BoardID_2=?
and deleted=0
group by top desc,date desc limit ?,2;

GROUP BY 还是ORDER BY ?!
wwwwb 2011-06-07
  • 打赏
  • 举报
回复
select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;

这个是SQL语句?
不是标准的SQL语句
lzj0470 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wwwwa 的回复:]
索引情况是什么,SQL语句是什么
[/Quote]

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment
-------- ---------- --------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- -------
bbstitle 0 PRIMARY 1 id A 47539 (NULL) (NULL) BTREE
bbstitle 1 BoardID_2 1 BoardID_2 A (NULL) 333 (NULL) YES BTREE
bbstitle 1 titleType 1 titleType A (NULL) (NULL) (NULL) YES BTREE
bbstitle 1 deleted 1 deleted A (NULL) (NULL) (NULL) YES BTREE
bbstitle 1 BoardID_1 1 BoardID_1 A (NULL) 333 (NULL) YES BTREE
bbstitle 1 user_id 1 user_id A (NULL) 333 (NULL) YES BTREE

SQL语句 大概是这样.
select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;
lzj0470 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acmain_chm 的回复:]
引用会有where,也有order by你需要创建复合索引。
[/Quote]
好的.今晚测试一下.

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧