Your code is correct, although in modern SQL we prefer to use explicit JOIN
clauses rather than cross products, so it would be better to write it as:
SELECT
do_something(X.column, Y.another_column, X.our_column),
do_something_else(Y.that_column, X.that_column, Y.my_column)
FROM this_scheme.your_table as X,
INNER JOIN that_scheme.our_table as Y
ON condition(X.column, Y.my_column) AND
another_condition(Y.another_column, X.our_column)
See INNER JOIN ON vs WHERE clause for discussion about the merits of INNER JOIN
over cross products.
If the conditions are actual function calls, this will most likely be very inefficient, since it will not be able to make use of indexes. The DBMS will have to generate the full cross product, then call the functions on all the columns to determine if it should be included in the result set, so it will be O(m * n)
, where m
and n
are the number of rows in the tables. But if it's something simple like X.colA = Y.colB
, indexes on these columns can be used to optimize the query.
Indexes can typically be used to optimize equality and inequality comparisons (e.g. col1 < col2
), and string prefixes (col1 LIKE 'foo%')
. You can also create specialized indexes for full-text searching and geographic coordinates.