SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Функция получения планируемого количества строк CREATE FUNCTION row_estimator(query TEXT) RETURNS BIGINT LANGUAGE plpgsql AS $$ DECLARE plan JSONB; BEGIN EXECUTE 'EXPLAIN (FORMAT JSON) ' || query INTO plan; RETURN (plan->0->'Plan'->>'Plan Rows')::BIGINT; END; $$; -- Тестовые данные CREATE TABLE person (id INT, name TEXT, age INT); INSERT INTO person (id, name, age) VALUES (generate_series(1, 10000), 'Person # ' || TRUNC(RANDOM()*10000), TRUNC(RANDOM() * 100)); ANALYZE person; -- Расчет по COUNT SELECT COUNT(*) FROM person WHERE age > 85; -- Расчет по функции SELECT row_estimator('SELECT * FROM person WHERE age > 85');

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear