1 | INSERT INTO task(name,id,comment) VALUES ('task1','100','100th task'); |
SELECT * FROM test WHERE timestamp_col = 20000101;
In the preceding example, if timestamp_col is the timestamp type, the system first searches for the function that supports the "equal" operation of the timestamp and int types (constant numbers are considered as the int type). If no such function is found, the timestamp_col data and constant numbers are implicitly converted into the text type for calculation.
1 | SELECT id, (SELECT COUNT(*) FROM films f WHERE f.did = s.id) FROM staffs_p1 s; |
Scalar subqueries often result in query performance deterioration. During application development, scalar subqueries need to be converted into equivalent table associations based on the service logic.
1 | SELECT id, from_image_id, from_person_id, from_video_id FROM face_data WHERE current_timestamp(6) - time < '1 days'::interval; |
The modification is as follows:
1 | SELECT id, from_image_id, from_person_id, from_video_id FROM face_data where time > current_timestamp(6) - '1 days'::interval; |
1 2 | SELECT * FROM scdc.pub_menu WHERE (cdp= 300 AND inline=301) OR (cdp= 301 AND inline=302) OR (cdp= 302 AND inline=301); |
Convert the statement to the following:
1 2 3 4 5 6 7 8 | SELECT * FROM scdc.pub_menu WHERE (cdp= 300 AND inline=301) union all SELECT * FROM scdc.pub_menu WHERE (cdp= 301 AND inline=302) union all SELECT * FROM scdc.pub_menu WHERE (cdp= 302 AND inline=301); |
1 | SELECT * FROM T1 WHERE T1.C1 NOT IN (SELECT T2.C2 FROM T2); |
Rewrite the statement as follows:
1 | SELECT * FROM T1 WHERE NOT EXISTS (SELECT * FROM T1,T2 WHERE T1.C1=T2.C2); |