Number of rows locked in mysql query - mysql

Is there a way in mysql that I can find the number of rows that get locked when a certain query runs? Eg. for a query, what is the number of rows locked:-
UPDATE xyz SET ARCHIVE = 1 , LAST_MODIFIED = CURRENT_TIMESTAMP WHERE ID = '123' AND ARCHIVE = 0;
Assume in this case, there is a index on ID and Archive is part of primary key.

BEGIN;
# lock
UPDATE xyz SET ARCHIVE = 1 , LAST_MODIFIED = CURRENT_TIMESTAMP WHERE ID = '123' AND ARCHIVE = 0;
# returns locked rows (X)
SELECT trx_rows_locked FROM information_schema.innodb_trx;
# release
COMMIT;

Related

Delete first row and place copy of row on end of table

Hi guys I have a mySQL database with a table called queue the rows consist columns singer and song. I'm using the table to keep the order the singers have gone up there is an auto increment Id
What I am trying to find is the proper syntax to copy the first row from my table to the last row with a new auto increment ID value and then delete the first row. It didn't seem such a challenge until I tried to write it . Any ideas are welcome.
Since getting an answer I have tried this.. but keep getting error 1111
UPDATE `queue`
SET `id` = MAX(id) + 1;
WHERE `id` = MIN(id);
Have also tried this...how can this be so hard.
Set $max = (SELECT MAX(id) + 1 FROM queue);
Set $min = (SELECT MIN(id) FROM queue);
UPDATE `queue`
SET `id` = $max
WHERE `id` = $min;
So I have abandoned the max() min() thought unless someone has a usable answer and moved to my original thought and I am very close. but my problem is I will not know the lowest id to remove because it would only be 1 once so I need to fill it in with a MIN(id) value or variable somehow. Here is where I am at.
INSERT INTO queue (SELECT NULL,singer, song1 FROM queue WHERE id = 1);
DELETE FROM queue ORDER BY id ASC LIMIT 1;
Why not just UPDATE the row to change its id by using a SET and WHERE clause?
UPDATE `queue`
SET `id` = 21 -- (current_highest_value + 1)
WHERE `id` = 1
This way you don't have to worry about adding a new row and deleting the old one.
i think all things are fine just you need to update SQL_SAFE_UPDATES = 0 for delete statement, so you can try below way
SET SQL_SAFE_UPDATES = 0;
INSERT INTO queue (SELECT NULL,singer, song1 FROM queue WHERE id = 1);
DELETE FROM queue ORDER BY id ASC LIMIT 1;

update mysql table row with unique row id

I want to update a table row within mysql that doesn't have row id's.
The table name is "unit_status"
unit is_active enabled
17625012 Active 0
I have 2 million of these but want to update this single row to change enabled to "1"
update unit_status
set enabled = 1
where unit = 17625012
and is_active = 'Active'
and enabled = 0
limit 1
You can try:
UPDATE unit_status SET enabled = 1 WHERE unit = 17625012 AND is_active = 'Active' AND enabled = 0
But you should always have a primary key or a unique key.

MYSQL Multi-Table Update Is Extremely Slow

I am trying to run a multi-table update in MYSQL (Amazon RDS) and it is extremely slow.
What I am trying to do?
Remove all duplicate rows based on a 1 hour time frame.
Below I created a temp table to identify the duplicate rows in the table. This query runs in 2 seconds.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
CREATE TEMPORARY TABLE tmpIds (id int primary key);
INSERT into tmpIds
SELECT distinct
d.id
FROM api d INNER JOIN api orig
on d.domain_id = orig.domain_id and d.user_id = orig.user_id
WHERE
orig.created_at < d.created_at
AND d.created_at <= DATE_ADD(orig.created_at, Interval 1 hour)
AND d.type = 'api/check-end'
AND d.created_at >= '2016-08-01';
SET TRANSACTION ISOLATION LEVEL READ COMMITTED ;
The problem is the UPDATE query it takes way to long to run on the production server. It also locks the api table.
SET #TRIGGER_DISABLED = 1;
UPDATE
api
SET
deleted_at = now()
WHERE type = 'api/check-end' AND created_at >= '2016-08-01'
AND id IN (SELECT id FROM tmpIds);
SET #TRIGGER_DISABLED = 0;
I also tried this version:
SET #TRIGGER_DISABLED = 1;
UPDATE
api a,
tmpIds ti
SET
a.deleted_at = now()
WHERE
type = 'api/check-end' AND created_at >= '2016-08-01' AND a.domain_id < 10 AND a.id = ti.id;
SET #TRIGGER_DISABLED = 0;
STATS
Temp Table: 32,000 rows
api table: total - 250,000 rows, after where clause (type, created_at)
200,000 rows.
The api table has costly triggers, this is why I turned them
off.
Sample run for 1000 updates 6 minutes.
There is an index on the api table primary key
The problem was the following statement:
SET #TRIGGER_DISABLED = 1;
Was not disabling the triggers. I had to delete the UPDATE trigger on the api table and the UPDATE ran in 1.3 seconds.
Any help on the best way to disable triggers while running a query?

Select updated records in mysql

How can I select Ids of updated records in mysql;
Is there any way to do something like this
select Id from (update tblA set col1=false where col2 > 5 )
You can't, you have to select them before updating.
If your table uses InnoDB storage engine, use SELECT FOR UPDATE to lock selected rows and prevent other clients from inserting, updating or deleting selected rows.
START TRANSACTION;
SELECT id FROM tblA WHERE col2 > 5 FOR UPDATE;
UPDATE tblA SET col1 = false WHERE col2 > 5;
COMMIT;
Another alternative is to set some column to a value which is unique to your client (e.g. process id) and when select using that value.
But it requires an additional column.
UPDATE tblA SET col1 = false, process_id = 12345 WHERE col2 > 5 AND process_id IS NULL;
SELECT id FROM tblA WHERE process_id = 12345

How to update multiple rows with one query with MySQL?

I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?
UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)
If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;
Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)