米兰的小铁匠z 2021-12-06 14:14 采纳率: 25%
浏览 40

MySQL 慢sql优化

表结构:

CREATE TABLE `user_rewards_record` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_uuid` varchar(32) CHARACTER SET utf8mb4 DEFAULT '0' COMMENT '用户id',
  `type` tinyint(4) DEFAULT '0' COMMENT '类型 0注册 1 签到',
  `rewards_type` tinyint(4) DEFAULT '0' COMMENT '奖励类型 0金币 1道具 2砖石',
  `amount` int(11) DEFAULT '0' COMMENT '奖励数量',
  `gift_uuid` bigint(20) DEFAULT '0' COMMENT '礼物uuid',
  `create_time` bigint(14) DEFAULT '0' COMMENT '创建时间',
  `is_pop_message` tinyint(4) DEFAULT '0' COMMENT '是否发送过弹窗消息',
  `update_time` bigint(14) DEFAULT '0' COMMENT '修改时间',
  `prop_uuid` varchar(32) CHARACTER SET utf8mb4 DEFAULT '' COMMENT '道具uuid, 当奖励类型为1时才有数据',
  PRIMARY KEY (`id`),
  KEY `idx_user_rewards_record_user_uuid` (`user_uuid`) USING BTREE COMMENT '用户uuid',
  KEY `idx_rewards_type` (`rewards_type`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=11554 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_icelandic_ci COMMENT='用户奖励记录表';

SQL:

SELECT
    user_uuid,
    `type`,
    rewards_type,
    amount,
    gift_uuid,
    create_time,
    is_pop_message 
FROM
    user_rewards_record 
WHERE
    `type` = 0 
    AND is_pop_message = 0 
    AND rewards_type IN ( 0, 2 ) 
    AND create_time > 1628218524000 
ORDER BY
    id DESC 
    LIMIT 1000
    
    

生产环境大概80w左右数据,

img

  • 写回答

2条回答 默认 最新

  • Yangyufan95 2021-12-06 14:29
    关注

    where条件的四个列,建一个联合索引试试

    评论

报告相同问题?

问题事件

  • 修改了问题 12月6日
  • 创建了问题 12月6日

悬赏问题

  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题