Solved

ms sql Update date a table scanning all records

Posted on 2014-10-12
6
148 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql join/ assign small # first 10 83
Very interesting Access query problem. 13 70
Query 14 54
query logins with details regarding database users and privilege level 4 35
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now