Record matching with String Grid components - Seeking better algorithm for this task

I'm working on a project that is supposed to be generating a list of matched items off of data drawn from a database. Each record in the table I'm drawing from consists of two records from a previous table matched together on one or more of a group of certain criteria (via database join), and contains two key values for this stage of the project - ID1 and ID2.

I'm trying to match all possible matching sets, not just sets of two. As an example, suppose I've got five records R1-R5 that match as follows:

This gives me, from the database, eight records I have to look at - one for each of the matches above, and one for their mirror inverse versions (R1-R2<=>R2-R1). By the instructions I'm working under, all five of my original records should be grouped together, as each group is supposed to receive a unique group id value in the next step.

I've developed code that seems to do the job, in a brute-force manner using a pair of string grids - the code is listed below. Grid 'Mimic' is directly loaded out with the database information, while grid 'Meta' is the one I'm trying to do my actual matching in.

For my little five-record sample above, this works - but I'm not working with a simple five samples, I'm working with a dataset of some 30,000 records, and no particular way of knowing what matches what or how many connected matches a group might have in it. The only time I've attempted to run that code block above against the full data set, it took well over twenty hours to run. Given that my original data set can change on a daily basis, this isn't an acceptable run time.

Does anyone have any suggestions for anything I could do to speed this process up so as to obtain a more reasonable runtime? I'm certain my current version is very poor and inefficient, but I'm also uncertain as to how to make it more efficient at this point.

It may help to provide a short version of what your original data looks like (as in the structure of the database tables the data originates from), and what it looks like after the database Join to produce your ID1 and ID2 key values.

As the question stands now, all we know is that you have some information in two StringGrids, not even how many columns it has, and I'm not sure how you get criterian C1, C2, ..., Cn

If your original data is sitting within a database, then the way to make it more efficient is to use the power of the database to reduce the number of records to just those you want to see and in the format you want to see it in. To do so, you need to use SQL which is a SET based approach as opposed to a ROW based approach you are attempting to solve your problem with.