New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a subquery when filtering information_schema.tables
by table_name
.
#39712
Use a subquery when filtering information_schema.tables
by table_name
.
#39712
Conversation
dd84a27
to
255e9c3
Compare
/cc @eileencodes |
Sorry I was unclear on your PR at GitHub. This should point at the master branch and then I'll backport to 6.0. It looks like there's an issue with this, the mysql2 build is failing
|
255e9c3
to
c900114
Compare
I've changed this to point against
Yeah, there was a typo that I just fixed. |
I'm trying to think of a way to add a test for this but creating a user in a test to test this would be difficult. Do you know if the bug in mysql is documented? I think we should link to the bug if there is a report for it somewhere. |
I don't think it is. I tried googling a bit, but could not find anything. I'll see if I can get around to open a report against MySQL as well. |
I filed a bug report with MySQL at https://bugs.mysql.com/bug.php?id=99981 with an easily reproducible test case. I also tested this against MySQL 8, and it looks like this particular bug was fixed starting 8.0.0. |
…r-information-schema Use a subquery when filtering `information_schema.tables` by `table_name`.
Merged and backported to 6.0 in 0793a14 |
Depending on MySQL version and configuration, this is more than two orders of magnitude slower than selecting only the two columns we need for this query, which don't require reading any of the data files or recalculating innodb stats. https://dev.mysql.com/doc/refman/5.6/en/information-schema-optimization.html Performance regression was introduced in rails#39712, before then the query took around 40-50ms on my test environment, after it took 4-8s. With this change it's back to 40-50ms.
Summary
This is a workaround for a MySQL bug we've encountered when using a database user that only has been granted
SELECT
permissions on a subset of the tables that exist in a given database.When requesting data from the
information_schema.tables
without specifying a table name explicitly, MySQL only returns data for tables that can be accessed by the current user.But when requested data with specifying a condition for a specific table, MySQL will return data even if the specified table can not be accessed by the current user.
We found out that using a subquery would make MySQL behave as expected. 🙈
Here's an example:
This query will only return table names for tables where the current user has been granted
SELECT
.This query will return a single row containing
'some_table'
, ifsome_table
exists but the current user has not been grantedSELECT
on it.This query will return an empty result set if
some_table
exists but the current user has not been grantedSELECT
on it.Other Information
We're not sure which versions of MySQL are affected by this (we know it exists on MySQL 5.7.28), but the proposed change should be compatible with all versions of MySQL