Different results for query on mysql 5.6 and 5.7 - mysql

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?

Related

Getting Innodb Lock Thread Id in MySQL 5.7?

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

MySql Query acts differently on my local version (8.0.21) and my staging (5.7.12)

I have an issue running a query on MySql using a Goapplication.
My local version works fine using mysql 8.0.21 but the same query on my staging version 5.7.12 on aurora fails
SELECT COUNT(*) AS cnt
FROM (
SELECT ? AS item_id, ? AS ar
)
AS A
JOIN item ON A.item_id = item.id
AND A.ar + item.existing_qty > item.qty;
Running this code in data grip with replacements works fine on both local and staging
Running this code but replacing the question marks with ints works fine
The error I get is:
Error 1054: Unknown column 'A.ar' in 'field list
I am thinking there is some driver / version issue
Does it work if you phrase the query like this?
select count(*)
from item
where item = ? and existing_qty + ? < qty
This is the same logic as your original query, and the parameters are passed in the same order.

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...)

MySQL Query using SubQueries Added Together to Sequelize Format

I have the following MySQL query which uses subqueries and sums them together.
SELECT *,(SELECT count(`id`) FROM `Texts` WHERE `UserId` =
`Users`.`id`) + (SELECT count(`id`) FROM `Calls` WHERE `UserId` =
`Users`.`id`) as `totalTextsAndCalls` FROM `Users` WHERE `id` IN
(1,2) ORDER BY `totalTextsAndCalls` DESC
This query works when I test manually right now and gives me the results I want.
I'm not sure how you handle subqueries in Sequelize however. I've looked into the Sequelize literals but not sure if this is the only way to do this? I can't seem to get the literal to work either. I will post my attempts at Sequelize writing this shortly if they are needed.

How do you run a MySQL query from a rake task?

I'm working on removing duplicates from a legacy database for a client of mine, and I've found a MySQL query to do just that. I want to create a Rake task to run the query for the production server. How would I do that?
MySQL query:
select * from community_event_users;
create table dups as
select distinct username, count(*)
from community_event_users group by username
having count(*) > 1;
delete community_event_users from community_event_users inner join dups
on community_event_users.username = dups.username;
insert into community_event_users select username from dups;
If you are on Rails and using ActiveRecord you can simply use:
ActiveRecord::Base.execute(my_sql)
ActiveRecord::Base.connection.execute(my_sql)
Where my_sql is your SQL string.
For Rails 5 and above you better use:
ApplicationRecord.connection.execute <<~END_OF_SQL
YOUR QUERY HERE
END_OF_SQL