mysql 1kw数据 快速查询
问题描述

gift_id 有100多种
gift_id,user_id 建立了索引
只需要找拥有某一gift_id的用户的查询如下,而且速度非常快select * from user_gift where gift_id = 1004302 group by user_id
怎么快速找到 同时拥有 gift_id 为1004302和1004004的用户user_id呢 ?
问题解答
回答1:查 gift_id 为1004302的用户存list1 查 gift_id 为1004004的用户存list2 两个list取交集
回答2:select t.user_id, count(1) as c from table as twhere t.gift_id in(1004302, 1004004)group by t.user_idhaving count(1)>1
效率问题, 没数据, 也测试不了
如果, (user_id, gift_id) 是有可能重复的, 那在计算同时拥有之前还得将 (user_id, gift_id) 去重.
select t.user_id, count(1) as c from (select user_id, gift_id from table group by user_id, gift_id) as twhere t.gift_id in(1004302, 1004004)group by t.user_idhaving count(1)>1
相关文章:
1. Docker for Mac 创建的dnsmasq容器连不上/不工作的问题2. android 微信是如何实现即时更新好友头像的3. 用ie浏览器查看html文件显示中文乱码,用360浏览器就没事,这是什么情况/4. python - 用Pyinstaller打包时出现IndexError怎么回事?5. javascript - webpack1和webpack2有什么区别?6. <html>怎么变成<>的7. html5 - vue SPA 项目如果组件过多,岂不是打包的文件太大8. 随机数 - 有没有用Python生成n个不重复随机坐标的算法?9. html - H5 做微信微场景 下拉露底怎么解决10. python小白,问一个关于可变类型和不可变类型底层的问题

网公网安备