Why is one query consistently ~25ms faster than another in postgres?

Posted by Emory on Stack Overflow See other posts from Stack Overflow or by Emory
Published on 2010-05-06T17:46:53Z Indexed on 2010/05/06 17:58 UTC
Read the original article Hit count: 122

Filed under:

A friend wrote a query with the following condition:

    AND ( SELECT count(1) FROM users_alerts_status uas 
         WHERE uas.alert_id = context_alert.alert_id 
         AND   uas.user_id = 18309 
         AND   uas.status = 'read' ) = 0

Seeing this, I suggested we change it to:

    AND NOT EXISTS ( SELECT 1 FROM users_alerts_status uas 
           WHERE uas.alert_id = context_alert.alert_id 
           AND   uas.user_id = 18309 
           AND   uas.status = 'read' )

But in testing, the first version of the query is consistently between 20 and 30ms faster (we tested after restarting the server). Conceptually, what am I missing?

© Stack Overflow or respective owner

Related posts about postgresql