• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

这个sql怎么优化date>=’2009-1-1′ and date<‘2010-5-1’ 是否需要 改写为between (‘2009-1-1’,’2010

mysql 搞代码 7年前 (2018-05-31) 135次浏览 已收录 0个评论

这个sql如何优化date>=’2009-1-1′ and date<‘2010-5-1’ 是否需要 改写为between (‘2009-1-1’,’2010-
这个sql如何优化 

select * from a where type=? and date>=’2009-1-1′ and date<‘2010-5-1’ and t.status in (1,2,3,4,5,6..)

1 date>=’2009-1-1′ and date<‘2010-5-1’ 是否需要 改写为between (‘2009-1-1′,’2010-5-1’)
2 status 是否必须单独建立一个只有该字段的索引 当前在线表已经有索引(Type`,`status`)

(附后 
explain 
select * from a where type=? and date>=’2009-1-1′ and date<‘2010-5-1’ and t.status in (1,2,3,4,5,6..)

——-+——-+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+——————————————–+————————–+———+——-+——-+————-+
| 1 | SIMPLE | t | ref | idx_TE_sate,idx_TEnTypePard,idx_TE_d_tt_status | idx_TE__tt_status | 4 | const | 27563 | Using where |
+—-+————-+——-+——+——————————————–+————————–+———+——-+——-+————-+
1 row in set

可以看到他实际是走 索引(Type`,`status`) , 可以key_len=4| ref=const  

——解决方案——————–
date>=’2009-1-1′ and date<‘2010-5-1’ 

is the same to

date between ‘2009-1-1’ and ‘2010-5-1’
——解决方案——————–
date>=’2009-1-1′ and date<‘2010-5-1’
在date上加个索引会比较好。status,type这种重复率极高的字段,没有索引比有索引可能更快


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:这个sql怎么优化date>=’2009-1-1′ and date<‘2010-5-1’ 是否需要 改写为between (‘2009-1-1’,’2010

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址