Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Update Script to select Expand / Collapse
Author
Message
Posted Wednesday, August 06, 2008 8:05 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 03, 2013 6:16 AM
Points: 26, 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)
Post #547965
Posted Thursday, August 07, 2008 2:16 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 02, 2011 3:40 AM
Points: 212, 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
Post #548070
Posted Thursday, August 07, 2008 2:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 03, 2013 6:16 AM
Points: 26, 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
Post #548088
Posted Thursday, August 07, 2008 2:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 02, 2011 3:40 AM
Points: 212, 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
Post #548089
Posted Thursday, August 07, 2008 1:58 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 7:31 AM
Points: 2,832, Visits: 5,316
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.
Post #548645
Posted Thursday, August 07, 2008 2:28 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 02, 2011 3:40 AM
Points: 212, 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
Post #548669
Posted Thursday, August 07, 2008 2:50 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 7:31 AM
Points: 2,832, Visits: 5,316
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.
Post #548687
Posted Thursday, August 07, 2008 3:35 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 02, 2011 3:40 AM
Points: 212, 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
Post #548717
Posted Monday, January 02, 2012 4:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, January 02, 2012 3:51 PM
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
Post #1228997
Posted Monday, January 02, 2012 4:21 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 11:41 PM
Points: 1,058, Visits: 5,732
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
Post #1229005
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse