August 6, 2008 at 8:05 pm
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)
August 7, 2008 at 2:16 am
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.
August 7, 2008 at 2:49 am
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
August 7, 2008 at 2:56 am
August 7, 2008 at 1:58 pm
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.
August 7, 2008 at 2:28 pm
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.
August 7, 2008 at 2:50 pm
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.
August 7, 2008 at 3:35 pm
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
January 2, 2012 at 4:00 pm
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 🙂
January 2, 2012 at 4:21 pm
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.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
January 14, 2012 at 8:51 am
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply