Solved

ms sql Update date a table scanning all records

Posted on 2014-10-12
6
156 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
[X]
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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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