This task is about improving memory utilization and performance for
Information schema
Some work has recently been done in bb-10.2-ext to free memory early for
tables and views used be performance schema. The next step is to create
more efficient temporary tables that doesn't store information that we don't
need.
MariaDB [test]> select MEMORY_USED,MAX_MEMORY_USED from information_schema.processlist where db="test";
+-------------+-----------------+
| MEMORY_USED | MAX_MEMORY_USED |
+-------------+-----------------+
| 86120 | 245768 |
+-------------+-----------------+
1 row in set (0.00 sec)
MariaDB [test]> select table_name from information_schema.tables where table_schema="mysql";
....
MariaDB [test]> select MEMORY_USED,MAX_MEMORY_USED from information_schema.processlist where db="test";
+-------------+-----------------+
| MEMORY_USED | MAX_MEMORY_USED |
+-------------+-----------------+
| 86120 | 696880 |
+-------------+-----------------+
Here we used 600K memory for a simple query
MariaDB [test]> select count(*) from information_schema.tables where table_schema="mysql";
MariaDB [test]> select table_name from information_schema.tables;
...
MariaDB [test]> select MEMORY_USED,MAX_MEMORY_USED from information_schema.processlist where db="test";
+-------------+-----------------+
| MEMORY_USED | MAX_MEMORY_USED |
+-------------+-----------------+
| 86120 | 5293216 |
+-------------+-----------------+
Here we used 5M memory for a simple query over 341 tables.
The reason for the excessive memory used comes from that the temporary table
created has a very wide record:
While running:
select table_name from information_schema.tables;
in gdb:
(gdb) break handler::ha_write_tmp_row
(gdb) p table->s->reclength
$2 = 14829
Two possible ways to fix this:
1) Extend heap tables to store VARCHAR and BLOB efficiently
2) In sql_show, change all fields that are not used to be CHAR(1)
1) is a major tasks and we can't get that done in time for 10.3
2) will help even if we do 1) as we have less to store.
This task is to do 2)
This should not be that hard as information_schema already knows which
fields are accessed in the query. This is already used to decide if we
can solve the information_schema access without opening the table.
This should be done against the bb-10.2-ext tree, which has the new
MAX_MEMORY_USED column in information_schema.processlist.
Hi, TXSQL from Tencent has work on this issue and improved memory used during query on information_schema. The ideas just like bellow:
1) Fields used by select and where cond should be collected during query.
2) When run create_schema_table before instantiate_tmp_table, we reduce fields_info->field_length to 1 to reduce memory used in the SQL.
3) The method can only be used in non-nested query.
The attachment is based on bb-10.2-ext tree, would you please have a look on it and give some suggestions ?
musazhang
added a comment - Hi, TXSQL from Tencent has work on this issue and improved memory used during query on information_schema. The ideas just like bellow:
1) Fields used by select and where cond should be collected during query.
2) When run create_schema_table before instantiate_tmp_table, we reduce fields_info->field_length to 1 to reduce memory used in the SQL.
3) The method can only be used in non-nested query.
The attachment is based on bb-10.2-ext tree, would you please have a look on it and give some suggestions ?
By the way, we didn't deal with "order by" or "group by" and in this case, there may be wrong results when query use "order by", "group by". So, give me some suggestions and I will modify it according your suggestions.
musazhang
added a comment - By the way, we didn't deal with "order by" or "group by" and in this case, there may be wrong results when query use "order by", "group by". So, give me some suggestions and I will modify it according your suggestions.
musazhang
added a comment - I built a branch named bb-10.2-ext ( https://github.com/musazhang/bb-10.2-ext ), and commit the change based on it , you can find the change by visiting https://github.com/musazhang/bb-10.2-ext/commit/f1273a78900466e641995ecc01a86a6bda9176e3 , thank you.
The test suite had not been run (as there was a lot of test failing
because of generated warnings when the old code tried to write too long
strings into the shortened fields)
To solve the issue with the not handled queries, I decide to use a little
different approach:
Create a bitmap for all fields in the information_schema table
Use a field processor to mark which fields where used in the query
Use the bitmap to decide if a column should be replaced with a short
string column or not.
Ensure that we don't generate warnings when trying to write to shortend
columns.
The final patch is attached to this issue.
Note that even if I decided to use a different approach, having your code
as a base made my work much faster, so thanks a lot for doing this!
Michael Widenius
added a comment - diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index ae18e1cac04..99cf4b84ad6 100644
— a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -7759,6 +7759,88 @@ ST_SCHEMA_TABLE *get_schema_table(enum enum_schema_tables schema_table_idx)
return &schema_tables [schema_table_idx] ;
}
+bool evaluate_schema_field_recursive(Item* item, const char* field_name)
+{
+ switch(item->type())
+ {
+ case Item::FIELD_ITEM:
+
{
+ Item_field* field= (Item_field *)item;
+ if (!strcasecmp(field->field_name.str, field_name))
+ return true;
+ else if (!strcasecmp(field->field_name.str, "*"))
+ return true;
+ else
+ return false;
+ }
+
+ case Item::FUNC_ITEM:
+ {
+ bool show_field= false;
+ Item_func* func= (Item_func *)item;
+ for (uint i= 0; i < func->argument_count(); i++)
+
{
+ show_field= show_field ||
+ evaluate_schema_field_recursive(func->arguments()[i],
+ field_name);
+ if (show_field)
+ return true;
+ }
+ return false;
+ }
+
+ case Item::COND_ITEM:
+ {
+ Item *tmp;
+ bool show_field= false;
+ Item_cond* cond= (Item_cond *)item;
+ List_iterator<Item> it(*(cond->argument_list()));
+ while ((tmp= it++))
+
{
+ show_field= show_field ||
+ evaluate_schema_field_recursive(tmp, field_name);
+ if (show_field)
+ return true;
+ }
+ return false;
+ }
+
+ default:
+ return false;
+ }
+}
The above function doesn't check all possible item types, for example
SUM_FUNC_ITEM is required to be tested.
+
+bool field_can_be_used_in_query(THD* thd, ST_FIELD_INFO *field_info)
+{
+ if (thd->lex->select_lex.sj_nests.elements > 0 ||
+ thd->lex->select_lex.sj_subselects.elements > 0 ||
+ thd->lex->select_lex.nest_level > 0)
+ return true;
+
+ reg2 Item *item;
+ List_iterator<Item> it(thd->lex->select_lex.item_list);
+
+ /* select fields list check */
+ while ((item= it++))
+
{
+ if (evaluate_schema_field_recursive(item, field_info->field_name))
+ return true;
+ }
+
+ /* select fields where cond check */
+ if (thd->lex->select_lex.where &&
+ evaluate_schema_field_recursive(thd->lex->select_lex.where,
+ field_info->field_name))
+ return true;
+
+ /* select fields having cond check */
+ if (thd->lex->select_lex.having &&
+ evaluate_schema_field_recursive(thd->lex->select_lex.having,
+ field_info->field_name))
+ return true;
+
+ return false;
+}
The above was quite ok, but it missed a couple of things:
Doesn't handle sub queries
Doesn't handle multiple tables in a query
Doesn't handle ON conditions for multiple tables
Doesn't handle SET @innodb_rows_deleted_orig = (SELECT
The code goes trough all parts of select for every column,
which will take some resources if there is many columns used.
/**
Create information_schema table using schema_table data.
@@ -7783,6 +7865,7 @@ ST_SCHEMA_TABLE *get_schema_table(enum enum_schema_tables schema_table_idx)
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
{
+ bool show_field= false;
int field_count= 0;
Item *item;
TABLE *table;
@@ -7869,19 +7952,35 @@ TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list)
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
if (!(item= new (mem_root)
Item_blob(thd, fields_info->field_name,
fields_info->field_length)))
+
+ show_field= field_can_be_used_in_query(thd, fields_info);
+ if (show_field)
{
DBUG_RETURN(0);
+ if (!(item= new (mem_root)
+ Item_blob(thd, fields_info->field_name,
+ fields_info->field_length)))
+
{
+ DBUG_RETURN(0);
+ }
+ }
+ else
+ {
+ if (!(item= new (mem_root)
+ Item_empty_string(thd, "", 1, cs)))
+ {
+ DBUG_RETURN(0);
+ }
+ item->set_name(thd, fields_info->field_name,
+ field_name_length, cs);
}
break;
default:
/* Don't let unimplemented types pass through. Could be a grave error. */
DBUG_ASSERT(fields_info->field_type == MYSQL_TYPE_STRING);
+ show_field= field_can_be_used_in_query(thd, fields_info);
if (!(item= new (mem_root)
Item_empty_string(thd, "", fields_info->field_length, cs)))
+ Item_empty_string(thd, "", show_field ? fields_info->field_length : 1, cs)))
{
DBUG_RETURN(0);
}
The above code was ok.
What was missing in the code:
The test suite had not been run (as there was a lot of test failing
because of generated warnings when the old code tried to write too long
strings into the shortened fields)
To solve the issue with the not handled queries, I decide to use a little
different approach:
Create a bitmap for all fields in the information_schema table
Use a field processor to mark which fields where used in the query
Use the bitmap to decide if a column should be replaced with a short
string column or not.
Ensure that we don't generate warnings when trying to write to shortend
columns.
The final patch is attached to this issue.
Note that even if I decided to use a different approach, having your code
as a base made my work much faster, so thanks a lot for doing this!
People
Michael Widenius
Michael Widenius
Votes:
2Vote for this issue
Watchers:
7Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":967.1999998092651,"ttfb":296.90000009536743,"pageVisibility":"visible","entityId":64112,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"036e9426-c25b-4433-b83c-89a2a3af086e","navigationType":0,"readyForUser":1095.5999999046326,"redirectCount":0,"resourceLoadedEnd":696.1999998092651,"resourceLoadedStart":302.2999997138977,"resourceTiming":[{"duration":60.30000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":302.2999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":302.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":362.59999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":61.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":302.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":302.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":363.69999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":265.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":302.7999997138977,"connectEnd":302.7999997138977,"connectStart":302.7999997138977,"domainLookupEnd":302.7999997138977,"domainLookupStart":302.7999997138977,"fetchStart":302.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":367.5,"responseEnd":568.6999998092651,"responseStart":383.7999997138977,"secureConnectionStart":302.7999997138977},{"duration":393.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":303,"connectEnd":303,"connectStart":303,"domainLookupEnd":303,"domainLookupStart":303,"fetchStart":303,"redirectEnd":0,"redirectStart":0,"requestStart":378.7999997138977,"responseEnd":696.1999998092651,"responseStart":395.5,"secureConnectionStart":303},{"duration":95.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":303.09999990463257,"connectEnd":303.09999990463257,"connectStart":303.09999990463257,"domainLookupEnd":303.09999990463257,"domainLookupStart":303.09999990463257,"fetchStart":303.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":380.09999990463257,"responseEnd":398.19999980926514,"responseStart":396.7999997138977,"secureConnectionStart":303.09999990463257},{"duration":98.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":303.2999997138977,"connectEnd":303.2999997138977,"connectStart":303.2999997138977,"domainLookupEnd":303.2999997138977,"domainLookupStart":303.2999997138977,"fetchStart":303.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":380.7999997138977,"responseEnd":402.19999980926514,"responseStart":398.7999997138977,"secureConnectionStart":303.2999997138977},{"duration":101,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":303.5,"connectEnd":303.5,"connectStart":303.5,"domainLookupEnd":303.5,"domainLookupStart":303.5,"fetchStart":303.5,"redirectEnd":0,"redirectStart":0,"requestStart":382.40000009536743,"responseEnd":404.5,"responseStart":402.69999980926514,"secureConnectionStart":303.5},{"duration":77.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":303.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":303.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":381.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":113.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":303.90000009536743,"connectEnd":303.90000009536743,"connectStart":303.90000009536743,"domainLookupEnd":303.90000009536743,"domainLookupStart":303.90000009536743,"fetchStart":303.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":385.19999980926514,"responseEnd":417.5,"responseStart":416.09999990463257,"secureConnectionStart":303.90000009536743},{"duration":78.7999997138977,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":304,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":304,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":382.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":115.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":304.19999980926514,"connectEnd":304.19999980926514,"connectStart":304.19999980926514,"domainLookupEnd":304.19999980926514,"domainLookupStart":304.19999980926514,"fetchStart":304.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":387.2999997138977,"responseEnd":419.7999997138977,"responseStart":418.40000009536743,"secureConnectionStart":304.19999980926514},{"duration":377.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":310.19999980926514,"connectEnd":310.19999980926514,"connectStart":310.19999980926514,"domainLookupEnd":310.19999980926514,"domainLookupStart":310.19999980926514,"fetchStart":310.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":404.2999997138977,"responseEnd":687.7999997138977,"responseStart":684.5999999046326,"secureConnectionStart":310.19999980926514},{"duration":381.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":310.2999997138977,"connectEnd":310.2999997138977,"connectStart":310.2999997138977,"domainLookupEnd":310.2999997138977,"domainLookupStart":310.2999997138977,"fetchStart":310.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":412.59999990463257,"responseEnd":692.1999998092651,"responseStart":688,"secureConnectionStart":310.2999997138977},{"duration":103.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":720.4000000953674,"connectEnd":720.4000000953674,"connectStart":720.4000000953674,"domainLookupEnd":720.4000000953674,"domainLookupStart":720.4000000953674,"fetchStart":720.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":793,"responseEnd":823.9000000953674,"responseStart":823.2999997138977,"secureConnectionStart":720.4000000953674},{"duration":190.90000009536743,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":961.0999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":961.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1152,"responseStart":0,"secureConnectionStart":0},{"duration":174.7000002861023,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":967.1999998092651,"connectEnd":967.1999998092651,"connectStart":967.1999998092651,"domainLookupEnd":967.1999998092651,"domainLookupStart":967.1999998092651,"fetchStart":967.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1110.0999999046326,"responseEnd":1141.9000000953674,"responseStart":1141.0999999046326,"secureConnectionStart":967.1999998092651}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":105,"responseStart":297,"responseEnd":304,"domLoading":300,"domInteractive":1193,"domContentLoadedEventStart":1193,"domContentLoadedEventEnd":1253,"domComplete":1502,"loadEventStart":1502,"loadEventEnd":1502,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1154.5},{"name":"bigPipe.sidebar-id.end","time":1155.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":1155.5},{"name":"bigPipe.activity-panel-pipe-id.end","time":1157.6999998092651},{"name":"activityTabFullyLoaded","time":1270.5999999046326}],"measures":[],"correlationId":"fb5a132b85d5d8","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":104,"dbReadsTimeInMs":9,"dbConnsTimeInMs":17,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Hi, TXSQL from Tencent has work on this issue and improved memory used during query on information_schema. The ideas just like bellow:
1) Fields used by select and where cond should be collected during query.
2) When run create_schema_table before instantiate_tmp_table, we reduce fields_info->field_length to 1 to reduce memory used in the SQL.
3) The method can only be used in non-nested query.
The attachment is based on bb-10.2-ext tree, would you please have a look on it and give some suggestions ?