最起码地,判断是否存在记录,用 if exist(select * from ........ where .......) select 1 else select 0或者类似select top 1 from ..... where ......这样的形式,怎么能用 count 统计函数?谁教你的 sql 啊?
select top 1 from ==》 select top 1 1 from
top 1
你的数据应该分表管理了, 第二 你的SQL 是有问题的第一个写法 应该是 if EXISTS (select * from 表 where phone='130xxxxxx') begin update end else begin insert end 第二种 @@ROWCOUNT 返回受上一语句影响的行数 UPDATE 表 SET creattime = 时间 WHERE phone='130xxxxxx' IF @@ROWCOUNT = 0 insert剩下的你自己考虑吧, 这个只是语句的变化, 但是我觉得 对 SQL来说 千万条没啥事啊, 要不就是你电脑太烂了。
merge into
select top 1 1 FROM Cust(nolock) where phone='130xxxxxx'脏读 应该不死锁
直接插入不可以吗INSERT INTO Table select * FROM Cust a where Not Exists (Select CustID From Cust b Where phone='130xxxxxx' and a.CustID=b.CustID)
如果已经有索引列还这么慢。建议升级硬件或者分库。
可以使用Redis之类的NoSQL库
内存操作总比数据库来得快
缺点是耗内存
if EXISTS (select * from 表 where phone='130xxxxxx')
begin
update
end
else
begin
insert
end
第二种
@@ROWCOUNT 返回受上一语句影响的行数
UPDATE 表 SET creattime = 时间 WHERE phone='130xxxxxx'
IF @@ROWCOUNT = 0
insert剩下的你自己考虑吧, 这个只是语句的变化, 但是我觉得 对 SQL来说 千万条没啥事啊, 要不就是你电脑太烂了。
1.能建索引就建索引!
2.不要用count这种判断,用in或者exsits判断,内表比外表大或者两者大小差不多就用exsits判断!为什么这么写,底层逻辑自行百度!