CREATE TABLE `t_user_view` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', `user_id` bigint(20) DEFAULT NULL COMMENT '用户id', `viewed_user_id` bigint(20) DEFAULT NULL COMMENT '被查看用户id', `viewed_user_sex` tinyint(1) DEFAULT NULL COMMENT '被查看用户性别', `viewed_user_age` int(5) DEFAULT NULL COMMENT '被查看用户年龄', `create_time` datetime(3) DEFAULT CURRENT_TIMESTAMP(3), `update_time` datetime(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), PRIMARY KEY (`id`), UNIQUE KEY `idx_user_viewed_user` (`user_id`,`viewed_user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SELECT viewed_user_age as age, count(*) as num FROM t_user_view WHERE user_id = 1 AND viewed_user_age BETWEEN 18 AND 22 AND viewed_user_sex = 1 GROUP BY viewed_user_age
EXPLAIN SELECT viewed_user_age as age, count(*) as num FROM t_user_view WHERE user_id = 1 AND viewed_user_age BETWEEN 18 AND 22 AND viewed_user_sex = 1 GROUP BY viewed_user_age
ALTER TABLE `t_user_view` ADD INDEX `idx_user_age_sex` (`user_id`, `viewed_user_age`, `viewed_user_sex`);
END 十期推荐 【261期】面试官:说出几个你熟悉的 Zookeeper 命令 【262期】面试官:谈谈MySQL主从复制的原理 【263期】面试最后一问:你有什么要问我的吗? 【264期】盘点MySQL主从复制,在面试中能被问什么? 【265期】面试官:为什么Integer用==比较时127相等而128不相等? 【266期】面试官:Redis主从集群切换数据丢失问题如何应对? 【267期】10道经典MySQL面试题 【268期】美团面试题:当你的JVM 堆内存溢出后,其他线程是否可继续工作? 【269期】链表高频面试题(包括反转、合并、相交、分割、环长等) 【270期】面试官:Spring的Bean实例化过程应该是怎样的? ? ~