New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimise ecommerce life time metric query #18036
Comments
what seems to have fixed this for me was SELECT /*+ JOIN_FIXED_ORDER() */ COALESCE(SUM(ROUND(revenue,2)), 0) ... or also using It changes the plan to {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "4155.07"
},
"nested_loop": [
{
"table": {
"table_name": "log_visit",
"access_type": "ref",
"possible_keys": [
"PRIMARY",
"index_idsite_config_datetime",
"index_idsite_datetime",
"index_idsite_idvisitor"
],
"key": "index_idsite_idvisitor",
"used_key_parts": [
"idsite",
"idvisitor"
],
"key_length": "12",
"ref": [
"const",
"const"
],
"rows_examined_per_scan": 2798,
"rows_produced_per_join": 2798,
"filtered": "100.00",
"using_index": true,
"cost_info": {
"read_cost": "7.82",
"eval_cost": "559.60",
"prefix_cost": "567.42",
"data_read_per_join": "84M"
},
"used_columns": [
"idvisit",
"idsite",
"idvisitor"
],
"attached_condition": "(`test`.`log_visit`.`idvisitor` = (@`id`))"
}
},
{
"table": {
"table_name": "log_conversion",
"access_type": "ref",
"possible_keys": [
"PRIMARY"
],
"key": "PRIMARY",
"used_key_parts": [
"idvisit",
"idgoal"
],
"key_length": "12",
"ref": [
"test.log_visit.idvisit",
"const"
],
"rows_examined_per_scan": 1,
"rows_produced_per_join": 3948,
"filtered": "100.00",
"cost_info": {
"read_cost": "2798.00",
"eval_cost": "789.65",
"prefix_cost": "4155.07",
"data_read_per_join": "144M"
},
"used_columns": [
"idvisit",
"idgoal",
"items",
"revenue"
]
}
}
]
}
} |
@tsteur Two ideas came to mind here. I can't verify if either will work without experimentation, but just something else to consider.
|
@samjf there is already such an index on the log_visit table, but because it matches so many rows (like 1000) it seems to prefer a different strategy and not use it. Didn't realise we execute it twice 👍 It should be using a prepared statement already AFAIK although the idgoal could be a parameter too. Generally though it's interesting we could combine those two queries into one as it shouldn't make it slower like
|
Something to be careful of when we test this:
https://dev.mysql.com/doc/refman/5.6/en/query-cache-in-select.html Interestingly "a space character must precede and follow the SQL_NO_CACHE keyword" |
@samjf that's good to know! could you mention this on https://developer.matomo.org/guides/profiling-code#profiling-a-specific-query where we talk about SQL_NO_CACHE? There is an edit button on the bottom right which allows you to edit the page using markdown. You can edit it directly in the Github UI. Then you can create a PR from this change by setting a branch name |
@tsteur Sure thing! That is a good idea. I'll put a notice there too. |
I'm meaning this query https://github.com/matomo-org/matomo/blob/4.5.0-b2/plugins/Ecommerce/VisitorDetails.php#L154-L169 which looks like this
At first glance the query looks good as it uses indexes etc.
This is usually also the case
However, things change when one visitor has hundreds or thousands of visits. Then it looks like this:
Here is more explain format=json information:
FYI Overall, in that specific case, there is actually no entry that matches
idsite = 2 and idgoal = 0
inlog_conversion
.refs #16904
Not sure if we can improve this query, or maybe not execute it in the first place. Like if we are requesting the data for only one site, we could eg check if
ecommerce
is enabled for that site. In this specific example where we had this problem it would have fixed it. Maybe we can also find a way though to further improve this.For more details, or if you want me to test queries please ping me.
To summarise the todo list
where idvisitor = ?
and join other tablesmatomo/plugins/Ecommerce/VisitorDetails.php
Lines 133 to 137 in 7ff0e20
The text was updated successfully, but these errors were encountered: