SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Script to select


Update Script to select

Author
Message
Ayie
Ayie
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 311
Hello everyone, any can help me to this script i want to enhance this script into fastest way, because if i run this query its to long to process, i dont know why, the total records without distinct is only 2000 records, anyone can help me please thanks

update ipadmin.GL_TXN set defunct = 'Y'
where gl_group_id in
(select distinct gl_group_id
from ipadmin.gl_txn with (nolock)
where transacted_on >= convert(datetime, '03/07/2008', 103)
and transacted_on < convert(datetime, '03/07/2008', 103) + 1
and belongs_to_hcare in (100) GROUP BY GL_GROUP_ID)

Maksymilian Mulawa
Maksymilian Mulawa
SSC Veteran
SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)

Group: General Forum Members
Points: 272 Visits: 222
Hi,

You could consider putting the data from the subquery to temp table


Create table #temp
(
gl_group_id Int
)

insert into #temp
select distinct gl_group_id
from ipadmin.gl_txn
where transacted_on >= convert(datetime, '03/07/2008', 103)
and transacted_on < convert(datetime, '03/07/2008', 103) + 1
and belongs_to_hcare in (100)



--You don't need the GROUP BY, as it makes no sense in this context
--If #temp has few hundred, thousands rows try creating index on gl_group_id column.


update ipa
set defunct = 'Y'
from ipadmin.GL_TXN ipa
inner join #temp t
ON ipa.gl_group_id = t.gl_group_id



Also one of the optimization options would be to create clustered index on ipadmin.gl_txn(transacted_on) column, this would speed up the datetime range query.

Cheers, Max

Check-out free open-source utility Sql Server Replication Explorer
Ayie
Ayie
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 311
Hi Mulawa,

This code is from VB, so i cannot create temp table for this, can can you give me the script using in visual basic as well?

thanks
Maksymilian Mulawa
Maksymilian Mulawa
SSC Veteran
SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)

Group: General Forum Members
Points: 272 Visits: 222
Ayie,

You should wrap this T-SQL code into stored procedure, or if you cannot create stored procedures, by using SqlCommand method ExecuteNonQuery you can submit full batch with temporary table code etc.

Cheers, Max

Check-out free open-source utility Sql Server Replication Explorer
tosscrosby
  tosscrosby
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3380 Visits: 5316
Unless I am totally missing something, wouldn't this do what you're looking for??

declare @lessdate = datetime
declare @greaterdate = datetime

set @lessdate = convert(datetime, '03/07/2008', 103)
set @greaterdate = convert(datetime, '03/07/2008', 103) + 1

update ipadmin.GL_TXN set defunct = 'Y'
where transacted_on >= @lessdate
and transacted_on < @greaterdate
and belongs_to_hcare in (100)

-- You can't be late until you show up.
Maksymilian Mulawa
Maksymilian Mulawa
SSC Veteran
SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)

Group: General Forum Members
Points: 272 Visits: 222
Your query is not an equivalent to the original one, as the result of subquery is just list of 'gl_group_id' and then you are updating records with these gl_group_id, not records filter by transacted_on and belongs_to_hcare.

Cheers, Max

Check-out free open-source utility Sql Server Replication Explorer
tosscrosby
  tosscrosby
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3380 Visits: 5316
As I'm leaving for the day and don't have time to create a more robust example, try this on Northwind. Ten bucks says you get the same results...If you don't agree, I'll create another example for you tomorrow.


select * from orders where OrderId in
(select distinct orderid from orders where orderdate >convert(datetime, '07/03/1996', 103)
and orderdate <= convert(datetime, '07/08/1996', 103) + 1)

select * from orders where orderdate >convert(datetime, '07/03/1996', 103) and orderdate <= convert(datetime, '07/08/1996', 103) + 1

-- You can't be late until you show up.
Maksymilian Mulawa
Maksymilian Mulawa
SSC Veteran
SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)SSC Veteran (272 reputation)

Group: General Forum Members
Points: 272 Visits: 222
Hi Terry,

How you can tell that gl_group_id column is a unique key in ipadmin.GL_TXN table?

I agree that your's Northwind example is working fine, but we know that orderid column is unique for orders table.

I'm not sure if you got my point? You are making assumption, which may or may not be true.

Cheers, Max

Cheers, Max

Check-out free open-source utility Sql Server Replication Explorer
rodneykee
rodneykee
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 0
Terry,

This is what I would've done:

update ipadmin.GL_TXN set defunct = 'Y'
from ipadmin.gl_txn
where transacted_on >= convert(datetime, '03/07/2008', 103)
and transacted_on < convert(datetime, '03/07/2008', 103) + 1
and belongs_to_hcare in (100)

Rodney :-)
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2139 Visits: 10394
rodneykee (1/2/2012)
Terry,

This is what I would've done:

update ipadmin.GL_TXN set defunct = 'Y'
from ipadmin.gl_txn
where transacted_on >= convert(datetime, '03/07/2008', 103)
and transacted_on < convert(datetime, '03/07/2008', 103) + 1
and belongs_to_hcare in (100)

Rodney :-)


This solution assumes that every member of a set of gl_group_id matches the filter. The query as originally written would update all members of a set of gl_group_id, regardless of how many members matched.
So, if there are 10 rows with gl_group_id = 3 and only 1 of these matches the filter, all 10 rows will be updated by OP's query. The query above would only update one row - as Max has pointed out.


update ipadmin.GL_TXN set defunct = 'Y' 
where gl_group_id in
(select
gl_group_id
from ipadmin.gl_txn
where transacted_on >= convert(datetime, '03/07/2008', 103)
and transacted_on < convert(datetime, '03/07/2008', 103) + 1
and belongs_to_hcare in (100)
GROUP BY GL_GROUP_ID)



- same as the original without the redundant DISTINCT.
Run it in SSMS and check the actual plan, compare time against running through application. At this point you don't know if it's the query or the app. If the run times are the same, then please post the actual plan, along with ddl and dml for table ipadmin.GL_TXN.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search