We are working with firebird 2.5. I have the following structure.
TABLE ARTICLES(ARTNR INTEGER NOT NULL PRIMARY KEY
, S1 INTEGER
, S2 INTEGER ..... S10 INTEGER)
TABLE S1..S10(ID INTEGER NOT NULL PRIMARY KEY
, DESCRIERE VARCHAR(40))
There are foreign keys from CATALOG
to Sx
for all 10 columns from catalog. ARTICLES.S1
has a foreign key TO S1.ID
.
TABLE RAPORT_CLASIFICARE (
ID INTEGER NOT NULL PRIMARY KEY,
IDOPERATOR INTEGER NOT NULL,
IDCLASA INTEGER NOT NULL,
VALUE_INTEGER INTEGER NOT NULL
);
The RAPORT_CLASIFICARE
table is used to store multiple ID's of the S1
..S10
tables. (the user select this from an interface)
IDCLASA
is the index of the S_table for example 1 for S1, 2 for S2 and so on.
The user can select one or multiple S'es maxim are 10.
The goal of the user selection is 'you select some "clasifications"' and the result is only the articles that cumulatively correspond to the selection.
For example I can select from S1 ID [3,4,5] and from S3 [9,13,22] and from S7 [1,2,3] and the result must be articles that have ARTICLES.S1 in (3,4,5) AND ARTICLES.S3 in (9,13,22) AND ARTICLES.S7 IN (1,2,3).
From code (we are working with Delphi) this is made by using LEFT JOIN
SELECT *
FROM ARTICLES A
LEFT JOIN RAPORT_CLASIFICARE RC_1 ON ((RC_1.IDOPERATOR = :AIDOPERATOR) AND (RC_1.IDCLASA = 1) AND (RC_1.VALUE_INTEGER = A.S1))
LEFT JOIN RAPORT_CLASIFICARE RC_3 ON ((RC_3.IDOPERATOR = :AIDOPERATOR) AND (RC_3.IDCLASA = 3) AND (RC_3.VALUE_INTEGER = A.S3))
WHERE A.ARTNR > 0
AND (RC_1.ID IS NOT NULL)
AND (RC_3.ID IS NOT NULL)
The SQL is ok as result, but I want to create a stored procedure to do this. Is there a way to do this? I have find a solution to work "correct" but is impossible to use because the slow speed. All the columns are indexed. We are avoiding using IN clause because is extremely slow.