From c68d72aa37008a144bb067011c8dd4ca7ff1878f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 18 Sep 2018 16:57:48 +0200 Subject: [PATCH] Add new variable mysql-query_cache_stores_empty_result #1693 The variable controls if resultset withour rows will be cached or not --- include/MySQL_Thread.h | 2 ++ lib/MySQL_Session.cpp | 34 ++++++++++++++++++---------------- lib/MySQL_Thread.cpp | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 821f03085..581530291 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -235,6 +235,7 @@ class MySQL_Thread struct { bool stats_time_backend_query; bool stats_time_query_processor; + bool query_cache_stores_empty_result; } variables; pthread_mutex_t thread_mutex; @@ -416,6 +417,7 @@ class MySQL_Threads_Handler int query_cache_size_MB; bool stats_time_backend_query; bool stats_time_query_processor; + bool query_cache_stores_empty_result; bool kill_backend_connection_when_disconnect; } variables; struct { diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 66aac88ac..5c5dd04dc 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -4446,22 +4446,24 @@ void MySQL_Session::MySQL_Result_to_MySQL_wire(MYSQL *mysql, MySQL_ResultSet *My if (transfer_started==false) { // we have all the resultset when MySQL_Result_to_MySQL_wire was called if (qpo && qpo->cache_ttl>0) { // the resultset should be cached if (mysql_errno(mysql)==0) { // no errors - client_myds->resultset->copy_add(client_myds->PSarrayOUT,0,client_myds->PSarrayOUT->len); - client_myds->resultset_length=MyRS->resultset_size; - unsigned char *aa=client_myds->resultset2buffer(false); - while (client_myds->resultset->len) client_myds->resultset->remove_index(client_myds->resultset->len-1,NULL); - GloQC->set( - client_myds->myconn->userinfo->hash , - (const unsigned char *)CurrentQuery.QueryPointer, - CurrentQuery.QueryLength, - aa , - client_myds->resultset_length , - thread->curtime/1000 , - thread->curtime/1000 , - thread->curtime/1000 + qpo->cache_ttl - ); - l_free(client_myds->resultset_length,aa); - client_myds->resultset_length=0; + if (thread->variables.query_cache_stores_empty_result || MyRS->num_rows) { + client_myds->resultset->copy_add(client_myds->PSarrayOUT,0,client_myds->PSarrayOUT->len); + client_myds->resultset_length=MyRS->resultset_size; + unsigned char *aa=client_myds->resultset2buffer(false); + while (client_myds->resultset->len) client_myds->resultset->remove_index(client_myds->resultset->len-1,NULL); + GloQC->set( + client_myds->myconn->userinfo->hash , + (const unsigned char *)CurrentQuery.QueryPointer, + CurrentQuery.QueryLength, + aa , + client_myds->resultset_length , + thread->curtime/1000 , + thread->curtime/1000 , + thread->curtime/1000 + qpo->cache_ttl + ); + l_free(client_myds->resultset_length,aa); + client_myds->resultset_length=0; + } } } } diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 67b2c287e..880fb3dd6 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -319,6 +319,7 @@ static char * mysql_thread_variables_names[]= { (char *)"connpoll_reset_queue_length", (char *)"stats_time_backend_query", (char *)"stats_time_query_processor", + (char *)"query_cache_stores_empty_result", NULL }; @@ -432,6 +433,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() { variables.connpoll_reset_queue_length = 50; variables.stats_time_backend_query=false; variables.stats_time_query_processor=false; + variables.query_cache_stores_empty_result=true; variables.kill_backend_connection_when_disconnect=true; variables.sessions_sort=true; #ifdef IDLE_THREADS @@ -731,6 +733,7 @@ int MySQL_Threads_Handler::get_variable_int(char *name) { if (!strcasecmp(name,"connpoll_reset_queue_length")) return (int)variables.connpoll_reset_queue_length; if (!strcasecmp(name,"stats_time_backend_query")) return (int)variables.stats_time_backend_query; if (!strcasecmp(name,"stats_time_query_processor")) return (int)variables.stats_time_query_processor; + if (!strcasecmp(name,"query_cache_stores_empty_result")) return (int)variables.query_cache_stores_empty_result; if (!strcasecmp(name,"kill_backend_connection_when_disconnect")) return (int)variables.kill_backend_connection_when_disconnect; if (!strcasecmp(name,"sessions_sort")) return (int)variables.sessions_sort; #ifdef IDLE_THREADS @@ -1143,6 +1146,9 @@ char * MySQL_Threads_Handler::get_variable(char *name) { // this is the public f if (!strcasecmp(name,"stats_time_query_processor")) { return strdup((variables.stats_time_query_processor ? "true" : "false")); } + if (!strcasecmp(name,"query_cache_stores_empty_result")) { + return strdup((variables.query_cache_stores_empty_result ? "true" : "false")); + } if (!strcasecmp(name,"kill_backend_connection_when_disconnect")) { return strdup((variables.kill_backend_connection_when_disconnect ? "true" : "false")); } @@ -2182,6 +2188,17 @@ bool MySQL_Threads_Handler::set_variable(char *name, char *value) { // this is t } return false; } + if (!strcasecmp(name,"query_cache_stores_empty_result")) { + if (strcasecmp(value,"true")==0 || strcasecmp(value,"1")==0) { + variables.query_cache_stores_empty_result=true; + return true; + } + if (strcasecmp(value,"false")==0 || strcasecmp(value,"0")==0) { + variables.query_cache_stores_empty_result=false; + return true; + } + return false; + } #ifdef IDLE_THREADS if (!strcasecmp(name,"session_idle_show_processlist")) { if (strcasecmp(value,"true")==0 || strcasecmp(value,"1")==0) { @@ -3584,6 +3601,7 @@ void MySQL_Thread::refresh_variables() { mysql_thread___query_digests_lowercase=(bool)GloMTH->get_variable_int((char *)"query_digests_lowercase"); variables.stats_time_backend_query=(bool)GloMTH->get_variable_int((char *)"stats_time_backend_query"); variables.stats_time_query_processor=(bool)GloMTH->get_variable_int((char *)"stats_time_query_processor"); + variables.query_cache_stores_empty_result=(bool)GloMTH->get_variable_int((char *)"query_cache_stores_empty_result"); mysql_thread___hostgroup_manager_verbose = GloMTH->get_variable_int((char *)"hostgroup_manager_verbose"); mysql_thread___kill_backend_connection_when_disconnect=(bool)GloMTH->get_variable_int((char *)"kill_backend_connection_when_disconnect"); mysql_thread___sessions_sort=(bool)GloMTH->get_variable_int((char *)"sessions_sort"); @@ -3662,6 +3680,7 @@ MySQL_Thread::MySQL_Thread() { variables.stats_time_backend_query=false; variables.stats_time_query_processor=false; + variables.query_cache_stores_empty_result=true; } void MySQL_Thread::register_session_connection_handler(MySQL_Session *_sess, bool _new) {