The following sp:
I have a stored procedure which runs anywhere from 1/2 minute to 4 hours (during nightly processing):
update tableA
set tableA.Other_Flag_50 = isnull(Staging.other_flag_50, 0)
from tableA
inner join (
select acct_nbr,
appl_code,
Other_Flag_50
from tableB
) Staging on tableA.lnhist_acct_nbr = Staging.acct_nbr
and tableA.lnhist_appl_code = Staging.appl_code
I ran Blocking reports in Profiler for 2 nights in a row, first at 10 minutes interval then at 5 minutes. The stored procedure never shows up as being blocked (but it blocks other queries).
Any ideas on optimizing this? Would creating a view with the join help? (acct_nbr, appl_code, Other_Flag_50 from tableB) Thanks!!
Have you tried doing the INNER JOIN directly to tableB?
UPDATE tableA
SET tableA.Other_Flag_50=isnull(tableB.other_flag_50,0)
FROM tableA
INNER JOIN tableB
ON tableA.lnhist_acct_nbr = tableB.acct_nbr
AND tableA.lnhist_appl_code = tableB.appl_code
Try using rowlock to prevent locking a whole table.
update tableA with (rowlock)
...
EDIT
Not sure about other RDBMS but the answer I provided works for SQL Server
Related
I am facing a weird experience with a query which is for handling data from ADO. Here is the view
ALTER view [dbo].[v_Missing_Pepics] as
select ProjectName Title,PM.ProjectCode,LifeCycle,dbo.fn_kip_ado_status_mapping(LifeCycle,PM.ProjectCode) KipStatus,TeamCode,ISNULL((select top 1 value from string_split(TA.AreaPath,'\') where LTRIM(value) like 'Domain%'),'Automation') Domain,DM.DomainName KipDomain,TeamName
from [dbo].[v_ProjectMaster_Latest] PM
left outer join areapath_mapping TA on TA.KeyedInTeamCode=PM.TeamCode
left join v_portfolio_epics PE on PE.ProjectCode=PM.ProjectCode
inner join domain_master DM on DM.DomainCode=PM.DomainCode
where ProjectActive = 'yes' and LifeCycle not in ('In Close-Down', 'Completed','Withdrawn')
and PE.ProjectCode is null and DM.DomainName not in ('Data Power') and PM.ProjectCode not like 'EXP%'
GO
When I try to execute the query like this
Select * from v_Missing_Pepics
It took more than 80sec to finish. But when I copy the query alone (Within the view), it executes in just 1 second.
I don't understand why??
I am working in Azure SQL.
Try
sp_recompile 'v_Missing_Pepics'
And see if that resolves the performance problems with the view.
I have an sql query to update a column through a count result from a view, this is my query
UPDATE [dbo].[Table]
SET [ColumnName] = (select Count(View.Column) from View
where table.Column = View.ColumnN and View.Column1>0)
WHERE [dbo].[Table].Column in (select Column from View)
this query is taking 1 second when i execute it in my local SqlServer but when i executed in the server where the application is deployed it takes about 1.36 minute , is there something wrong i'm doing :)
Thnks in advance
It's common. Sometimes records from 10 to 100 will cause efficiency loss apparently. Try to group first then use join to combine update records. Your columns relation is not clear for me, just for reference.
UPDATE [dbo].[Table]
SET [ColumnName] = d.Result
FROM (SELECT View.ColumnN, COUNT(View.Column) AS Result
FROM View
WHERE View.Column1 > 0
GROUP BY View.ColumnN
) d INNER JOIN
[dbo].[Table] t
ON d.ColumnN = t.Column
I am trying to do an Update query with a left join in SQL Server 2005 but for some reason it's not working.
[EDIT: "not working" - sorry, this means that the update isn't running. Records aren't updated. I'm running this Update query in a SRSS report, because the CRM I'm using doesn't allow direct access to the database - it runs Selects, Updates, Inserts, Deletes fine but gives you no useful error messages if things don't work as expected. Operating in the dark unfortunately!]
My SQL statement is this:
UPDATE [tblSlots]
SET [tblSlots].[PublishedStartTime] = '10:00'
FROM tblSlots
LEFT JOIN tblDays ON tblSlots.SlotDayID = tblDays.DayID
WHERE tblDays.Published = 1
If I take out the LEFT JOIN line and just filter on e.g. a tblSlots.SlotID, the update works fine.
But I'd like to be able to update the slots on ALL published days at once.
(I tried it as just a JOIN, but that didn't work either...)
I'm sure it's something terribly obvious...
Your query syntax looks fine to me, and as you said it runs fine until you try to do an update. This is a shot in the dark, but if the issue is with updating while selecting from multiple tables, then perhaps simply changing your query to not join to the tblDays might work.
UPDATE [tblSlots]
--SET [tblSlots].[PublishedStartTime] = '10:00'
SET [tblSlots].[PublishedStartTime] = (select SomeValue from tblDays where DayID = SlotDayID) --If a value is needed from Day table
FROM tblSlots
Where SlotDayID in (select DayID from tblDays WHERE Published = 1 and DayID is not null)
you can try:
UPDATE [tblSlots]
SET [tblSlots].[PublishedStartTime] = '10:00'
FROM [tblSlots] ,
(
SELECT DayID FROM
[tblSlots]
LEFT JOIN tblDays
ON tblSlots.SlotDayID = tblDays.DayID
WHERE tblDays.Published = 1
) AS [Data_Days]
WHERE
[Data_Days].DayId = [tblSlots].SlotDayID
I'm trying to see if there are any rows in table A which I've missed in table B.
For this, I'm using the following query:
SELECT t1.cusa
FROM patch t1
LEFT JOIN trophy t2
ON t2.titleid = t1.titleid
WHERE t2.titleid IS NULL
And the query worked before, but now that the trophy table has nearly 200.000 rows, it's extremely slow. I've waited 5 minutes for it to execute but it was still loading and timed out eventually.
Is there any way to speed this query up?
Adding Indexes to titleId on both tables (but especially t2) is the quickest way to get better performance. 200K records is nothing for SQL Server.
Try this and it might perform a bit better!
SELECT t1.cusa
FROM patch t1
WHERE NOT EXISTS (SELECT 1
FROM trophy t2
WHERE t2.titleid = t1.titleid );
I am a complete beginner to SQL Server, and I have reached my limit.
Currently I am using a script to update a table from another table using a column. Since both databases are assigned to 2 different 3rd party software, I created a .bat script to use for task manager in windows server, that way it can update every 10 minutes.
While this is tested and works, I feel there has to be a way to create a relationship between the two databases without having to use the task.
UPDATE therefore.dbo.thecat51
SET num_factura =
(SELECT therefore.dbo.documentos.num_factura
FROM therefore.dbo.Documentos
WHERE therefore.dbo.thecat51.num_albaran=therefore.dbo.documentos.num_albaran)
WHERE therefore.dbo.thecat51.num_albaran =
( SELECT therefore.dbo.documentos.num_albaran
FROM therefore.dbo.Documentos
WHERE therefore.dbo.thecat51.num_Albaran = therefore.dbo.documentos.num_albaran)
Also, we are using SQL Server Express, so I don't have the option to create a scheduled job.
You can do the UPDATE with an INNER JOIN to perform the update you need:
UPDATE A SET
num_factura = B.num_factura
FROM therefore.dbo.thecat51 A
INNER JOIN therefore.dbo.Documentos B
ON A.num_albaran = B.num_albaran
Use an INNER JOIN between your two tables. At the time I posted this, you had not told us which RDBMS you are using so I will give answers for SQL Server and MySQL:
SQL Server:
UPDATE t1
SET t1.num_factura = t2.num_factura
FROM therefore.dbo.thecat51 AS t1
INNER JOIN therefore.dbo.Documentos AS t2
ON t1.num_albaran = t2.num_albaran
MySQL:
UPDATE therefore.dbo.thecat51 AS t1
INNER JOIN therefore.dbo.Documentos AS t2
ON t1.num_albaran = t2.num_albaran
SET t1.num_factura = t2.num_factura