Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ms sql Update date a table scanning all records

Posted on 2014-10-12
6
151 Views
Last Modified: 2014-10-17
I have 2 tables.

t1 has 30,000,000 records
t2 has 20,000 records

I do this

update table1 set fieldx=1 from table2 where t1.email=t2.email

t2 is indexed on email
t1 is indexed on email

I realize that the process has to scan the 30,000,000 records to get the matches

I am sure there must be a way to run a query where I scan the 20,000 records of t2 and update t1. Which would be much faster.

In Foxpro I would run al loop for each record in t2 find if the record was in t1 and update it.

Any ideas?
0
Comment
Question by:robrodp
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40375721
update table1 set fieldx=1 from table2 where t1.email=t2.email

t2 is indexed on email
t1 is indexed on email

I realize that the process has to scan the 30,000,000 records to get the matches
No, it'll use the index to only process the records matching.

You may check the execution plan in SSMS, but I'm quite confident it'll not show a table scan on t1.

Bye, Olaf.

PS: As you don't limit the query on t2, the execution plan will have a table scan on that, maybe also use the email index to get all email values only.
0
 

Author Comment

by:robrodp
ID: 40375737
Attached find the execution plan
sql.png
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40375742
Well, on the right hand side you see both indexes are used for an index scan and index seek.

carlosf is the smaller table, isn't it? it's email index is read in, and the prefix.email index is seeked in to find the records to update. There is no full scan of all prefix records.

Everythings OK. If this takes too long, check out the overall server load and resources. The query is not the problem.

Bye, Olaf.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Accepted Solution

by:
robrodp earned 0 total points
ID: 40375760
thx... I will set this as a solution, though sometime I receive psosts saying that it is not. Well ist is good enough foe me.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 40375787
Well, first of all an index scan is not a table scan. It loads the smaller index with email field only, so that is faster than a table scan. Then of course, what do you expect when not having any condition on the t2 (carlosf) table? It has to load all data from it, loading the email index instead of scanning the whole table already is the best SQL Server can do.

Bye, Olaf.
0
 

Author Closing Comment

by:robrodp
ID: 40386220
Thx
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help Required 2 39
sql server insert 12 39
sql server query 12 25
reccommendations for a free msft sql query manager? 4 27
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question