Getting Innodb Lock Thread Id in MySQL 5.7? - mysql

I am trying to debug 'innodb lock waits' according to this manual. It suggests that I visualise debugging info using query
use performance_schema;
SELECT THREAD_ID,EVENT_ID,EVENT_NAME,CURRENT_SCHEMA,SQL_TEXT FROM events_statements_history_long
WHERE THREAD_ID IN (SELECT BLOCKING_THREAD_ID FROM data_lock_waits) ORDER BY EVENT_ID')
However data_lock_waits is present in MySQL > 5.7 while I have MySQL = 5.7. Tutorial suggests that I look into information_schema.INNODB_LOCK_WAITS. However, this table has the following columns: requesting_trx_id requested_lock_id blocking_trx_id blocking_lock_id.
How I can translate blocking_trx_id into Blocking Thread Id in MySQL 5.7?

Apparently it is this request:
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
FROM information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b
ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r
ON r.trx_id = w.requesting_trx_id;
https://dev.mysql.com/doc/refman/5.6/en/innodb-information-schema-examples.html

Related

Lost connection to MySQL server during query just on MacOS

I have an SQL query with 2 subqueries. whenever I run it on MySQL Workbench on macOS, it gives "Error Code: 2013. Lost connection to MySQL server during query". However, when it runs on Workbench on Windows, it runs normally without any errors.
I tried to increase the connection timeout, but still no success!
Any clue on how to solve this issue?
I appreciate your support and cooperation.
here is a query that gives an error:
with t1 as(
SELECT s.name rep_name, r.name region_name, sum(o.total_amt_usd) as total_amt
FROM sales_reps s
JOIN accounts a
ON a.sales_rep_id = s.id
JOIN orders o
ON o.account_id = a.id
JOIN region r
ON r.id = s.region_id
group by 1,2),
t2 as(
select region_name, max(total_amt) as total_amt
from t1
group by 1)
select t1.rep_name, t1.region_name, t1.total_amt
from t1
join t2
ON t1.region_name = t2.region_name AND t1.total_amt = t2.total_amt;
Your query is taking too long to return data so the connection gets dropped. There are 2 ways to fix this issue.
(i) Optimize query
(ii) Increase MySQL timeout
Explaining 2nd way:
1. In the application menu, select Edit > Preferences > SQL Editor.
2. Look for the MySQL Session section and increase the DBMS connection read time out value.
3. Save the settings, quite MySQL Workbench and reopen the connection.
Finally, I uninstalled the workbench and installed it again and now it is working properly. Thanks for who tried to answer my questions.

MySQL results different 5.6 to 5.7 (AWS RDS)

In the process of upgrading from MySQL 5.6 to 5.7 on RDS I'm finding differences in results returned for a number of queries.
For instance (table names obfuscated):
SELECT `model_s`.`id`,`model_s`.`pr`,`model_s`.`o_id`,`model_s`.`uploadee`
,`model_s`.`created_at`,`model_s`.`updated_at`,`model_s`.`o_ref`,`model_s`.`i_ref`
,`model_s`.`stss`,`model_s`.`u_fol`,`model_s`.`sdd`,`model_s`.`an_t_id`,`model_s`.`rean`
,`model_s`.`dpp`,`model_s`.`bg_t_cr`
FROM `model_s`
INNER JOIN `o_o` ON (`model_s`.`o_id` = `o_o`.`id`)
WHERE (
`model_s`.`pr` IN
(
SELECT u0.`id` AS col1
FROM `pr_pr` u0
LEFT OUTER JOIN `pr_stfr_pr_p` u1 ON (u0.`id` = u1.`pr`)
LEFT OUTER JOIN `acc_strf` u2 ON (u1.`stfr_id` = u2.`id`)
LEFT OUTER JOIN `acc_us_strf_as` u3 ON (u2.`id` = u3.`stfr_id`)
LEFT OUTER JOIN `pr_us_pr_p` u5 ON (u0.`id` = u5.`pr`)
WHERE (u3.`usr` = 7 OR u5.`usr` = 7)
)
AND NOT (`model_s`.`stss` = d)
AND `o_o`.`name` LIKE %my text%
)
ORDER BY `model_s`.`created_at` DESC
In this case, 5.6 returns 1 result and 5.7 returns 2 results (the latter being the expected behaviour).
Options for the DB are merely the default RDS 5.6 and 5.7 options (none changed at all).
Any idea why with default options we see such a difference between 5.6 and 5.7? (Please, no comments on complexity/readability/optimization of the query - its generated by the Django ORM and is a question for the future...)

Different results for query on mysql 5.6 and 5.7

I have a laravel application that was running an elqouent query with a whereHas method. The method was not returning any data on a virtual machine running mysql 5.7 but was working on 5.6. I got the raw query out of eloquent, here's what it is:
select * from `threads` where `id` = 5053 and exists (select * from `users` inner join `threads_users` on `users`.`id` = `threads_users`.`user_id` where `threads_users`.`thread_id` = `threads`.`id` and `user_id` = 296) limit 1
When I run this sql in mysql 5.6 it will return a single row but when I run it on mysql 5.7 it returns nothing. Is there some feature that was removed between these versions that would break this query?

Joining derived tables

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;

Convert MS Access "TOP" to MySQL "LIMIT" in subquery

I am trying to convert an MS Access query to MySQL and the problem is converting MS Access top to MySQL limit to get the same result. When I change query to limit I get the error that this version of MySQL does not support limit in subquery.
This is the MS Access query:
SELECT a.FK_CONTRIBUTOR_ID
FROM tPUBLISHERS
INNER JOIN (tCONTRIBUTORS AS b
INNER JOIN tCLIPS AS a ON b.CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID)
ON tPUBLISHERS.PUBLISHER_ID = b.FK_PUBLISHER_ID
WHERE ((a.CLIP_ID) In
(select top 5 CLIP_ID
from tCLIPS
where FK_CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID
AND SUSPEND = a.SUSPEND))
AND ((a.FK_CONTRIBUTOR_ID) In (1922,2034,2099))
Previously answered at:
MySQL Subquery LIMIT
basically change the subquery to a Join
Google for more with "mysql limit on subquery"