Query runs forever ORACLE - sql

Am trying to update the isdeleted column in one table when a record is not in the other user table . My problem is the query l have written runs forever. how best can l write the query below.
update TBLG2O_REGISTER a set a."isDeleted" = '1'
where a."UserID" not in (select k."UserID" from TBLG2O_USER k)

The answer is going to be database engine-specific. Performance characteristic differ wildly, across different database engines, and you failed to specify which DB server you are using.
However, subqueries are frequently MySQL's Achilles heel; I wouldn't be surprised that if this was MySQL. If so, the following approach should have better performance characteristics with MySQL:
update TBLG2O_REGISTER a left join TBLG20_USER k using(UserID)
set a.isDeleted = '1' where k.UserID is null;

Finally got it to work Thank you for your help
Update TBLG2O_REGISTER a set a."isDeleted" = '1' where a."UserID" in (select p."UserID"
from TBLG2O_REGISTER p left join TBLG2O_USER k on p."UserID" =k."UserID"
where k."UserID" is null)

Related

SQL Query Optimization - SQL Server 2005

I have to write a query such as the one below. This query I have currently works, but I am curious if there is a better implementation. This will be placed inside of a much larger script so I want to ensure it runs as fast as possible.
CASE
WHEN EXISTS (
SELECT DISTINCT x.User_Index
FROM ActiveUser_s
INNER JOIN Entity_s
ON ActiveEntity_s.Entity_Index = Entity_s.Entity_Index
INNER JOIN x
ON Entity_s.User_Index = x.User_Index
WHERE ActiveUser_s.Active = 1 AND Entity_s.User_Index = x.User_Index
)
then 'Yes'
ELSE 'No'
END AS [Is Real]
Your script fragment provides little informations for a solution "I want to ensure it runs as fast as possible."
Some advice:
do not use DISTINCT, use SELECT x.User_Index ....
create indexes for database tables ActiveUser_s, Entity_s and x

Tuning Oracle Query for slow select

I'm working on an oracle query that is doing a select on a huge table, however the joins with other tables seem to be costing a lot in terms of time of processing.
I'm looking for tips on how to improve the working of this query.
I'm attaching a version of the query and the explain plan of it.
Query
SELECT
l.gl_date,
l.REST_OF_TABLES
(
SELECT
MAX(tt.task_id)
FROM
bbb.jeg_pa_tasks tt
WHERE
l.project_id = tt.project_id
AND l.task_number = tt.task_number
) task_id
FROM
aaa.jeg_labor_history l,
bbb.jeg_pa_projects_all p
WHERE
p.org_id = 2165
AND l.project_id = p.project_id
AND p.project_status_code = '1000'
Something to mention:
This query takes data from oracle to send it to a sql server database, so I need it to be this big, I can't narrow the scope of the query.
the purpose is to set it to a sql server job with SSIS so it runs periodically
One obvious suggestion is not to use sub query in select clause.
Instead, you can try to join the tables.
SELECT
l.gl_date,
l.REST_OF_TABLES
t.task_id
FROM
aaa.jeg_labor_history l
Join bbb.jeg_pa_projects_all p
On (l.project_id = p.project_id)
Left join (SELECT
tt.project_id,
tt.task_number,
MAX(tt.task_id) task_id
FROM
bbb.jeg_pa_tasks tt
Group by tt.project_id, tt.task_number) t
On (l.project_id = t.project_id
AND l.task_number = t.task_number)
WHERE
p.org_id = 2165
AND p.project_status_code = '1000';
Cheers!!
As I don't know exactly how many rows this query is returning or how many rows this table/view has.
I can provide you few simple tips which might be helpful for you for better query performance:
Check Indexes. There should be indexes on all fields used in the WHERE and JOIN portions of the SQL statement.
Limit the size of your working data set.
Only select columns you need.
Remove unnecessary tables.
Remove calculated columns in JOIN and WHERE clauses.
Use inner join, instead of outer join if possible.
You view contains lot of data so you can also break down and limit only the information you need from this view

Updating a set of values using criteria from multiple tables

Given a specific set of rows that I've queried I want to update the "ifSpeed" value on each of them to a static value of 1000000000.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table. That query is the first block of code. The second is one of my attempts at updating the values. I've also looked into subqueries, but my SQL is very rusty as it has been about 8 years since I've worked with it.
SELECT ifSpeed
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
UPDATE master_dev.device_interfaces
SET ifSpeed = '1000000000'
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
My expectation is that all rows that exist in my SELECT query have their ifSpeed updated to 1000000000.
What actually happens is simply an error. I'm using a restricted interface to query the DB and it only provides rows affected, the data from the rows or simply "ERROR". Very helpful... I know.
in SSMS try Below
---- Updated ---
UPDATE MDDI
SET ifSpeed = '1000000000'
FROM master_dev.dbo.device_interfaces AS MDDI
JOIN master_dev.dbo.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed = 10000000
AND MDDITM.tag_id = 13
The syntax of UPDATE that has a FROM clause is rather cumbersome. Luckily in SQL Server there is a better way.
I usually use CTE (common-table expression) in this case, which makes the query very easy to write and read and verify that it works as intended.
So, you have your complex SELECT statement that returns all the rows that you want to update. Great.
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
;
Now, wrap it into CTE and update the CTE:
WITH
CTE
AS
(
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
)
UPDATE CTE
SET ifSpeed = '1000000000'
;
You can write SELECT * FROM CTE instead of the line UPDATE CTE SET ... before performing an actual update to verify that you are selecting correct rows.
Thanks for all the input on this! Unfortunately I had two different teams give me two different answers. While I supposedly was able to do this after escalating further up the chain the engineering team on the backend put up a brick wall saying don't risk the DB tables. That decision baffles me based on what's affected, but not my call to make I suppose.
That being said they did let me know that the original code was fine and likewise the variation that maSTAShuFu provided.
#Vladimir - Sorry didn't get to try your's out, but I'm definitely keeping it in my back pocket. Thank you!

Issue with joins in a SQL query

SELECT
c.ConfigurationID AS RealflowID, c.companyname,
c.companyphone, c.ContactEmail, COUNT(k.caseid)
FROM
dbo.Configuration c
INNER JOIN
dbo.cases k ON k.SiteID = c.ConfigurationId
WHERE
EXISTS (SELECT * FROM dbo.RepairEstimates
WHERE caseid = k.caseid)
AND c.AccountStatus = 'Active'
AND c.domainid = 46
GROUP BY
c.configurationid,c.companyname, c.companyphone, c.ContactEmail
I have this query - I am using the configuration table to get the siteid of the cases in the cases table. And if the case exists in the repair estimates table pull the company details listed and get a count of how many cases are in the repair estimator table for that siteid.
I hope that is clear enough of a description.
But the issue here is the count is not correct with the data that is being pulled. Is there something I could do differently? Different join? Remove the exists add another join? I am not sure I have tried many different things.
Realized I was using the wrong table. The query was correct.

Trying SQL table update matching on string field

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.
I only have a string field that matches between the two tables.
tblJP AND tblMaster
I need to update tblJP.LangString with tblMaster.Long_text when
tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'
Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.
A simple update with an INNER JOIN should do the trick.
UPDATE tblJP
SET tblJP.LangString = tblMaster.Long_Text
FROM tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE tblMaster.Lang = 'jp'
WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.
You could also use MERGE
MERGE INTO tblJP
USING (SELECT *
FROM tblMaster
WHERE Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
UPDATE SET LangString = SOURCE.Long_Text;
In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.