Sample Header Ad - 728x90

Fastest way to check if record is related to a particular user in postgres

0 votes
1 answer
809 views
While implementing a sharelikes table I added a procedure to add a new record for a like. I wanted to add a check before insert that the item being liked does not belong to the users who submitted the like request. Given function parameters Pshareid and Puserid I used the following conditional in the procedure SELECT INTO checkuser true FROM ks.shares WHERE id = Pshareid AND userid = Puserid limit 1; IF checkuser THEN -- Cannot like your own share ELSE --INSERT the new record END IF; In the select described above the record will almost always not exist. Is that more efficient than a SELECT on the share id to return the record (if it exists which is very likely at the point of select) and then to check the userid of the returned record? SELECT userid INTO checkuser FROM ks.shares WHERE id = Pshareid limit 1; IF checkuser = Puserid THEN -- Cannot like your own share ELSE --INSERT the new record END IF;
Asked by Russell Ormes (193 rep)
May 29, 2018, 08:39 AM
Last activity: Mar 13, 2025, 11:03 AM