关于in和exists的优化问题,搜索无准确结果,在MYSQL
时间:2011-05-30
来源:互联网
如下搜索要62.75S,时间太久
select count(ID) from baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6 and goodsid in (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
尝试改成如下,无效,老是提示syntax to use near'34410001,34410002.....:
select a.goodsid from(select id,goodsid from usr_baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6) a where exists (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
会的请写一下吧,看在我结贴率100%。
select count(ID) from baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6 and goodsid in (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
尝试改成如下,无效,老是提示syntax to use near'34410001,34410002.....:
select a.goodsid from(select id,goodsid from usr_baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6) a where exists (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
会的请写一下吧,看在我结贴率100%。
作者: fuzongfan1 发布时间: 2011-05-30
在intime、goodsid 上建立复合索引试试
作者: wwwwb 发布时间: 2011-05-30
数据表结构不是我能改动。现有索引 KEY `idx_user` (`UserID`,`GoodsID`) USING BTREE,
KEY `idx_goods` (`GoodsID`),
KEY `idx_goods` (`GoodsID`),
作者: fuzongfan1 发布时间: 2011-05-30
第一个语句
select count(ID) from baggoodsinlog
where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00'
and inmode=6
and goodsid in (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
你只有KEY `idx_goods` (`GoodsID`),这个索引可用,如果速度很慢,则没有办法,除非可以添加 intime,inmode的索引。
select count(ID) from baggoodsinlog
where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00'
and inmode=6
and goodsid in (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
你只有KEY `idx_goods` (`GoodsID`),这个索引可用,如果速度很慢,则没有办法,除非可以添加 intime,inmode的索引。
作者: ACMAIN_CHM 发布时间: 2011-05-30
第二个语句,则根本语法不对,不知道你想实现什么逻辑功能,所以也无法帮你修改。
select a.goodsid from (
select id,goodsid from usr_baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6
) a
where exists (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
正常的EXISTS用法
select * from t where exists (select * from b where id=t.id)
select a.goodsid from (
select id,goodsid from usr_baggoodsinlog where intime>='2011-05-23 00:00:00' and intime <'2011-05-31 00:00:00' and inmode=6
) a
where exists (34410001,34410002,34410003,34410004,34410005,34410006,34410007,34410008,34410009,34410010,34410011,34410012,34410013,34410014,34410015,34410016);
正常的EXISTS用法
select * from t where exists (select * from b where id=t.id)
作者: ACMAIN_CHM 发布时间: 2011-05-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28