Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7368147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:37:42+00:00 2026-05-29T03:37:42+00:00

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) <=

  • 0
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]

I am unable to get the query time. Instead I get the following error:

sql:/home/an/Desktop/dbms/query.sql:9: ERROR:  syntax error at or near "@"
LINE 1: DECLARE @StartTime datetime,@EndTime datetime
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:37:43+00:00Added an answer on May 29, 2026 at 3:37 am

    There are various ways to measure execution time, each has pros and cons. But whatever you do, some degree of the observer effect applies. I.e., measuring itself may distort the result.

    1. EXPLAIN ANALYZE

    You can prepend EXPLAIN ANALYZE, which reports the whole query plan with estimated costs actually measured times. The query is actually executed (with all side -effect, if any!). Works for all DDL commands and some others. See:

    • EXPLAIN ANALYZE not working with ALTER TABLE

    To check whether my adapted version of your query is, in fact, faster:

    EXPLAIN ANALYZE
    SELECT DISTINCT born_on.name
    FROM   born_on b
    WHERE  date '2012-01-30' - b.dob <= (
        SELECT max(d1.dod - b1.dob)
        FROM   born_on b1
        JOIN   died_on d1 USING (name)  -- name must be unique!
        )
    AND NOT EXISTS (
        SELECT FROM died_on d2
        WHERE  d2.name = b.name
        );
    

    Execute a couple of times to get more comparable times with warm cache. Several options are available to adjust the level of detail.

    While mainly interested in total execution time, make it:

    EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF)
    

    Mostly, TIMING matters – the manual:

    TIMING

    Include actual startup time and time spent in each node in the output.
    The overhead of repeatedly reading the system clock can slow down the
    query significantly on some systems, so it may be useful to set this
    parameter to FALSE when only actual row counts, and not exact times,
    are needed. Run time of the entire statement is always measured, even
    when node-level timing is turned off with this option. […]

    EXPLAIN ANALYZE measures on the server, using server time from the server OS, excluding network latency. But EXPLAIN adds some overhead to also output the query plan.

    2. psql with \timing

    Or use \timing in psql. Like Peter demonstrates.

    The manual:

    \timing [ on | off ]

    With a parameter, turns displaying of how long each SQL statement
    takes on or off. Without a parameter, toggles the display between on
    and off. The display is in milliseconds; intervals longer than 1
    second are also shown in minutes:seconds format, with hours and days
    fields added if needed.

    Important difference: psql measures on the client using local time from the local OS, so the time includes network latency. This can be a negligible difference or huge depending on connection and volume of returned data.

    3. Enable log_duration

    This has probably the least overhead per measurement and produces the least distorted timings. But it’s a little heavy-handed as you have to be superuser, have to adjust the server configuration, cannot just target the execution of a single query, and you have to read the server logs (unless you redirect to stdout).

    The manual:

    log_duration (boolean)

    Causes the duration of every completed statement to be logged. The
    default is off. Only superusers can change this setting.

    For clients using extended query protocol, durations of the Parse,
    Bind, and Execute steps are logged independently.

    There are related settings like log_min_duration_statement.

    4. Precise manual measurement with clock_timestamp()

    The manual:

    clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command.

    filiprem provided a great way to get execution times for ad-hoc queries as exact as possible. On modern hardware, timing overhead should be insignificant but depending on the host OS it can vary wildly. Find out with the server application pg_test_timing.
    Else you can mostly filter the overhead like this:

    DO
    $do$
    DECLARE
       _timing1  timestamptz;
       _start_ts timestamptz;
       _end_ts   timestamptz;
       _overhead numeric;     -- in ms
       _timing   numeric;     -- in ms
    BEGIN
       _timing1  := clock_timestamp();
       _start_ts := clock_timestamp();
       _end_ts   := clock_timestamp();
       -- take minimum duration as conservative estimate
       _overhead := 1000 * extract(epoch FROM LEAST(_start_ts - _timing1
                                                  , _end_ts   - _start_ts));
    
       _start_ts := clock_timestamp();
       PERFORM 1;  -- your query here, replacing the outer SELECT with PERFORM
       _end_ts   := clock_timestamp();
       
    -- RAISE NOTICE 'Timing overhead in ms = %', _overhead;
       RAISE NOTICE 'Execution time in ms = %' , 1000 * (extract(epoch FROM _end_ts - _start_ts)) - _overhead;
    END
    $do$;
    

    Take the time repeatedly (doing the bare minimum with 3 timestamps here) and pick the minimum interval as conservative estimate for timing overhead. Also, executing the function clock_timestamp() a couple of times should warm it up (in case that matters for your OS).

    After measuring the execution time of the payload query, subtract that estimated overhead to get closer to the actual time.

    Of course, it’s more meaningful for cheap queries to loop 100000 times or execute it on a table with 100000 rows if you can, to make distracting noise insignificant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DECLARE @TEMP_TBL TABLE (COL1 INT, COL2 VARCHAR(100) SELECT ID, NAME INTO @TEMP_TBL FROM TB1
DECLARE @Symb varchar SET @Symb = 'Lara' SELECT Names FROM Cricket where Names like
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
DECLARE @lastname VARCHAR(25) DECLARE @birthdate VARCHAR(25) SELECT @lastname = 'Smith' SELECT @birthdate = '19-Apr-36'
This is my code. declare @dt datetime; declare @AddTime int; declare @earliestTime int; declare
DECLARE l_string NVARCHAR2(600) := '123456'; checksum NVARCHAR2(600); BEGIN DBMS_OUTPUT.DISABLE; DBMS_OUTPUT.ENABLE(1000000); DBMS_OBFUSCATION_TOOLKIT.md5 (input_string => l_string,
Declare: LPWSTR** lines= new LPWSTR*[totalLines]; then i set using: lines[totalLines]=&totalText; SetWindowText(totalChat,(LPWSTR)lines[totalLines]); totalLines++; Now I
declare @d varchar set @d = 'No filter' if (@d like 'No filter') BEGIN
declare c int set c = 1 while c<700 do update users set profile_display_name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.