Could someone help me with optimizing my SQL Query. The database is postgres. My table structure looks like:
create table test_table(test_id integer NOT NULL, sequence_id integer NOT NULL,value1 integer NOT NULL, value2 integer NOT NULL, CONSTRAINT test_table_pk PRIMARY KEY (test_id , sequence_id ))
create table test_event(event_id integer NOT NULL,test_id integer NOT NULL, sequence_id integer NOT NULL , CONSTRAINT test_event_pk PRIMARY KEY(event_id , test_id, sequence_id))
test_table
1,1, 200,300
2,2, 400,500
2,3, 600,700
2,4, 300,500
2,5, 200,900
test_event
1, 1,1
1, 2,2
1, 2,3
2, 2,4
2, 2,5
And I want to get all the value1 and value2 from test_table where sequence_id and test_id corresponds to event_id =1 in test_event. My query looks like
SELECT
value1, value2
FROM
test_table
WHERE
sequence_id IN (
SELECT sequence_id
FROM test_event
WHERE event_id=1) AND
test_id IN (
SELECT test_id
FROM test_event
WHERE event_id=1)
Can someone please let me know if this is the optimal way of writing this query?
EXPLAIN ANALYZE SELECT...Use real data if you want to know what the query optimizer really thinks. Use fake data if you want to know what the query optimizer fake thinks.