Lock wait timeout exceeded MySQL - mysql

Well, I'm trying to delete duplicated rows in MySql.
To know what rows are duplicated I use this query:
select Path, COUNT(Path) from Entrega GROUP BY Path HAVING COUNT(Path) > 1;
And reading documentation, this query will delete duplicated items, and leave only one:
DELETE t1 FROM Entrega t1 INNER JOIN Entrega t2 WHERE t1.id > t2.id AND t1.Path = t2.Path
This simple query show me an alert that says
Error SQL(1205): Lock wait timeout exceeded; try restarting de transaction.
Why is it taking longer than expected?

Related

Why update clause takes a long time while select clause (with the same logic) doesn't?

Here is a select query which executes in 0.03 sec:
select * from engine.transactions
where transaction_row not in (select transaction_row from d.pos_transactions)
I want to update the matched (selected) rows from the query above this way:
update engine.transactions set retry = 0
where transaction_row not in (select transaction_row from d.pos_transactions)
which returns timeout error:
#1205 - Lock wait timeout exceeded; try restarting transaction
Why the performance drops this much on update clause?
Noted that there is only one index on engine.transactions table: transactions(psp_id, transaction_row).
Also, the table engine is InnoDB
I would recommend writing the query like this:
update engine.transactions t left join
d.pos_transactions pt
using (transaction_row)
set retry = 0
where pt.transaction_row is null;
For performance, you want an index on d.post_transactions(transaction_row). However, given the performance of the select, you might also have (or not need) the index.

after `Query execution was interrupted` can not access table

I was running a simple query in MySQL, joining two tables and getting where it doesn't match. Both tables have 500k data. my query was something like
select count(*) from t1 join t2 t1.id <> t2.id
and after 300 seconds I got following error
Error Code: 1317. Query execution was interrupted
after that, I could not run a simple query on that table, like
select * from t1 limit 50
but all other tables were working and my system also got down for a while. Finally, I restarted my MySQL server then everything started working.
Any idea why my table got stuck??
TIA
Your table was locked, if a query crash for some reason, you have to kill your query to unlock you table (or restart your mysql server)

MySql Update takes very long

I found a strange behavior on following query:
UPDATE llx_socpeople SET no_email=1 WHERE rowid IN (SELECT source_id FROM llx_mailing_cibles where tag = "68d74c3bc618ebed67919ed5646d0ffb");
takes 1 min and 30 seconds.
When I split up the commands to 2 queries:
SELECT source_id FROM llx_mailing_cibles where tag = "68d74c3bc618ebed67919ed5646d0ffb";
Result is 10842
UPDATE llx_socpeople SET no_email=1 WHERE rowid = 10842;
Result is shown in milliseconds.
Table llx_socpeople has about 7.000 records, llx_mailing_cibles has about 10.000 records.
MySQL Version is: 5.7.20-0ubuntu0.16.04.1
I already tried to optimize/repair both tables with no effect.
Any ideas?
Currently, as the subquery is being run for each row of the main query, we can expect a longer execution time.
What I would suggest would be to rely on a inner join for performing the update:
UPDATE llx_socpeople AS t1
INNER JOIN llx_mailing_cibles AS t2
ON t1.rowid = t2.source_id
SET t1.no_email=1
WHERE t2.tag = "68d74c3bc618ebed67919ed5646d0ffb";
This way you will definitely get far better performance.
You can troubleshoot your slow queries using the EXPLAIN MySql statement. Find more details about it on it's dedicated page from the official documentation. It might help you discover any missing indexes.

MySQL: Why my query can't finish?

I don't know why the query I've wroten, doesn't give me any output and just can't finish. Here http://sqlfiddle.com/#!9/8656d2/1 is the sample of my database, in real I have there about 260k records(rows). So you can see that query works with that table in the link, but in my whole database something is wrong. I waited almost 30 minutes for any results, but the query process is interminably. I don't know what should I do now, what can be the reason of described problem?
I don't actually know why the query isn't completing on your MySQL with 260K records. But I can speculate that the correlated subquery you have in the SELECT statement is the culprit. Let's look closely at that guy:
SELECT DATE_SUB(MAX(EVENT_TIME), INTERVAL 12 HOUR)
FROM my_table mt
WHERE
EVENT_TYPE = '2' AND
mt.ID = my_table.ID
You are basically telling MySQL to do a MAX() calculation across the entire table, for every record in your my_table table. Note that because the subquery is correlated, it might have to be run fresh for literally all 260K records. Hopefully you can see that 260K x 260K operations would be a bit slow.
If I be correct, then a possible fix would be to rephrase your query using a join to a subquery table which finds the max event times for each ID in your table. This query would be run once, and only once, and then it would be up to MySQL to find an efficient way to join back to your original table. But in any case, this approach should be eons faster than what you were using.
SELECT t1.*
FROM my_table t1
INNER JOIN
(
SELECT ID, MAX(EVENT_TIME) AS max_event_time
FROM my_table
WHERE EVENT_TYPE = '2'
GROUP BY ID
) t2
ON t1.ID = t2.ID AND
t1.EVENT_TIME BETWEEN
DATE_SUB(t2.max_event_time, INTERVAL 12 HOUR) AND
t2.max_event_time
WHERE t1.EVENT_TYPE != 3
ORDER BY t1.ID;
Here is link to your updated Fiddle:
Demo

Mysql update query over 10 million records

I've got to update a field in a table (250 000 records) with the value of a field from another table (10 000 000 records) based on the email...
I've tried:
UPDATE table1 t1, table2 t2
SET t1.country = t2.country
WHERE t1.email = t2.email
But I got a "Query is being executed" forever.
What query should I use?
Thanks
This would be a good opportunity to employ a JOIN.
UPDATE table1 as t1
JOIN table2 t2 ON t1.email = t2.email
SET t1.country = t2.country
It will still take a while for your query to process, but it should reduce the time by a significant amount.
I don't see an obvious error in your query (and the database would yield an error in that case). So the problem we're looking at is to speed up the execution of your update. Is one of your two email fields indexed? If not, could you add an index and try again? E.g. ALTER TABLE table2 ADD INDEX(email)