Kriteria pencarian seperti itu tidak akan dapat menggunakan indeks apa pun, tetapi dapat dilakukan...
SELECT
*
FROM
yourTable
WHERE
N-M <= CASE WHEN yourTable.field1 = searchValue1 THEN 1 ELSE 0 END
+ CASE WHEN yourTable.field2 = searchValue2 THEN 1 ELSE 0 END
+ CASE WHEN yourTable.field3 = searchValue3 THEN 1 ELSE 0 END
...
+ CASE WHEN yourTable.fieldN = searchValueN THEN 1 ELSE 0 END
Demikian pula, jika kriteria pencarian Anda ada di tabel lain...
SELECT
*
FROM
yourTable
INNER JOIN
search
ON N-M <= CASE WHEN yourTable.field1 = search.field1 THEN 1 ELSE 0 END
+ CASE WHEN yourTable.field2 = search.field2 THEN 1 ELSE 0 END
+ CASE WHEN yourTable.field3 = search.field3 THEN 1 ELSE 0 END
...
+ CASE WHEN yourTable.fieldN = search.fieldN THEN 1 ELSE 0 END
(Anda perlu mengisi nilai N-M
sendiri)
EDIT:
Pendekatan yang lebih panjang lebar, yang dapat membuat beberapa penggunaan indeks...
SELECT
id, -- your table would need to have a primary key / identity column
MAX(field1) AS field1,
MAX(field2) AS field2,
MAX(field3) AS field3,
...
MAX(fieldN) AS fieldN
FROM
(
SELECT * FROM yourTable WHERE field1 = searchValue1
UNION ALL
SELECT * FROM yourTable WHERE field2 = searchValue2
UNION ALL
SELECT * FROM yourTable WHERE field3 = searchValue3
...
SELECT * FROM yourTable WHERE fieldN = searchValueN
)
AS unioned_seeks
GROUP BY
id
HAVING
COUNT(*) >= N-M
Di mana Anda memiliki indeks pada setiap bidang secara individual, dan di mana Anda mengharapkan jumlah kecocokan yang relatif rendah untuk setiap bidang mungkin ini mengungguli opsi pertama, dengan mengorbankan kode yang sangat berulang.