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.
If you mean in psql, rather than some program you are writing, use \?
for the help, and see:
\timing [on|off] toggle timing of commands (currently off)
And then you get output like:
# \timing on
Timing is on.
# select 1234;
?column?
----------
1234
(1 row)
Time: 0.203 ms
PostgreSQL is not Transact-SQL. These are two slightly different things.
In PostgreSQL, this would be something along the lines of
DO $proc$
DECLARE
StartTime timestamptz;
EndTime timestamptz;
Delta double precision;
BEGIN
StartTime := clock_timestamp();
PERFORM foo FROM bar; /* Put your query here, replacing SELECT with PERFORM */
EndTime := clock_timestamp();
Delta := 1000 * ( extract(epoch from EndTime) - extract(epoch from StartTime) );
RAISE NOTICE 'Duration in millisecs=%', Delta;
END;
$proc$;
On the other hand, measuring query time does not have to be this complicated. There are better ways:
In postgres command line client there is a \timing
feature which measures query time on client side (similar to duration in bottomright corner of SQL Server Management Studio).
It's possible to record query duration in server log (for every query, or only when it lasted longer than X milliseconds).
It's possible to collect server-side timing for any single statement using the EXPLAIN command:
EXPLAIN (ANALYZE, BUFFERS) YOUR QUERY HERE;
For testing purposes you can also use EXPLAIN ANALYZE.
You can use it like this 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 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
);
Shows the total runtime in addition to the query plan. Execute a couple of times to exclude artifacts.
A couple of options are available for more details.