I have dozens of tables, that I have to join constantly using three string fields.
f1, f2 and f3 are always not null and each of them have a fixed number of characters.
I know that that's not very optimal but I can only query the database, I am not in charge of the design.
The condition I am using for the queries is:
concat(table1.f1, table1.f2, table1.f3) = concat (table2.f1, table2.f2, table2.f3)
These queries are against a database with millions of registries so queries always take a few minutes.
But bow, I am thinking that perhaps the join is quicker if I write the following?
table1.f1 = table2.f1
and table1.f2 = table2.f2
and table1.f3 = table2.f3
I am thinking that maybe that way the database can make use of indexes to join only the desired rows?
Sometimes I am very restrictive about the rows that must be joined but I think that in a join query all the rows are always matched and the rows are discarded once the tables have been joined.
If I know that table2.f6='whatever' and table2.f7='whatever' etc. does it make sense that the query joins ALL the rows and then discards most of them?
Because I think that the following three queries are the same, in terms of how optimal they are. Am I right?
SELECT ...
FROM table1
INNER JOIN table2
ON table1.f1 = table2.f1
and table1.f2 = table2.f2
and table1.f3 = table2.f3
and table2.f6 = 'whatever here'
SELECT ...
FROM table1
INNER JOIN table2
ON table1.f1 = table2.f1
and table1.f2 = table2.f2
and table1.f3 = table2.f3
WHEN table2.f6 = 'whatever here'
SELECT ...
FROM table1, table2
WHEN table1.f1 = table2.f1
and table1.f2 = table2.f2
and table1.f3 = table2.f3
and table2.f6 = 'whatever here'
So, should I use concat or not, and what is the best way to restrict the rows in a join query to make it faster?
Thanks!