Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- select from multiple tables


Demaestro - 10:08 pm on Sep 10, 2010 (gmt 0)


I have always been under the impression that using UNION is faster and can be considered optimized if you use it over a bunch of OR statements.

This:
SELECT * FROM tableA, tableB WHERE tableA.field1 = tableB.field2
UNION
SELECT * FROM tableA, tableB WHERE tableA.field3 = tableB.field4

Is faster than this:
SELECT * FROM tableA, tableB WHERE tableA.field1 = tableB.field2 or tableA.field3 = tableB.field4

As LIA said, make sure you have properly indexed your fields. Also, if you don't need to return all columns then change * to just the fields you need, that will also help speed things up.


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4199964.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com