当前位置:网站首页>sqlserver删除重复数据
sqlserver删除重复数据
2022-08-04 12:29:00 【lyk_dtf】
前言
老是有些项目忘记加唯一索引,如果项目正常运行倒也没事,但是一旦高并发或者服务器带宽波动,极可能出现重复数据,需要手动删除一下
最主要的还是别懒!索引一定要加,索引能将查询速度直接提高100倍
1、表结构
CREATE TABLE [dbo].[met_treasure_hunt_box_rec](
[rec_id] [int] IDENTITY(1,1) NOT NULL,
[userCode] [nvarchar](20) NULL,
[box_type] [int] NULL
)
userCode,box_type两个字段需要加联合唯一索引,语法如下:
@>_<当初加上也没下面什么事了,
alter table 表名 add unique index 索引名(字段1,字段2,字段3)
2、删除重复数据
大概的意思:
通过userCode、box_type 进行分组,查询分组中数量大于2的记录的主键id,每次只取每个分组中的第一条,得到一个id数组,然后delete in 删除
这个sql每执行一次,就会删除每个重复分组中的第一条
注:如果不加 top 1000 会报错,因为子查询使用了group by 必须要指定返回行数,根据数据量定就行
select top 1000 (
select top 1 rec_id from met_treasure_hunt_box_rec b where a.userCode=b.userCode and a.box_type=b.box_type
) rec_id from met_treasure_hunt_box_rec a group by a.userCode,a.box_type having count(1)>1 order by count(1) desc
)
边栏推荐
- 情人节浪漫3D照片墙【附源码】
- Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
- 缓存字符流
- Focus!2022 interview must brush 461 interview questions summary + interview + resume template
- 广告电商系统开发
- Linux-Docker-Mysql安装
- 数据中台建设(九):数据中台资产运营机制
- 编辑器vscode Already included file name ‘xxx‘ differs from file name ‘xxx‘ only in casing报错
- Django框架MySQL数据库到models模型的映射关系
- String is a reference type
猜你喜欢
MATLAB——图像分块
免费翻译软件哪个好用
什么是 DevOps?看这一篇就够了!
Hit the interview!The latest interview booklet of Ali Jin, nine silver and ten is stable!
聪明的儿子处理婆媳关系的方法(处理婆媳关系的方法)
第10章 模块和包
【黑马早报】尚乘数科上市13天,市值超阿里;北大终止陈春花聘用合同;新东方花近200亿退学费和遣散费;张小泉75%产品贴牌代工...
11. Network planning and design
Diffusion Models:生成扩散模型
LeetCode每日一题(858. Mirror Reflection)
随机推荐
FHQ-Treap 简介
RobotFramework二次开发(一)
backbone核心详解系列——RepVGG
罗振宇的A股梦,咋这么难圆?
What is DevOps?Enough to read this one!
“蔚来杯“2022牛客暑期多校训练营3 C
A Survey of Multi-Label Classification under Supervised and Semi-Supervised Learning
Geoffrey Hinton:深度学习的下一个大事件
基于BiLSTM的回归预测方法
String is a reference type
【水一个徽章】
拥有一台服务器,程序猿装X的开始
缓存中间件技术选型Memcached、MongoDB、Redis
移动跨端技术方案分析对比
力扣每日一题-第48天-345. 反转字符串中的元音字母
企业应当实施的5个云安全管理策略
高速电路PCB布局布线参考
yolo系列的head模块
用VbScript控制光驱
5 cloud security management strategies enterprises should implement