Найти в Дзене
69 подписчиков

--scenario1 SELECT BY PK QUERY PLAN

--scenario1
--SELECT BY PK
EXPLAIN (ANALYZE, BUFFERS , COSTS , SUMMARY )
SELECT *
FROM bookings.airports_data
WHERE airport_code = (
  SELECT airport_code
  FROM bookings.airports_data
  ORDER BY random()
  LIMIT 1
);

   QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Index Scan using idx_airports_data_code_timezone on airports_data (cost=143.04..145.26 rows=1 width=190) (actual time=2.557..2.559 rows=1 loops=1)
  Index Cond: (airport_code = (InitPlan 1).col1)
  Buffers: shared hit=23
  InitPlan 1
   -> Limit (cost=142.75..142.76 rows=1 width=12) (actual time=2.468..2.469 rows=1 loops=1)
   Buffers: shared hit=20
   -> Sort (cost=142.75..156.51 rows=5501 width=12) (actual time=2.467..2.467 rows=1 loops=1)
   Sort Key: (random())
   Sort Method: top-N heapsort Memory: 25kB
   Buffers: shared hit=20
   -> Index Only Scan using airports_data_pkey on airports_data airports_data_1 (cost=0.28..115.25 rows=5501 width=12) (actual time=0.127..1.319 rows=5501 loops=1)
   Heap Fetches: 0
   Buffers: shared hit=17
 Planning:
  Buffers: shared hit=199
 Planning Time: 1.783 ms
 Execution Time: 2.675 ms
(17 rows)
-- scenario1.sql
--SELECT BY PK
CREATE OR REPLACE FUNCTION scenario1() RETURNS integer AS $$
DECLARE
 test_rec record ;
BEGIN
SET application_name = 'scenario1';

SELECT *
INTO test_rec
FROM bookings.airports_data
WHERE airport_code = (
  SELECT airport_code
  FROM bookings.airports_data
  ORDER BY random()
  LIMIT 1
);

 return 0 ;
END
$$ LANGUAGE plpgsql;
1 минута