Query blocking entirely MySQL server - mysql

I try to a run a specific query. However, when I execute it, the MySQL server doesn't respond anymore.
There is approximately 30000 rows in the table base_contrats_actifs but I don't know if this is a problem.
Here is the query :
UPDATE
base_contrats_actifs a
SET
a.code_indice = (
SELECT
MAX(g.code_indice)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
),
a.flag_bailleur_locataire = (
SELECT
MAX(g.flag_bailleur_locataire)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
),
a.compte_client = (
SELECT
MAX(g.compte_client)
FROM
base_gid g
WHERE
a.num_version = g.num_version_contrat
)
Can you see if there is an error ? If not, is there any way to debug the query ?

I don't know exactly why your update is non performant, but given the number of correlated subqueries you have, I'm not surprised. Try rewriting it as an update join:
UPDATE base_contrats_actifs a
INNER JOIN
(
SELECT
num_version_contrat,
MAX(code_indice) AS max_code_indice,
MAX(flag_bailleur_locataire) AS max_flag_bailleur_locataire,
MAX(compte_client) AS max_compte_client
FROM base_gid
GROUP BY num_version_contrat
) g
ON a.num_version = g.num_version_contrat
SET
a.code_indice = g.max_code_indice,
a.flag_bailleur_locataire = g.max_flag_bailleur_locataire,
a.compte_client = g.max_compte_client;

Related

How to get records that match value or exist in another table?

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)

Need records from one table if value matches or if value exists in another table [duplicate]

I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)

Complex UPDATE query in SQL Server 2008, rewrite from Oracle 10g

I need to re-write the following Oracle 10g query to work in SQL Server 2008
It's an update query, where some field are retrieved from a SELECT and some are given (from code).
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID"
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
),
"SESSION_ID" = 1553245736,
"USER_ID" = 13
WHERE EXISTS ( SELECT * FROM ... )
Note that I need to use the UPDATE SET ... WHERE EXIST ... structure for compatibility with Oracle (query are automatically built by a QueryBuilder class for each specific DBMS).
I also cannot write:
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID", "SESSION_ID", "USER_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID", 1553245736, 13
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
)
WHERE EXISTS ( SELECT * FROM ... )
because (as per this old thread Oracle "Cannot update to NULL") it returns an error if the SELECT does not fetch any record.
Thanks in advance!
You need to join update query in this case. Please see syntax below. <Your condition here> Replace this section with your join condition here.
UPDATE m
SET
GLIST_ID = r.GLIST_ID
,GLIST_VALUE_ID = r.GLIST_VAL_ID
, SESSION_ID= 1553245736
, USER_ID = 13
from BMAN_SQL.CELLS_GLIST m
inner join BMAN_SQL.GLISTS_VAL_UOR r on <Your condition here>
WHERE
r.UOR_ID=3 AND (r.GLIST_CODE='X') AND
EXISTS ( SELECT * FROM ... )
EDIT
UPDATE BMAN_SQL.CELLS_GLIST
SET
GLIST_ID = (SELECT TOP 1 GLIST_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,GLIST_VALUE_ID = (SELECT TOP 1 GLIST_VAL_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,SESSION_ID = 1553245736
,USER_ID = 13
WHERE EXISTS ( SELECT * FROM ... )

Update a single table based on multiple tables - appears to works MySql not ACCESS 2003

Mysql Version Works
UPDATE results SET rCARRIER = (
SELECT cellCarrierName
FROM tblImportedTempTable, user, cellCarrier
WHERE
userEmployeeNumber = tblImportedTempTable.EMPLOYEENUMBER
AND userId = results.rUserId
AND results.rPHONENUMBER = tblImportedTempTable.PHONENUMBER
AND CARRIER = cellCarrierId )
I have written this sql that works fine in MySql(above) and fails in access 2003(below) any suggestions? Is one or both of the 2 nonstandard sql? Does Access hav an admin problem?
Sorry the field and table names are diferent this is the ACCESS version.
Access version
UPDATE tblWorkerPhoneNumber SET tblWorkerPhoneNumber.PhoneCarrier = (
SELECT PhoneCarrierType.CarrierName
FROM tblImportedPhoneCarrier, tblWorkerMaster, PhoneCarrierType
WHERE
tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
AND tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
AND tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
AND tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID )
Error Message
Operation must use and updateable query
Thanks
In MS Access, something like this:
UPDATE tblWorkerPhoneNumber
INNER JOIN tblWorkerMaster ON tblWorkerMaster.WorkerID = tblWorkerPhoneNumber.WorkerID
INNER JOIN tblImportedPhoneCarrier ON tblWorkerPhoneNumber.PhoneNumber = tblImportedPhoneCarrier.Cell
INNER JOIN PhoneCarrierType ON tblImportedPhoneCarrier.CarrierCode = PhoneCarrierType.CarrierID
SET tblWorkerPhoneNumber.PhoneCarrier = PhoneCarrierType.CarrierName
WHERE tblWorkerMaster.EmployeeNumber = tblImportedPhoneCarrier.Emp
(Might need to change the join conditions; I'm not familiar with your schema)

hibernate, mysql

i have the following hql query:
UPDATE TaskAssessment taskAssessment
SET taskAssessment.activeFlag = false
WHERE taskAssessment IN
(
SELECT taskAssessment2
FROM TaskAssessment taskAssessment2
Where taskAssessment2.activeFlag = true
AND taskAssessment2.patient.id
AND taskAssessment2.needsLevel.careNeed = :careNeed
)
but its giving me errors:
You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause
could anyone help me to correct the query for mysql and hibernate. thanks in advance.
To resolve You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause, rewrite the query to use JOIN instead of IN (in mysql you need to write something like this):
UPDATE TaskAssessment a
INNER JOIN TaskAssessment a2 ON (a2.id = a.id)
SET a.activeFlag = 0
WHERE a2.active_flag = 1 AND
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed