When I run a query in Trino without a where clause, it takes a lot of time, with a where clause it takes a few seconds. Similarly, when I run a query without any where clause in Grafana, it seems that regardless of the time range, it loads all the data from the trino first, then in the panel, it shows the data of the desired time range, that’s why it takes a long time, but when I use the clause where DATE_KEY between 20230829 and 20230904
in the query panel, the loading time of the panel is greatly reduced. But the problem is that this expression is fixed and I want the user to decide what data to see in the panel by selecting the time in the time range.
当我在不带WHERE子句的Trino中运行查询时,会花费很多时间,而使用WHERE子句则需要几秒钟。同样,当我在Grafana中运行没有任何WHERE子句的查询时,似乎无论时间范围如何,它都会首先从Trino加载所有数据,然后在面板中显示所需时间范围的数据,这就是为什么需要很长时间的原因,但当我在查询面板中使用WHERE DATE_KEY BETWING 20230829和20230904时,面板的加载时间大大减少。但问题是,这个表达式是固定的,我希望用户通过选择时间范围内的时间来决定要在面板中查看哪些数据。
DATE_KEY column is a numeric or integer data type column, In order for the query to be dynamic I need to convert the __from
and __to
date ranges to be same format as DATE_KEY using one of the date functions.
DATE_KEY列是数字或整数数据类型列,为了使查询成为动态的,我需要使用DATE函数之一将__FROM和__TO日期范围转换为与DATE_KEY相同的格式。
declare @from = CAST(date_format(TIMESTAMP __from, ‘%Y-%m-%d’) as integer);
declare @to = CAST(date_format(TIMESTAMP __to, ‘%Y-%m-%d’) AS integer);
SELECT * FROM kimchi
WHERE DATE_KEY between @from and @to
GROUP BY DATE_KEY
but I am facing the following error:
但我面临着以下错误:
error querying the database: trino: query failed (200 OK): “io.trino.sql.parser.ParsingException: line 1:1: mismatched input ‘declare’. Expecting: ‘ALTER’, ‘ANALYZE’, ‘CALL’, ‘COMMENT’, ‘COMMIT’, ‘CREATE’, ‘DEALLOCATE’, ‘DELETE’, ‘DENY’, ‘DESC’, ‘DESCRIBE’, ‘DROP’, ‘EXECUTE’, ‘EXPLAIN’, ‘GRANT’, ‘INSERT’, ‘MERGE’, ‘PREPARE’, ‘REFRESH’, ‘RESET’, ‘REVOKE’, ‘ROLLBACK’, ‘SET’, ‘SHOW’, ‘START’, ‘TRUNCATE’, ‘UPDATE’, ‘USE’, ”
更多回答
WHERE DATE_KEY between ${__from:date:YYYYMMDD} and ${__to:date:YYYYMMDD}, That's right, thank you
其中DATE_KEY介于${__FROM:DATE:YYYYMMDD}和${__TO:DATE:YYYYMMDD}之间,正确,谢谢
优秀答案推荐
Based on the error you are getting, it looks like your data source doesn't expect declare
instruction (I'm not even sure Trino supports it at all, but I'm no expert in Trino).
根据您收到的错误,您的数据源似乎不需要DECLARE指令(我甚至不确定Trino是否支持它,但我不是Trino方面的专家)。
You can skip variable declaration and use inline casting with query like
您可以跳过变量声明,使用类似于查询的内联转换
SELECT *
FROM kimchi
WHERE DATE_KEY BETWEEN CAST(date_format(TIMESTAMP __from, ‘%Y%m%d’) as integer)
AND CAST(date_format(TIMESTAMP __to, ‘%Y%m%d’) AS integer)
GROUP BY DATE_KEY
Note, I'm assuming based on description of your question that DATE_KEY
contains dates in format %Y%m%d
, and not the one you used in your attempts.
注意,根据您问题的描述,我假设DATE_KEY包含%Y%m%d格式的日期,而不是您在尝试中使用的日期。
Alternatively, you can skip all the castings and use Grafana's variable formatting:
或者,您可以跳过所有类型转换,使用Grafana的变量格式:
SELECT *
FROM kimchi
WHERE DATE_KEY BETWEEN ${__from:date:YYYYMMDD} AND ${__to:date:YYYYMMDD}
GROUP BY DATE_KEY
更多回答
我是一名优秀的程序员,十分优秀!