Using SQL Server 2005, what is the most efficient way to join two tables in the following scenario?
The number of entries in each table can be quite large, approximately 200,000.
The only way I can do this now is to use cursors and some dynamic SQL for each element, which will obviously be very inefficient.
I have two tables - the PERSON table and the SEARCHITEMS table. The SEARCHITEMS table contains a column with some simple criteria that should be used when matching records with the PERSON table. Criteria can refer to any column in the PERSON table.
For example, the following tables:
PERSON table
PERSONID FIRSTNAME LASTNAME GENDER AGE ... VARIOUS OTHER COLUMNS 1 Fred Bloggs M 16 .... 200000 Steve Smith M 18
SEARCHITEMS table
ITEMID DESCRIPTION SEARCHCRITERIA 1 Males GENDER = 'M' 2 Aged 16 AGE=16 3 Some Statistic {OTHERCOLUMN >= SOMEVALUE AND OTHERCOLUMN < SOMEVALUE} .... 200000 Males Aged 16 GENDER = 'M' AND AGE = 16
RESULTS table should contain something like the following:
ITEMID DESCRIPTION PERSONID LASTNAME 1 Males 1 Bloggs 1 Males 200000 Smith 2 Aged 16 1 Bloggs .... 200000 Males Aged 16 1 Bloggs
It would be nice to be able to do something like
INSERT INTO RESULTSTABLE SELECT * FROM PERSON P LEFT JOIN SEARCHITEMS SI ON (APPLY SI.SEARCHCRITERIA TO P)
But I do not see a way to do this work. Any help or ideas appreciated.
source share