Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Rahul Raj
Starting Member
41 Posts |
Posted - 2015-03-16 : 13:17:31
|
Hi,I have to update a target table column by joining 4 tablesTable 1 : Job_ID, Job_name --> has only 20 recordsTable 2 : company_ID,Job_name Table 3 : company_ID,line_ID --> has many records for each company_ID around 8 lakhs.Table 4(Target Table) : line_ID,status_flagTable 1 and Table 2 has 1-2-1 relationshipTable 2 and Table 3 has 1-2-many relationshipTable 3 and Table 4 has 1-2-1 relationshipI want to update the status_flag with 'Y' in table 4. Below is the query which I have written please suggest if this can be optimized.update aset d.status_flag = 'Y'from table_4 aJOINtable_3 bon a.line_id = b.line_idJOINtable_2 con c.company_id = b.company_idJOINtable_1 dd.job_name = c.job_namePlease suggest if this can be optimized.Thanks for your help! |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-03-16 : 13:34:49
|
Ensure that each of the join conditions is indexed and then check the execution plan.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Rahul Raj
Starting Member
41 Posts |
Posted - 2015-03-16 : 14:39:49
|
Thanks Tara!Yes, the join condition is indexed. So, is the query correctly optimized or there's any chance of improvements. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-03-16 : 14:42:38
|
The query is very simple. As long as EACH of the columns in the various joins are indexed, then it should be efficient. You'll need to verify the execution plan.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Rahul Raj
Starting Member
41 Posts |
Posted - 2015-03-17 : 01:13:43
|
Thanks Tara!will check the explain plan. |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2015-04-02 : 12:04:29
|
Something must be missing here. Why join at all if there are no conditions??update table4set status_flag = 'Y' |
|
|
Rahul Raj
Starting Member
41 Posts |
Posted - 2015-05-23 : 18:08:19
|
Hi Scott/Tara,Thanks for your response. I forget to mention that there's a 'Date' filter condition in Table 1. Also, the relationship is as below :Table 1 and Table 2 has 1-2-1 relationshipTable 2 and Table 3 has 1-2-many relationshipTable 3 and Table 4 has 1-2-1 relationshipupdate aset d.status_flag = 'Y'from table_4 aJOINtable_3 bon a.line_id = b.line_idJOINtable_2 con c.company_id = b.company_idJOINtable_1 dd.job_name = c.job_namewhere d.start_date < 'some date value'After execution, I am getting some more records updated in Table 4 than expected. I suspect the join between table 3 and 4 is not correct e.g if the data is as below Table 3Company-ID line-ID111 1111 2111 3222 4222 5333 6333 7Table 4 line-ID status-Flag1 Y2 Y3 Y4 Y5 Y6 N7 Nso, if the company ID eligible are 111 and 222; so I want only line-id's 1,2,3 and 4,5 to be updated; but when i checked the count of records updated in table 4 then it's showing me more than 5 record in Table 4. Can someone please suggest me the query how I can identify the additional records which are updated in table 4 and how i can identify the corresponding records in Table 1.Your help is much appreciated!! Thanks in advance |
|
|
Rahul Raj
Starting Member
41 Posts |
Posted - 2015-05-24 : 23:24:53
|
hi,Can someone please suggest!many Thanks! |
|
|
|
|
|
|
|