我正在尝试优化MariaDB中的一些查询,并且我发现ANALYZE FORMAT = JSON功能非常有用。但是,在一些复杂的连接的情况下,我遇到了一些时间没有加起来或接近它的例子。
一个工作时的例子,在一个查询的情况下,query_block r_total_time_ms大约是239ms;查询本身涉及11个表,其中一个占用了近227毫秒,占查询花费的大部分时间。有一个明显的索引更改,并使该元素下降到小于1毫秒,总查询时间超过1毫秒。
但在另一种情况下,query_block r_total_time_ms略高于163ms;查询涉及9个表和/或子查询,任何组件的最高 r_total_time_ms刚刚超过15毫秒。所有这些组件的总计总计大约25毫秒。那剩下的时间花在哪里了?它为其他140-ms的ms做了什么?如果有办法告诉我,我不知道它是什么。
这是我正在努力解决的ANALYZE输出:
ANALYZE
{
"query_block": {
"select_id": 1,
"r_loops": 1,
"r_total_time_ms": 163.18,
"filesort": {
"sort_key": "tcontent.lastUpdate",
"r_loops": 1,
"r_total_time_ms": 0.0147,
"r_limit": 20,
"r_used_priority_queue": false,
"r_output_rows": 2,
"r_buffer_size": "252",
"temporary_table": {
"table": {
"table_name": "tcontent",
"access_type": "index_merge",
"possible_keys": [
"IX_TContent",
"IX_TContent_1",
"IX_TContent_2",
"IX_TContent_5",
"IX_tcontent_mobileExclude",
"IX_tcontent_displaystart",
"IX_tcontent_displaystop",
"IX_tcontent_approved",
"IX_tcontent_active",
"IX_tcontent_display",
"IX_tcontent_type",
"ntID_Type_subType_Display_DisplayStart_DisplayStop_mobileExclude",
"ix_tcontent_SiteID_Active_Filename_Type"
],
"key_length": "2,2,2,2,2,78,106",
"index_merge": {
"union": {
"range": {
"key": "IX_tcontent_display",
"used_key_parts": ["Display"]
},
"range": {
"key": "IX_tcontent_display",
"used_key_parts": ["Display"]
},
"intersect": {
"range": {
"key": "IX_tcontent_approved",
"used_key_parts": ["Approved"]
},
"range": {
"key": "IX_tcontent_active",
"used_key_parts": ["Active"]
},
"range": {
"key": "IX_tcontent_display",
"used_key_parts": ["Display"]
},
"range": {
"key": "IX_TContent_2",
"used_key_parts": ["SiteID"]
},
"range": {
"key": "IX_TContent_5",
"used_key_parts": ["ModuleID"]
}
}
}
},
"r_loops": 1,
"rows": 2583,
"r_rows": 3966,
"r_total_time_ms": 15.183,
"filtered": 96.129,
"r_filtered": 98.664,
"attached_condition": "tcontent.Active = 1 and tcontent.Approved = 1 and tcontent.searchExclude = 0 and tcontent.SiteID = 'CAE' and tcontent.ModuleID = '00000000000000000000000000000000000' and tcontent.ContentID <> '00000000000000000000000000000000001' and tcontent.`Type` <> 'Module' and (tcontent.Display = 1 or tcontent.Display = 2 and (tcontent.DisplayStart <= '2018-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null) or tcontent.DisplayStart <= '2019-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null))) and (tcontent.mobileExclude is null or tcontent.mobileExclude in (0,1))"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "105",
"used_key_parts": ["contentHistID"],
"ref": ["func"],
"r_loops": 3913,
"rows": 1,
"r_rows": 5.1e-4,
"r_total_time_ms": 6.2577,
"filtered": 100,
"r_filtered": 100,
"attached_condition": "tcontent.Display = 1 or tcontent.Display = 2 and (tcontent.DisplayStart <= '2018-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null) or tcontent.DisplayStart <= '2019-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null))",
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"table": {
"table_name": "tcontentcategories",
"access_type": "ALL",
"possible_keys": ["PRIMARY"],
"r_loops": 1,
"rows": 166,
"r_rows": 166,
"r_total_time_ms": 0.2246,
"filtered": 100,
"r_filtered": 0.6024,
"attached_condition": "tcontentcategories.path like '%D3386B7B-5056-8740-7CA949A8C39FACF3%'"
},
"table": {
"table_name": "tcontentcategoryassign",
"access_type": "ref",
"possible_keys": [
"PRIMARY",
"IX_tcontentcategoryassign_categoryID"
],
"key": "IX_tcontentcategoryassign_categoryID",
"key_length": "105",
"used_key_parts": ["categoryID"],
"ref": ["dbMuraCMS.tcontentcategories.categoryID"],
"r_loops": 1,
"rows": 53,
"r_rows": 33,
"r_total_time_ms": 0.0413,
"filtered": 100,
"r_filtered": 100,
"using_index": true
}
}
}
},
"table": {
"table_name": "tfiles",
"access_type": "eq_ref",
"possible_keys": ["PRIMARY"],
"key": "PRIMARY",
"key_length": "105",
"used_key_parts": ["fileID"],
"ref": ["dbMuraCMS.tcontent.FileID"],
"r_loops": 2,
"rows": 1,
"r_rows": 1,
"r_total_time_ms": 0.0109,
"filtered": 100,
"r_filtered": 100,
"attached_condition": "trigcond(trigcond(tcontent.FileID is not null))"
},
"table": {
"table_name": "tcontentstats",
"access_type": "eq_ref",
"possible_keys": ["PRIMARY"],
"key": "PRIMARY",
"key_length": "182",
"used_key_parts": ["contentID", "siteID"],
"ref": ["dbMuraCMS.tcontent.ContentID", "dbMuraCMS.tcontent.SiteID"],
"r_loops": 2,
"rows": 1,
"r_rows": 0,
"r_total_time_ms": 0.0127,
"filtered": 100,
"r_filtered": 100,
"attached_condition": "trigcond(trigcond(tcontent.ContentID is not null and tcontent.SiteID is not null))"
},
"table": {
"table_name": "tparent",
"access_type": "ref",
"possible_keys": [
"IX_TContent",
"IX_TContent_2",
"IX_tcontent_active",
"ntID_Type_subType_Display_DisplayStart_DisplayStop_mobileExclude",
"ix_tcontent_SiteID_Active_Filename_Type"
],
"key": "IX_TContent",
"key_length": "106",
"used_key_parts": ["ContentID"],
"ref": ["dbMuraCMS.tcontent.ParentID"],
"r_loops": 2,
"rows": 1,
"r_rows": 238,
"r_total_time_ms": 1.5375,
"filtered": 100,
"r_filtered": 0.4202,
"attached_condition": "trigcond(tcontent.Display = 1 or tcontent.Display = 2 and (tparent.`Type` <> 'Calendar' and tcontent.DisplayStart <= '2018-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null) or tparent.`Type` = 'Calendar' and tcontent.DisplayStart <= '2019-01-09 15:17:00' and (tcontent.DisplayStop >= '2018-01-09 15:17:00' or tcontent.DisplayStop is null))) and trigcond(tparent.SiteID = tcontent.SiteID and tparent.Active = 1 and trigcond(tcontent.ParentID is not null))"
},
"table": {
"table_name": "tcontentfilemetadata",
"access_type": "ALL",
"possible_keys": [
"IX_tcontentfilemetadata_contenthistid",
"IX_tcontentfilemetadata_fileid"
],
"r_loops": 2,
"rows": 1,
"r_rows": 0,
"r_total_time_ms": 0.0045,
"filtered": 100,
"r_filtered": 100,
"attached_condition": "trigcond(tcontentfilemetadata.fileid = tcontent.FileID and tcontentfilemetadata.contenthistid = tcontent.ContentHistID)"
}
}
}
}
}