473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Conditionally UPDATE a column in one big table from a column in another big table

Hi, all.
Here is my problem:

Let's say i have table1 with columns a,b,c and table2 with cols x,y,z. Some of columns contain the same type of data from one table to the other, but others are totally different. Both tables have a very large number of rows (~70k).

Now, i need to replicate in table1 a column that exists only in table2, so i create a new column in table1, let's name it d, and i want to conditionally import column z from table2 in column d from table1. Conditionally means where a = x and b=y, for example.

i created this query:
Expand|Select|Wrap|Line Numbers
  1. UPDATE table1
  2. SET d = (SELECT z FROM table2 WHERE table1.a=table2.x AND table1.b=table2.y)
i tested this query on a similar set of tables with a few rows and it does the job, but really slow (took ~1 minute to process 300 rows). I'm sure that if i run it on the 2 big tables it will take forever to complete or may not even finish at all.

any better ideas, faster alternatives ? (deleting and re-creating tables in not an option)
thanks

PS: i use mysql 5.0.26 on Win XP SP2 and MySQL Query Browser. I also tried to query from a PHP script, takes just as much.
May 17 '07 #1
4 2838
pradeep kaltari
102 Expert 100+
Hi, all.
Here is my problem:

Let's say i have table1 with columns a,b,c and table2 with cols x,y,z. Some of columns contain the same type of data from one table to the other, but others are totally different. Both tables have a very large number of rows (~70k).

Now, i need to replicate in table1 a column that exists only in table2, so i create a new column in table1, let's name it d, and i want to conditionally import column z from table2 in column d from table1. Conditionally means where a = x and b=y, for example.

i created this query:
Expand|Select|Wrap|Line Numbers
  1. UPDATE table1
  2. SET d = (SELECT z FROM table2 WHERE table1.a=table2.x AND table1.b=table2.y)
i tested this query on a similar set of tables with a few rows and it does the job, but really slow (took ~1 minute to process 300 rows). I'm sure that if i run it on the 2 big tables it will take forever to complete or may not even finish at all.

any better ideas, faster alternatives ? (deleting and re-creating tables in not an option)
thanks

PS: i use mysql 5.0.26 on Win XP SP2 and MySQL Query Browser. I also tried to query from a PHP script, takes just as much.
Hi bubulle,
Welcome to TSDN.
I can suggest an alternative.
Expand|Select|Wrap|Line Numbers
  1. UPDATE table1 LEFT OUTER JOIN table2 ON table1.a=table2.x AND table1.b=table2.y
  2. SET table1.d=table2.z
  3.  
You can also add WHERE table2.z IS NOT NULL. Please get back with performance details. I hope this helps.

Regards,
Pradeep
May 17 '07 #2
Hi bubulle,
Welcome to TSDN.
I can suggest an alternative.
Expand|Select|Wrap|Line Numbers
  1. UPDATE table1 LEFT OUTER JOIN table2 ON table1.a=table2.x AND table1.b=table2.y
  2. SET table1.d=table2.z
  3.  
You can also add WHERE table2.z IS NOT NULL. Please get back with performance details. I hope this helps.

Regards,
Pradeep
Hi, Pradeep.
Thanks for the welcoming and for the suggestion. I tested it on the 300 rows tables and it took 55 seconds, compared to the ~63 needed to run it my way. So i worked up the courage and ran it on the big tables. Now, after all these hours, it finished processing the 73k rows.

thanks again for the tip :)
Bob.
May 17 '07 #3
Motoma
3,237 Expert 2GB
Hi, Pradeep.
Thanks for the welcoming and for the suggestion. I tested it on the 300 rows tables and it took 55 seconds, compared to the ~63 needed to run it my way. So i worked up the courage and ran it on the big tables. Now, after all these hours, it finished processing the 73k rows.

thanks again for the tip :)
Bob.
Setting up Indexes should speed this up dramatically.
May 17 '07 #4
pradeep kaltari
102 Expert 100+
Hi, Pradeep.
Thanks for the welcoming and for the suggestion. I tested it on the 300 rows tables and it took 55 seconds, compared to the ~63 needed to run it my way. So i worked up the courage and ran it on the big tables. Now, after all these hours, it finished processing the 73k rows.

thanks again for the tip :)
Bob.
Hi Bob,
Sorry I missed out: adding Indexes to the searchable columns will definately speed up the process.

Regards,
Pradeep
May 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Caroline | last post by:
I need to update a table by copying a column from another table (having the same structure, but on another database), from the record having the same primary key. 1 - What is the correct...
8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
8
by: Yaro | last post by:
Hello Could anyone tell how can I drop table conditionally (UDB 8.2 Win) ? I have a script file create table_1@ create table_2@ etc.... I need add to this script statement like that
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.