mysql order by优化问题

hzhsky1985 2009-11-21 01:03:52
explain select im.item_id from item_message im FORCE INDEX (item_id) where ( im.keyStr like '%拉%' ) and im.class = 1 and im.top = 0 and industry = '餐饮' order by im.item_id desc LIMIT 0 , 8

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE im index NULL item_id 4 NULL 8 Using where

以上sql,可以再继续优化吗,不知为什么,type哪里总是标识index,是因为我排序的字段item_id是唯一的值有关吗?
另外再问一个菜鸟问题,如果我不用FORCE INDEX (item_id),explain结果如下

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE im ref Industry,class,top Industry 63 const 1 Using where; Using filesort
这是为什么呢?虽然有了 Using filesort,但这里的type却是ref,非常不明白,而我用了FORCE INDEX 会使我其他索引,如
keyStr 的FULLTEXT 失效吗

附索引
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment
item_message 0 PRIMARY 1 id A 8 NULL NULL BTREE
item_message 1 regional 1 regional A NULL NULL NULL YES BTREE
item_message 1 city 1 city A NULL NULL NULL BTREE
item_message 1 pic1 1 pic1 A NULL NULL NULL BTREE
item_message 1 pic2 1 pic2 A NULL NULL NULL BTREE
item_message 1 create_date 1 create_date A NULL NULL NULL BTREE
item_message 1 end_date 1 end_date A NULL NULL NULL BTREE
item_message 1 Industry 1 Industry A NULL NULL NULL YES BTREE
item_message 1 class 1 class A NULL NULL NULL BTREE
item_message 1 top 1 top A NULL NULL NULL BTREE
item_message 1 item_id 1 item_id A NULL NULL NULL BTREE
item_message 1 keyStr 1 keyStr NULL NULL NULL NULL YES FULLTEXT

...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2009-11-21
  • 打赏
  • 举报
回复
由于你用了 like '%xxx%' 这种,所以你的keyStr的索引无用
hzhsky1985 2009-11-21
  • 打赏
  • 举报
回复
楼上的,那我keyStr的索引有用到吗
阿_布 2009-11-21
  • 打赏
  • 举报
回复
mysql一次查询只能使用一个索引。如果要对多个字段使用索引,建立复合索引。
ACMAIN_CHM 2009-11-21
  • 打赏
  • 举报
回复
你的表中现在没有什么记录,太少了,无法看出你的数据分布。

所以只能粗略建议你再建一个基于 (class,top,industry,item_id) 的复合索引。
ACMAIN_CHM 2009-11-21
  • 打赏
  • 举报
回复
建议楼主可以浏览一下。

http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#query-speed
7.2. 优化SELECT语句和其它查询
7.2.1. EXPLAIN语法(获取SELECT相关信息)
7.2.2. 估计查询性能
7.2.3. SELECT查询的速度
7.2.4. MySQL怎样优化WHERE子句
7.2.5. 范围优化
7.2.6. 索引合并优化
7.2.7. MySQL如何优化IS NULL
7.2.8. MySQL如何优化DISTINCT
7.2.9. MySQL如何优化LEFT JOIN和RIGHT JOIN
7.2.10. MySQL如何优化嵌套Join
7.2.11. MySQL如何简化外部联合
7.2.12. MySQL如何优化ORDER BY
7.2.13. MySQL如何优化GROUP BY
7.2.14. MySQL如何优化LIMIT
7.2.15. 如何避免表扫描
7.2.16. INSERT语句的速度
7.2.17. UPDATE语句的速度
7.2.18. DELETE语句的速度
7.2.19. 其它优化技巧
阿_布 2009-11-21
  • 打赏
  • 举报
回复
like 'xxx%' 这种搜索才用得上索引。
ACMAIN_CHM 2009-11-21
  • 打赏
  • 举报
回复
你这是单表查询, index 和 ref 对这个来说没有区别。
hzhsky1985 2009-11-21
  • 打赏
  • 举报
回复
那么我的type:index,这个问题要如何解决呢

56,678

社区成员

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

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