checking if records exists in DB, in single step or 2 steps?

Posted by Sinan on Stack Overflow See other posts from Stack Overflow or by Sinan
Published on 2010-04-14T09:50:06Z Indexed on 2010/04/14 9:53 UTC
Read the original article Hit count: 303

Filed under:
|
|

Suppose you want to get a record from database which returns a large data and requires multiple joins.

So my question would be is it better to use a single query to check if data exists and get the result if it exists. Or do a more simple query to check if data exists then id record exists, query once again to get the result knowing that it exists.

Example:

3 tables a, b and ab(junction table)

select * from from a, b, ab where condition and condition and condition and condition etc...

or

select id from a, b ab where condition

then if exists do the query above.

So I don't know if there is any reason to do the second. Any ideas how this affects DB performance or does it matter at all?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about database