I need some assistance with a query I am using (MySQL) to update another table. When running the nested query, the query runs in less than a second. But as soon as I include the update part, it takes hours to run. The query I am using is as per the below:
UPDATE sys_reference.outlet_reference OUTREF LEFT JOIN
(SELECT
store_code 'storeCode'
, LEFT(header_value,20) 'CoOrds'
FROM
am_data_warehouse.am_headers
WHERE
action_date = CURDATE()- 1
AND header_field_id IN (3641, 4937)
) GPSCO
ON OUTREF.store_code = GPSCO.storeCode
SET OUTREF.gps_coordinates = GPSCO.CoOrds
Below is the structure of the table that is being updated:
I think the sub-query isn't doing you any favors here. I think you can rewrite it to elimiate the sub-query.
UPDATE
sys_reference.outlet_reference AS OUTREF
INNER JOIN am_data_warehouse.am_headers AS GPSCO ON OUTREF.store_code =
GPSCO.storeCode
SET
OUTREF.gps_coordinates = LEFT(GPSCO.header_value,20)
WHERE
GPSCO.action_date = CURDATE() - 1
AND GPSCO.header_field_id IN (3641, 4937)
Related
I am trying to update a table (~2 million rows) based on another table(10k rows). However, my update query is taking extremely long(30 mins) without any outputs as of yet. Is there a way to optimise this query?
UPDATE global_mobility_report
SET
global_mobility_report.locationID1 = (SELECT
geography.locationID
FROM
geography
WHERE
global_mobility_report.country_region = geography.country_region
AND global_mobility_report.sub_region_1 = geography.sub_region_1
AND global_mobility_report.sub_region_2 = geography.sub_region_2
AND global_mobility_report.metro_area = geography.metro_area
AND global_mobility_report.iso_3166_2_code = geography.iso_3166_2_code
AND global_mobility_report.census_fips_code = geography.census_fips_code);
UPDATE global_mobility_report
JOIN geography USING ( country_region,
sub_region_1,
sub_region_2,
metro_area,
iso_3166_2_code,
census_fips_code )
SET global_mobility_report.locationID1 = geography.locationID;
The presence of according index will improve.
The rows in global_mobility_report which have no according row in geography will not be updated (stay unchanged). If you need them to be set to NULL then use LEFT JOIN.
I simply indexed the country_region,
sub_region_1,
sub_region_2,
metro_area,
iso_3166_2_code,
census_fips_code columns and it worked like a charm!
I have a use case at which i need to update only one record based on different conditions, the problem is those conditions retrieve many record,
so the update statement update all of the records every time,
UPDATE t_wallet_log wallet_log
LEFT JOIN t_end_user enduser
ON enduser.f_id=wallet_log.f_end_user_id
set wallet_log.f_txn_status = 'successful'
WHERE enduser.f_ref_number='ugY-227'
AND wallet_log.f_amount=1000
AND wallet_log.f_txn_kind='cr'
I tried almost every thing i know but no luck
Please try below query.
UPDATE wallet_log set wallet_log.f_txn_status = 'successful' where id=(
select p1.id from ( select wallet_log.id from t_wallet_log wallet_log
LEFT JOIN t_end_user enduser
ON enduser.f_id=wallet_log.f_end_user_id
WHERE enduser.f_ref_number='ugY-227'
AND wallet_log.f_amount=1000
AND wallet_log.f_txn_kind='cr' order by id desc limit 1)p1);
This will help you.
I have table "Temp" and table "Today", with same column names ("url" and "date").
I want to update "date" column of "Temp" table when url match.
But my tables are quite big (30K elements) and phpmyadmin does not want to execute the following - right - query :
update Temp Tp
inner join Today Ty on
Tp.url = Ty.url
set Tp.date = Ty.date
I get a "Query execution was interrupted, error #1317"
Why ? I expect this is because I pay for a mutualized server (OVH) and I am not able to execute queries longer than 2-3 seconds.
Anyway, now I want to execute this query range by range. First 1000 rows, 1000-2000 etc.
I tried the following :
update Temp Tp
inner join
(
select Tp2.date
from Temp Tp2
inner join Today Ty2
on Tp2.url = Ty2.url
limit 1000
) Ty on Tp.url = Ty.url
set Tp.date = Ty.date
BUT I get the following error : #1054 - Unknown column 'Ty.url' in 'on clause'
I couldn't find out why ?
As far as I can see, there are two problems here. First, as already mentioned by #pmbAustin, you're missing a column in your subquery.
Secondly, I think your subquery should be selecting the date from Ty2, rather than Tp2:
update Temp Tp
inner join
(
select Ty2.date, Tp2.url
from Temp Tp2
inner join Today Ty2
on Tp2.url = Ty2.url
limit 1000
) Ty on Tp.url = Ty.url
set Tp.date = Ty.date
See SQLFiddle (although for practical reasons, this demo is limited to 2).
Although you haven't specifically asked this (and you're probably aware already), for completeness it should be mentioned that for subsequent queries, LIMIT should be used alongside OFFSET (or just use the shortcut LIMIT 1000, 1000, LIMIT 2000,1000, LIMIT <offset>, <limit>, etc.
I'm trying to write a MYSQL Query that updates a cell in table1 with information gathered from 2 other tables;
The gathering of data from the other 2 tables goes without much issues (it is slow, but that's because one of the 2 tables has 4601537 records in it.. (because all the rows for one report are split in a separate record, meaning that 1 report has more than 200 records)).
The Query that I use to Join the two tables together is:
# First Table, containing Report_ID's: RE
# Table that has to be updated: REGI
# Join Table: JT
SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100
This returns 100 records (I limit it because the database is in use during work hours and I don't want to steal all resources).
However, I want to use these results in a Query that updates the REGI table (which it uses to select the 100 records in the first place).
However, I get the error that I cannot select from the table itself while updateing it (logically). So I tried selecting the select statement above into a temp table and than Update it; however, then I get the issue that I get to much results (logically! I only need 1 result and get 100) however, I'm getting stuck in my own thougts.. I ultimately need to fill the ReportID into each record of REGI.
I know it should be possible, but I'm no expert in MySQL.. is there anybody that can point me into the right direction?
Ps. fixing the table containing 400k records is not an option, it's a program from an external developer and I can only read that database.
The errors I'm talking about are as follows:
Error Code: 1093. You can't specify target table 'TrialTable' for update in FROM clause
When I use:
UPDATE TrialTable SET TrialTable.BlanccoReport =
(SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100)
WHERE TrialTable.HardwareType="PC" AND TrialTable.BlanccoReport=0)
Then I tried:
UPDATE TrialTable SET TrialTable.BlanccoReport = (SELECT ReportID FROM (<<and the rest of the SQL>>> ) as x WHERE X.SerialNo = TrialTable.Serienummer)
but that gave me the following error:
Error Code: 1242. Subquery returns more than 1 row
Haveing the Query above with a LIMIT 1, gives everything the same result
Firstly, your query seems to be functionally identical to the following:
SELECT RE.report_id ReportID
, REGI.Serienummer SerialNo
FROM Blancco_Registration.TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92
LIMIT 100
So, why not use that?
EDIT:
I still don't get it. I can't see what part of the problem the following fails to solve...
UPDATE TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
SET TrialTable.BlanccoReport = RE.report_id
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92;
(This is not an answer, but maybe a pointer towards a few points that need further attention)
Your JT sub query looks suspicious to me:
(SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92
GROUP BY RE.report_id)
You use group by but don't actually use any aggregate functions. The column RE.Value_string should strictly be something like MAX(RE.Value_string) instead.
I am bit stuck with this one.. what i want is update app_name (first table). It returns no error.. but does nothing...
UPDATE tbl_m_app AS tma, tbl_measure AS tm
SET tma.app_name='Ap1'
WHERE (tm.mesure_id = tma.mesure_id
AND tm.live = 1)
This query will do the same work in more obvious way and without joins
UPDATE tbl_m_app AS tma
SET tma.app_name='Ap1'
WHERE tma.mesure_id IN (SELECT tm.mesure_id FROM tbl_measure AS tm WHERE tm.live = 1)
I think this SQL is fine, it's just not matching any rows.
Check this by using a select with the same where clause:
SELECT * FROM tbl_measure tm WHERE tm.live=1;
0 rows returned, right?