From aed8369e43c1ee4b929109ff9af1381d7fad1840 Mon Sep 17 00:00:00 2001 From: V S Murthy Sidagam Date: Wed, 26 Nov 2014 16:59:58 +0530 Subject: [PATCH] Bug #16869534 QUERYING SUBSET OF COLUMNS DOESN'T USE TABLE CACHE; OPENED_TABLES I Description: When querying a subset of columns from the information_schema.TABLES Analysis: When information about tables is collected for statements like "SELECT ENGINE FROM I_S.TABLES" we do not perform full-blown table opens in SE, instead we only use information from table shares from the Table Definition Cache or .FRMs. Still in order to simplify I_S implementation mock TABLE objects are created from TABLE_SHARE during this process. This is done by calling open_table_from_share() function with special arguments. Since this function always increments "Opened_tables" counter, calls to it can be mistakingly interpreted as full-blown table opens in SE. Note that claim that "'SELECT ENGINE FROM I_S.TABLES' statement doesn't use Table Cache" is nevertheless factually correct. But it misses the point, since such statements a) don't use full-blown TABLE objects and therefore don't do table opens b) still use Table Definition Cache. Fix: We are now incrementing the counter when db_stat(i.e open flags for ha_open( we have considered an optimization which would use TABLE objects from Table Cache when available instead of constructing mock TABLE objects, but found it too intrusive for stable releases. --- mysql-test/r/create.result | 2 +- mysql-test/r/information_schema.result | 15 +++++++++++++++ mysql-test/t/information_schema.test | 17 +++++++++++++++++ sql/table.cc | 6 ++++-- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index d5f3945fb6b..29af76ed8df 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -291,7 +291,7 @@ Level Code Message Note 1050 Table 't1' already exists show status like "Opened_tables"; Variable_name Value -Opened_tables 2 +Opened_tables 1 select * from t1; a b 1 1 diff --git a/mysql-test/r/information_schema.result b/mysql-test/r/information_schema.result index 062fa0a92af..3a55dd5aeb4 100644 --- a/mysql-test/r/information_schema.result +++ b/mysql-test/r/information_schema.result @@ -1965,5 +1965,20 @@ event_object_table trigger_name # Clean-up. drop database mysqltest; # +# Test for bug #16869534 - "QUERYING SUBSET OF COLUMNS DOESN'T USE TABLE +# CACHE; OPENED_TABLES INCREASES" +# +SELECT * FROM INFORMATION_SCHEMA.TABLES; +SELECT VARIABLE_VALUE INTO @val1 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE +VARIABLE_NAME LIKE 'Opened_tables'; +SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES; +# The below SELECT query should give same output as above SELECT query. +SELECT VARIABLE_VALUE INTO @val2 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE +VARIABLE_NAME LIKE 'Opened_tables'; +# The below select should return '1' +SELECT @val1 = @val2; +@val1 = @val2 +1 +# # End of 5.5 tests # diff --git a/mysql-test/t/information_schema.test b/mysql-test/t/information_schema.test index e6dc989bdb9..42897735014 100644 --- a/mysql-test/t/information_schema.test +++ b/mysql-test/t/information_schema.test @@ -1782,6 +1782,23 @@ disconnect con12828477_3; --echo # Clean-up. drop database mysqltest; +--echo # +--echo # Test for bug #16869534 - "QUERYING SUBSET OF COLUMNS DOESN'T USE TABLE +--echo # CACHE; OPENED_TABLES INCREASES" +--echo # +--disable_result_log +SELECT * FROM INFORMATION_SCHEMA.TABLES; +--enable_result_log +SELECT VARIABLE_VALUE INTO @val1 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE + VARIABLE_NAME LIKE 'Opened_tables'; +--disable_result_log +SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES; +--enable_result_log +--echo # The below SELECT query should give same output as above SELECT query. +SELECT VARIABLE_VALUE INTO @val2 FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE + VARIABLE_NAME LIKE 'Opened_tables'; +--echo # The below select should return '1' +SELECT @val1 = @val2; --echo # --echo # End of 5.5 tests diff --git a/sql/table.cc b/sql/table.cc index b24e03fbe1a..c17ee39fe9e 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -1,5 +1,5 @@ /* - Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. + Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -2138,7 +2138,9 @@ partititon_err: outparam->no_replicate= outparam->file && test(outparam->file->ha_table_flags() & HA_HAS_OWN_BINLOGGING); - thd->status_var.opened_tables++; + /* Increment the opened_tables counter, only when open flags set. */ + if (db_stat) + thd->status_var.opened_tables++; DBUG_RETURN (0);