需要sql ,查询优化一个表里面有上百万数据,需要查询某一天新增用户数

优雅de程序员 2016-04-15 09:01:18
现在有一个表,表数据上百万,需要查询某一天的新增用户数统计 。目前写的是,根据某天的时间查然后在查询之前的,相互比对今天的时间用户不在之前用户里面,用到了 not in 查询结果慢的不行,跪求大神有什么优化方案!
...全文
2035 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlh_150568 2017-07-03
  • 打赏
  • 举报
回复
引用 4 楼 weishaolin13x 的回复:
你的 not in完全可以不要 啊 ,
只用
SELECT COUNT(*) from order_info_ref as oif where oif.order_time>='2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59'

就行了,
楼主好 直接引用1楼的思路也能实现的吧
优雅de程序员 2016-04-15
  • 打赏
  • 举报
回复
谢谢了各位大哥,我通过另外一种方法实现的,我把两个时间段的用户ID 都查询出来,然后通过java 来比对 。 nowList.removeAll(historyList); 两个List 取差就OK 了,速度还可以,比直接在数据库查询快很多 。
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
max可以不要,只要min比较就行了,
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
select ca_user_id, max(order_time),min(order_time) from order_info_ref group by ca_user_id having max(order_time) between '2016-03-02 00:01:00' and '2016-03-02 23:59:59' and min(order_time) between '2016-03-02 00:01:00' and '2016-03-02 23:59:59'
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
select max(order_time),min(order_time) from order_info_ref group by ca_user_id having max(order_time) between '2016-03-02 00:01:00' and '2016-03-02 23:59:59' and min(order_time) between '2016-03-02 00:01:00' and '2016-03-02 23:59:59'
ACMAIN_CHM 2016-04-15
  • 打赏
  • 举报
回复
检查一下索引。
优雅de程序员 2016-04-15
  • 打赏
  • 举报
回复
引用 7 楼 weishaolin13x 的回复:
哦,明白了, select max(order_time),min(order_time) from order_info_ref having max(order_time) between '2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' and min(order_time) between '2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59'
mysql> select max(order_time),min(order_time) from order_info_ref having max(order_time) between '2016-03-02 00:01:00' and order_time<='2016-03-02 23:59:59' and min(order_time) between '2016-03-02 00:01:00' and order_time<='2016-03-02 23:59:59' ; 1054 - Unknown column 'order_time' in 'having clause' mysql> mysql> 说这个字段找不到。
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
加上 group by ca_user_id
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
哦,明白了, select max(order_time),min(order_time) from order_info_ref having max(order_time) between '2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' and min(order_time) between '2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59'
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
你的 not in完全可以不要 啊 , 只用 SELECT COUNT(*) from order_info_ref as oif where oif.order_time>='2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' 就行了,
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
where oif.order_time>='2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' where oi.order_time>='2016-03-01 00:01:00' and oi.order_time<='2016-03-01 23:59:59' 这两个 条件 是 互斥的,只能有一个条件满足 , 满足第一个条件,那第二个条件必然不成立, 所以 第二个条件完全可以不要。
优雅de程序员 2016-04-15
  • 打赏
  • 举报
回复
引用 3 楼 love468092550 的回复:
[quote=引用 1 楼 weishaolin13x 的回复:] 你 发 下你 写的 sql我看下 , 查询某一天的新增用户数统计 统计当天注册时间的 用户数就行了哦 ,
我们是分析日志数据的,更具用户购买数据统计的,因为可能上个星期这个用户买过东西,然后这个星期又买过东西,然后统计的时候这个用户就不能算新增。需要的是统计这个用户在之前的数据中是没有出现过的!然后这个用户才算新增!
优雅de程序员 2016-04-15
  • 打赏
  • 举报
回复
引用 楼主 love468092550 的回复:
现在有一个表,表数据上百万,需要查询某一天的新增用户数统计 。目前写的是,根据某天的时间查然后在查询之前的,相互比对今天的时间用户不在之前用户里面,用到了 not in 查询结果慢的不行,跪求大神有什么优化方案!
SELECT COUNT(*) from order_info_ref as oif where oif.order_time>='2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' and oif.ca_user_id NOT in (SELECT oi.ca_user_id from order_info_ref as oi where oi.order_time>='2016-03-01 00:01:00' and oi.order_time<='2016-03-01 23:59:59');
weishaolin13x 2016-04-15
  • 打赏
  • 举报
回复
你 发 下你 写的 sql我看下 , 查询某一天的新增用户数统计 统计当天注册时间的 用户数就行了哦 ,
优雅de程序员 2016-04-15
  • 打赏
  • 举报
回复
引用 1 楼 weishaolin13x 的回复:
你 发 下你 写的 sql我看下 , 查询某一天的新增用户数统计 统计当天注册时间的 用户数就行了哦 ,

SELECT COUNT(*) from order_info_ref as oif where oif.order_time>='2016-03-02 00:01:00' and oif.order_time<='2016-03-02 23:59:59' and oif.ca_user_id NOT in
(SELECT oi.ca_user_id from order_info_ref as oi where  oi.order_time>='2016-03-01 00:01:00' and oi.order_time<='2016-03-01 23:59:59');

56,687

社区成员

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

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