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. mysql_replication - mysql读写分离时如果单台写库也无法满足性能怎么解决2. php - 两个表数据怎么插到一个模板中,并按时间排序?3. javascript - onclick事件点击不起作用4. css3动画 - Canvas、Css3、动画高手请进Html5问题请教5. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””6. css - html表格 td 宽度随着固定为内容宽度7. javascript - npm run build后调用api返回index.html8. python - type="datetime-local" 的 input 如何通过 django form.is_valid() 校验9. java - 为什么此私有静态变量能被访问呢?10. 找不到emmet

网公网安备