select UPPER(country) as c,AVG(a.score) as avg from scoretable a UNNEST a.countries country inner join scoretable b on keys lower(a.source)||'-'||lower(a.category) where a.type = 'article' AND STR_TO_MILLIS(a.published) >= STR_TO_MILLIS('2017-01-15 00:00:00Z') AND STR_TO_MILLIS(a.published) < STR_TO_MILLIS('2017-01-26') AND a.score IS NOT NULL AND b.industry_group = 'capital goods' group by UPPER(country)
Is there any tips to optimize this query? it is taking around 2 secs. I have creates indexes
create index published_country_idx on scoretable(type,STR_TO_MILLIS(published),score,countries,source,category) where type = 'article' and sentiment_score is not missing
and
create index src_cat_idx on rss(source,category) where type = 'article'
Here is my explain
[
{
“plan”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “IndexScan”,
“covers”: [
“cover ((a.type))”,
“cover (str_to_millis((a.published)))”,
“cover ((a.score))”,
“cover ((a.countries))”,
“cover ((a.source))”,
“cover ((a.category))”,
“cover ((meta(a).id))”
],
“filter_covers”: {
“cover (((a.score) is not missing))”: true,
“cover ((a.type))”: “article”
},
“index”: “published_country_idx”,
“index_id”: “ebdf52951b05aa47”,
“keyspace”: “scoretable”,
“namespace”: “default”,
“spans”: [
{
“Range”: {
“High”: [
“"article"”,
“1485388800000”
],
“Inclusion”: 0,
“Low”: [
“"article"”,
“1484438400000”,
“null”
]
}
}
],
“using”: “gsi”
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “Unnest”,
“as”: “country”,
“expr”: “cover ((a.countries))”
},
{
“#operator”: “Join”,
“as”: “b”,
“keyspace”: “scoretable”,
“namespace”: “default”,
“on_keys”: “((lower(cover ((a.source))) || "-") || lower(cover ((a.category))))”
},
{
“#operator”: “Filter”,
“condition”: “(((((cover ((a.type)) = "article") and (str_to_millis("2017-01-15 00:00:00Z") <= cover (str_to_millis((a.published))))) and (cover (str_to_millis((a.published))) < str_to_millis("2017-01-26"))) and (cover ((a.score)) is not null)) and (((b.industry_group) = "capital goods") or ((b.industry_group) = "transportation")))”
},
{
“#operator”: “InitialGroup”,
“aggregates”: [
“avg(cover ((a.score)))”
],
“group_keys”: [
“upper(country)”
]
}
]
}
},
{
“#operator”: “IntermediateGroup”,
“aggregates”: [
“avg(cover ((a.score)))”
],
“group_keys”: [
“upper(country)”
]
},
{
“#operator”: “FinalGroup”,
“aggregates”: [
“avg(cover ((a.score)))”
],
“group_keys”: [
“upper(country)”
]
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“as”: “c”,
“expr”: “upper(country)”
},
{
“as”: “avg”,
“expr”: “avg(cover ((a.score)))”
}
]
},
{
“#operator”: “FinalProject”
}
]
}
}
]
},
“text”: “select UPPER(country) as c,AVG(a.score) as avg from scoretable a\r\nUNNEST a.countries country\r\njoin scoretable b on keys lower(a.source)||‘-’||lower(a.category)\r\nwhere a.type = ‘article’ AND \r\nSTR_TO_MILLIS(a.published) >= STR_TO_MILLIS(‘2017-01-15 00:00:00Z’) AND \r\nSTR_TO_MILLIS(a.published) < STR_TO_MILLIS(‘2017-01-26’) AND\r\na.score IS NOT NULL AND\r\n(b.industry_group = ‘capital goods’ or b.industry_group = ‘transportation’)\r\ngroup by UPPER(country)”
}
]