I have the following tables:
EachtupleofthePageViewstable(main)containsasingle-pageview.Ifasinglepersoninthesamesession(Sessions)viewsthesamepage(Pages),anewtupleisnotaddedinPageViews>.InsteadthePageViews.quantityfieldisincremented.
Inonereport,Ineedthenumberofuniquevisitors(COUNTDISTINCTVisitors.id)andpageviews(SUMPageViews.quantity)inagiventime,week,month,andyear)fromaspecificstore(whichstoresStores.code).
ThequeriesI'mcurrentlydoingare:
Visitors
SELECTDISTINCTVisitors.idFROMPageViewsINNERJOINVisitorsStoresONVisitorsStores.id=PageViews.visitorINNERJOINStoresONStores.id=VisitorsStores.storeINNERJOINSessionsONSessions.id=VisitorsStores.sessionINNERJOINVisitorsONVisitors.id=Sessions.visitorWHEREDATE(PageViews.createdAt)BETWEEN'2017-11-01'AND'2017-11-30'ANDStores.code='loja1';
PageViews
SELECTSUM(quantity)ASqtdeFROMPageViewsINNERJOINVisitorsStoresONVisitorsStores.id=PageViews.visitorINNERJOINStoresONStores.id=VisitorsStores.storeWHEREDATE(PageViews.createdAt)BETWEEN'2017-11-01'AND'2017-11-30'ANDStores.code='loja1';
Both
SET@loja:='loja1';SET@inicio:='2017-11-01';SET@fim:='2017-11-31';SELECTCOUNT(tVisitors.qtdVisitors)ASvisitors,SUM(tVisitors.qtdPageViews)aspageViewsFROM(SELECTVisitors.idASqtdVisitors,SUM(quantity)ASqtdPageViewsFROMPageViewsINNERJOINVisitorsStoresONVisitorsStores.id=PageViews.visitorINNERJOINStoresONStores.id=VisitorsStores.storeINNERJOINSessionsONSessions.id=VisitorsStores.sessionINNERJOINVisitorsONVisitors.id=Sessions.visitorWHEREDATE(PageViews.createdAt)BETWEEN@[email protected]=@lojaGROUPBYqtdVisitors)astVisitors;
Itworksperfectlywhenastorehasfewaccessesperday.Butwhenastorehasalotofhits(somearound300visitorsand5,000pageviews)thequerytakesalongtimetocomplete(32swasthelongestinthementionedsituation.Lotswithlessthan1,000pageviewsusuallytakelessthan3s.)
Thequestionis:HowelsecouldIdothisquerytobefaster?
EXPLAINofthequeryBothforasingleday(05/12)
(visitor:696|pageViews:5809|queryexecutiontime:32s)
[{"id": 1,
"select_type": "PRIMARY",
"table": "<derived2>",
"type": "ALL",
"possible_keys": null,
"key": null,
"key_len": null,
"ref": null,
"rows": 6192,
"Extra": ""
},
{
"id": 1,
"select_type": "PRIMARY",
"table": "<derived3>",
"type": "ALL",
"possible_keys": null,
"key": null,
"key_len": null,
"ref": null,
"rows": 6192,
"Extra": ""
},
{
"id": 3,
"select_type": "DERIVED",
"table": "Stores",
"type": "index",
"possible_keys": "PRIMARY",
"key": "code",
"key_len": "62",
"ref": null,
"rows": 1,
"Extra": "Using where; Using index"
},
{
"id": 3,
"select_type": "DERIVED",
"table": "VisitorsStores",
"type": "ref",
"possible_keys": "PRIMARY,FK_VisitorsStores_Stores",
"key": "FK_VisitorsStores_Stores",
"key_len": "5",
"ref": "tmw_views.Stores.id",
"rows": 6192,
"Extra": "Using index"
},
{
"id": 3,
"select_type": "DERIVED",
"table": "PageViews",
"type": "ref",
"possible_keys": "FK_PageViews_VisitorsStores",
"key": "FK_PageViews_VisitorsStores",
"key_len": "8",
"ref": "tmw_views.VisitorsStores.id",
"rows": 1,
"Extra": "Using where"
},
{
"id": 2,
"select_type": "DERIVED",
"table": "Stores",
"type": "index",
"possible_keys": "PRIMARY",
"key": "code",
"key_len": "62",
"ref": null,
"rows": 1,
"Extra": "Using where; Using index; Using temporary"
},
{
"id": 2,
"select_type": "DERIVED",
"table": "VisitorsStores",
"type": "ref",
"possible_keys": "PRIMARY,FK_VisitorsStores_Sessions,FK_VisitorsStores_Stores",
"key": "FK_VisitorsStores_Stores",
"key_len": "5",
"ref": "tmw_views.Stores.id",
"rows": 6192,
"Extra": "Using where"
},
{
"id": 2,
"select_type": "DERIVED",
"table": "Sessions",
"type": "eq_ref",
"possible_keys": "PRIMARY,FK_Sessions_Visitors",
"key": "PRIMARY",
"key_len": "8",
"ref": "tmw_views.VisitorsStores.session",
"rows": 1,
"Extra": ""
},
{
"id": 2,
"select_type": "DERIVED",
"table": "Visitors",
"type": "eq_ref",
"possible_keys": "PRIMARY",
"key": "PRIMARY",
"key_len": "8",
"ref": "tmw_views.Sessions.visitor",
"rows": 1,
"Extra": "Using index"
},
{
"id": 2,
"select_type": "DERIVED",
"table": "PageViews",
"type": "ref",
"possible_keys": "FK_PageViews_VisitorsStores",
"key": "FK_PageViews_VisitorsStores",
"key_len": "8",
"ref": "tmw_views.VisitorsStores.id",
"rows": 1,
"Extra": "Using where; Distinct"
}
]
SHOW TABLE STATUS FROM tmw_views
;
{
"table": "TABLES",
"rows":
[
{
"Name": "PageViews",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 1560181,
"Avg_row_length": 84,
"Data_length": 131694592,
"Max_data_length": 0,
"Index_length": 194314240,
"Data_free": 12582912,
"Auto_increment": null,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "PageViewsYear",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 25,
"Avg_row_length": 655,
"Data_length": 16384,
"Max_data_length": 0,
"Index_length": 32768,
"Data_free": 0,
"Auto_increment": 62,
"Create_time": "2017-11-27 11:17:44",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "Pages",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 94648,
"Avg_row_length": 116,
"Data_length": 11026432,
"Max_data_length": 0,
"Index_length": 0,
"Data_free": 4194304,
"Auto_increment": 205925,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "Sessions",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 366180,
"Avg_row_length": 73,
"Data_length": 26804224,
"Max_data_length": 0,
"Index_length": 41058304,
"Data_free": 3145728,
"Auto_increment": 531262,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "Stores",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 0,
"Avg_row_length": 0,
"Data_length": 16384,
"Max_data_length": 0,
"Index_length": 16384,
"Data_free": 0,
"Auto_increment": 31,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "SystemLogs",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 5913,
"Avg_row_length": 1687,
"Data_length": 9977856,
"Max_data_length": 0,
"Index_length": 540672,
"Data_free": 4194304,
"Auto_increment": null,
"Create_time": "2017-11-20 15:21:31",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "Visitors",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 31067,
"Avg_row_length": 51,
"Data_length": 1589248,
"Max_data_length": 0,
"Index_length": 1589248,
"Data_free": 4194304,
"Auto_increment": 102579,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
},
{
"Name": "VisitorsStores",
"Engine": "InnoDB",
"Version": 10,
"Row_format": "Compact",
"Rows": 297216,
"Avg_row_length": 44,
"Data_length": 13107200,
"Max_data_length": 0,
"Index_length": 17842176,
"Data_free": 4194304,
"Auto_increment": 562767,
"Create_time": "2017-10-27 23:31:14",
"Update_time": null,
"Check_time": null,
"Collation": "utf8_general_ci",
"Checksum": null,
"Create_options": "",
"Comment": ""
}
]
}
SHOW VARIABLES LIKE 'query_cache%';
query_cache_limit 1048576
query_cache_min_res_unit 4096
query_cache_size 33554432
query_cache_strip_comments OFF
query_cache_type ON
query_cache_wlock_invalidate OFF