mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-21 21:21:22 +08:00
This patch transforms constraints on hash-based space partitions to make them usable by postgres constraint exclusion. If we have an equality condition on a space partitioning column, we add a corresponding condition on get_partition_hash on this column. These conditions match the constraints on chunks, so postgres' constraint exclusion is able to use them and exclude the chunks. The following transformations are done: device_id = 1 becomes ((device_id = 1) AND (_timescaledb_internal.get_partition_hash(device_id) = 242423622)) s1 = ANY ('{s1_2,s1_2}'::text[]) becomes ((s1 = ANY ('{s1_2,s1_2}'::text[])) AND (_timescaledb_internal.get_partition_hash(s1) = ANY ('{1583420735,1583420735}'::integer[]))) These transformations are not visible in EXPLAIN output as we remove them again after hypertable expansion is done.
92 lines
3.6 KiB
MySQL
92 lines
3.6 KiB
MySQL
-- This file and its contents are licensed under the Timescale License.
|
|
-- Please see the included NOTICE for copyright information and
|
|
-- LICENSE-TIMESCALE for a copy of the license.
|
|
|
|
SET timescaledb.enable_chunk_append TO false;
|
|
SET timescaledb.enable_constraint_aware_append TO false;
|
|
SET timescaledb.current_timestamp_mock TO '1990-01-01';
|
|
|
|
\set PREFIX 'EXPLAIN (COSTS OFF, SUMMARY OFF, TIMING OFF)'
|
|
|
|
|
|
-- test valid variants we are optimizing
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id = 1;
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id IN (1);
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id IN (1,1);
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id IN (1,3);
|
|
|
|
-- test multiple constraints
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id = 1 AND device_id = 1;
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id = 1 AND device_id = 2;
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id = 1 OR device_id = 2;
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id IN (1) OR device_id IN (2);
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id IN (1) OR device_id = 2;
|
|
:PREFIX DELETE FROM metrics_space WHERE (time > '2000-01-01' OR device_id = 1) OR (time < '3000-01-01' OR device_id = 2);
|
|
|
|
-- variants we don't optimize
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id > 1;
|
|
:PREFIX DELETE FROM metrics_space WHERE device_id < 10;
|
|
|
|
-- CTE
|
|
:PREFIX WITH q1 AS (
|
|
DELETE FROM metrics_space WHERE device_id IN (1,3) RETURNING device_id
|
|
) SELECT FROM q1;
|
|
|
|
:PREFIX WITH q1 AS (
|
|
DELETE FROM metrics_space WHERE device_id = 2 RETURNING device_id
|
|
) DELETE FROM metrics_space WHERE device_id IN (1,3) RETURNING device_id;
|
|
|
|
-- JOIN
|
|
:PREFIX DELETE FROM metrics_space m1 using metrics_space m2 WHERE m1.device_id = 1 AND m2.device_id = 2;
|
|
:PREFIX UPDATE metrics_space m1 set v0 = 0.1 FROM metrics_space m2 WHERE m2.device_id=1 AND m1.device_id=2;
|
|
|
|
-- test multiple space dimensions and different datatypes
|
|
CREATE TABLE space_constraint(time timestamptz, s1 text, s2 numeric, s3 int, v float);
|
|
SELECT table_name FROM create_hypertable('space_constraint','time');
|
|
SELECT column_name FROM add_dimension('space_constraint','s1',number_partitions:=3);
|
|
SELECT column_name FROM add_dimension('space_constraint','s2',number_partitions:=3);
|
|
SELECT column_name FROM add_dimension('space_constraint','s3',number_partitions:=3);
|
|
|
|
INSERT INTO space_constraint
|
|
SELECT t,s1,s2,s3,0.12345 FROM
|
|
(VALUES ('2000-01-01'::timestamptz),('2001-01-01')) v1(t),
|
|
(VALUES ('s1_1'),('s1_2')) v2(s1),
|
|
(VALUES (1.23),(4.56)) v3(s2),
|
|
(VALUES (1),(2)) v4(s3);
|
|
|
|
\set PREFIX 'EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)'
|
|
|
|
BEGIN;
|
|
:PREFIX DELETE FROM space_constraint WHERE s1 = 's1_2';
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX DELETE FROM space_constraint WHERE s1 = 's1_2' AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX DELETE FROM space_constraint WHERE time > '2000-06-01' AND s1 = 's1_2' AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX DELETE FROM space_constraint WHERE s1 IN ('s1_2','s1_2') AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX DELETE FROM space_constraint WHERE s1 IN ('s1_1','s1_3') AND s2 IN (1.23,4.44) AND s3 IN (1,100);
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
:PREFIX UPDATE space_constraint SET v=0.7 WHERE s1 = 's1_2';
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX UPDATE space_constraint SET v=0.7 WHERE s1 = 's1_2' AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX UPDATE space_constraint SET v=0.7 WHERE time > '2000-06-01' AND s1 = 's1_2' AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX UPDATE space_constraint SET v=0.7 WHERE s1 IN ('s1_2','s1_2') AND s2 = 1.23 AND s3 = 2;
|
|
ROLLBACK;
|
|
BEGIN;
|
|
:PREFIX UPDATE space_constraint SET v=0.7 WHERE s1 IN ('s1_1','s1_3') AND s2 IN (1.23,4.44) AND s3 IN (1,100);
|
|
ROLLBACK;
|
|
|
|
DROP TABLE space_constraint;
|