当前位置:网站首页>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
)
边栏推荐
- 如何做好企业数字化转型?这10份靠谱案例收藏了(附下载)
- LeetCode每日一题(858. Mirror Reflection)
- A Collection of Flutter Tutorials (2022 Edition)
- 244 page PDF!"2022 China cloud computing ecological blue book published
- 七夕还没选好礼物,快送这套美妆秘籍,保准没错~~
- 树莓派入门
- Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
- Share | technology integration electronic fence function of scheduling system
- 抗积分饱和PID控制器
- ES 节点2G内存分析
猜你喜欢

移动跨端技术方案分析对比

Practical sharing of distributed link tracking Jaeger + microservice Pig on Rainbond

【自动微分实现】反向OO实现自动微分(Pytroch核心机制)

划重点!2022面试必刷461道大厂架构面试真题汇总+面经+简历模板

A comprehensive understanding of MOS tubes, an article is enough

Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes

LeetCode Daily Question (858. Mirror Reflection)

ShanDong Multi-University Training #4 A、B、C、G

技术分享| 融合调度系统中的电子围栏功能说明

聪明的儿子处理婆媳关系的方法(处理婆媳关系的方法)
随机推荐
开发小程序插件如何实现盈利?
酷开科技 × StarRocks:统一 OLAP 分析引擎,全面打造数字化的 OTT 模式
高速电路PCB布局布线参考
ES 节点2G内存分析
Diffusion Models:生成扩散模型
缓存中间件技术选型Memcached、MongoDB、Redis
第10章 模块和包
如何让 WPF 程序更好地适配 UI 自动化
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
String is a reference type
How to develop small program plug-ins to achieve profitability?
MATLAB——图像分块
Js获取当前页面url参数
力扣每日一题-第48天-345. 反转字符串中的元音字母
OAuth2图文快速入门
图像分割方法
matlab串口读写
FHQ-Treap 简介
5 cloud security management strategies enterprises should implement
业务中我们如何更新缓存?Redis