一尘不染

获取PostgreSQL查询的执行时间

sql

DECLARE @StartTime datetime,@EndTime datetime

SELECT @StartTime=GETDATE()

select distinct born_on.name
from   born_on,died_on
where (FLOOR(('2012-01-30'-born_on.DOB)/365.25) <= (
    select max(FLOOR((died_on.DOD - born_on.DOB)/365.25))
    from   died_on, born_on
    where (died_on.name=born_on.name))
    )
and   (born_on.name <> All(select name from died_on))

SELECT @EndTime=GETDATE()

SELECT DATEDIFF(ms,@StartTime,@EndTime) AS [Duration in millisecs]

我无法获取查询时间。相反,我得到以下错误:

sql:/home/an/Desktop/dbms/query.sql:9: ERROR:  syntax error at or near "@"
LINE 1: DECLARE @StartTime datetime,@EndTime datetime

阅读 415

收藏
2021-03-17

共1个答案

一尘不染

出于测试目的,您也可以使用 EXPLAIN ANALYZE

您可以像这样使用它来检查我的查询的适应版本实际上是否更快:

EXPLAIN ANALYZE
SELECT DISTINCT born_on.name
FROM   born_on b
WHERE  floor(('2012-01-30'::date - b.dob) / 365.25) <= (
    SELECT floor((max(d1.dod - b1.dob)/365.25))
    FROM   born_on b1
    JOIN   died_on d1 USING (name)
    )
AND NOT EXISTS (
    SELECT *
    FROM   died_on d2
    WHERE  d2.name = b.name
    );

除查询计划外,还显示总运行时间。执行几次以排除工件。
几个选项可提供更多详细信息。

2021-03-17