免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2192 | 回复: 2
打印 上一主题 下一主题

SQL 语句优化求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-04 23:53 |只看该作者 |倒序浏览
mysql> explain SELECT cid,  major,  COUNT(1) AS cnt
FROM table_cm
WHERE major = 7 AND cid != 0
GROUP BY cid,  major
ORDER BY cnt DESC LIMIT 100\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tb_club_member
         type: ref
possible_keys: idx_major_cid
          key: idx_major_cid
      key_len: 4
          ref: const
         rows: 62928
        Extra: Using where; Using index; Using temporary; Using filesort
1 row in set (0.00 sec)

mysql> explain select seq,  jid,  cid,  corp_name,  corp_scale,  corp_quality,  corp_rank,  title,  name,  profession,  industry,  kind,  is_intern,  department,  city,  head_count, salary,  welfare,  type,  status,  post_status,  end_date,  validity,  start_date,  cv_eng,  update_date,  create_date,  email,  project_tag  
from table_jid
where  cid in ('712','237738')  AND  post_status = 1   AND  status = 1  AND  (end_date >= DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s'))   AND  (project_tag ='380' OR  project_tag like '%380,%' OR project_tag like '%,380%')
order by update_date desc limit 5\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tb_job
         type: range
possible_keys: cid
          key: cid
      key_len: 108
          ref: NULL
         rows: 1202
        Extra: Using where; Using filesort
1 row in set (0.00 sec)

这两条SQL语句还能优化么? 修改索引或是SQL都可以;

论坛徽章:
0
2 [报告]
发表于 2010-11-05 00:50 |只看该作者
第一个SQL虽然出现filesort和使用了临时表,是因为major和cid的选择性不好,除了重新schema之外,从索引上说效果还可以,使用了覆盖索引。
第二个语句建议拆分,至少把函数运算放到程序里面做,以及尽量从业务逻辑上考虑不要使用两边%,这个使用不上索引,如果实在需要搜索关键字,可以考虑fulltext.

论坛徽章:
0
3 [报告]
发表于 2010-11-05 10:15 |只看该作者
把  这部分功能放到 应用层来做

AND cid != 0
GROUP BY cid,  major
ORDER BY cnt DESC LIMIT 100\
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP