当前位置:网站首页>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
)
边栏推荐
- Shell loop statement (for, while, until)
- 飞书更新招聘功能 候选人可选择面试时间
- ECCV 2022 | Towards Data Efficient Transformer Object Detectors
- TensorFlow学习记录(三):高阶操作 & 神经网络与全连接层
- COMSOL空气反应 模型框架
- 炫酷又高效的数据可视化大屏,做起来真的没那么难!丨极客星球
- Flutter强大的下拉筛选菜单gzx_dropdown_menu
- exness:美联储重现鹰派口吻,黄金承压面临转跌信号
- MySQL必知必会(初级篇)
- Django框架MySQL数据库到models模型的映射关系
猜你喜欢

技术分享| 小程序实现音视频通话

Focusing on data sources, data quality and model performance to build a credit profile of small and micro enterprises

什么是 DevOps?看这一篇就够了!

WPF---Grid布局讲解

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

Focus!2022 interview must brush 461 interview questions summary + interview + resume template

如何治理资源浪费?百度云原生成本优化最佳实践

他是“中台”之父,凭一个概念为阿里狂赚百亿

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

基于BiLSTM的回归预测方法
随机推荐
编辑器vscode Already included file name ‘xxx‘ differs from file name ‘xxx‘ only in casing报错
“蔚来杯“2022牛客暑期多校训练营4 N
高速电路PCB布局布线参考
Hit the interview!The latest interview booklet of Ali Jin, nine silver and ten is stable!
Why is Luo Zhenyu's A-share dream so difficult to fulfill?
LeetCode每日一题(858. Mirror Reflection)
The head module of the yolo series
Shell loop statement (for, while, until)
Do you understand the various configurations in the project?
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
力扣每日一题-第48天-345. 反转字符串中的元音字母
Yolov5 test and train own dataset
num_workers
String is a reference type
Small program on how to play in the construction of e-government service platform value
如何让 WPF 程序更好地适配 UI 自动化
【PHP实现微信公众平台开发—基础篇】第2章 微信公众账号及申请流程详解
MATLAB——图像分块
缓存中间件技术选型Memcached、MongoDB、Redis
COMSOL空气反应 模型框架