Possuo as seguintes tabelas:
Cada tupla da tabela PageViews (principal) contém uma visualização de página única. Se por um acaso uma mesma pessoa em uma mesma sessão (Sessions) visualiza uma mesma página (Pages), não é acrescentada uma nova tupla em PageViews. Ao invés disso é incrementado o campo PageViews.quantity.
Em um dos relatórios, preciso da quantidade de visitantes únicos (COUNT DISTINCT Visitors.id) e pageviews (SUM PageViews.quantity) em um determinado período (no dia, na semana, no mes e no ano) de uma loja específica (da qual é informada o Stores.code).
As consultas que estou fazendo no momento são:
Visitantes
SELECT DISTINCT Visitors.id
FROM PageViews
INNER JOIN VisitorsStores ON VisitorsStores.id = PageViews.visitor
INNER JOIN Stores ON Stores.id = VisitorsStores.store
INNER JOIN Sessions ON Sessions.id = VisitorsStores.session
INNER JOIN Visitors ON Visitors.id = Sessions.visitor
WHERE
DATE(PageViews.createdAt) BETWEEN '2017-11-01' AND '2017-11-30' AND
Stores.code = 'loja1';
PageViews
SELECT SUM(quantity) AS qtde
FROM PageViews
INNER JOIN VisitorsStores ON VisitorsStores.id = PageViews.visitor
INNER JOIN Stores ON Stores.id = VisitorsStores.store
WHERE
DATE(PageViews.createdAt) BETWEEN '2017-11-01' AND '2017-11-30' AND
Stores.code = 'loja1';
Ambas
SET @loja := 'loja1';
SET @inicio := '2017-11-01';
SET @fim := '2017-11-31';
SELECT
COUNT(tVisitors.qtdVisitors) AS visitors,
SUM(tVisitors.qtdPageViews) as pageViews
FROM (
SELECT
Visitors.id AS qtdVisitors,
SUM(quantity) AS qtdPageViews
FROM PageViews
INNER JOIN VisitorsStores ON VisitorsStores.id = PageViews.visitor
INNER JOIN Stores ON Stores.id = VisitorsStores.store
INNER JOIN Sessions ON Sessions.id = VisitorsStores.session
INNER JOIN Visitors ON Visitors.id = Sessions.visitor
WHERE
DATE(PageViews.createdAt) BETWEEN @inicio AND @fim AND Stores.code = @loja
GROUP BY qtdVisitors
) as tVisitors;
Funciona direitinho quando uma loja tem poucos acessos diários. Mas, quando uma loja possui muitos acessos (algumas em torno de 300 visitantes e 5.000 pageviews) a consulta demora muito para concluir (32s foi o maior tempo, na situação mencionada. Lojas com menos de 1.000 pageviews costumam demorar menos de 3s).
A dúvida é: De que outro jeito eu poderia fazer essa consulta de forma a ser mais rápida?
EXPLAIN da query Ambas para um único dia (05/12)
(visitor: 696 | pageViews: 5809 | tempo de execução da query: 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