Let's say we have a database with a given table named food. The food table has many columns but we are interested in only 3 of them:
buyorsell, which specifies if the food is for selling or buying.
amount, which is the amount of food to be bought or sold.
isVegan, which is set true if the food is vegan.
The goal is to find the best match between 2 rows. The best match should satisfy the following rules:
- Both rows have the same value for 'isVegan'
- One row should be buy/sell and the other row must be the opposite
- Both amounts must be equal
The table will have millions of rows and the table is changing rapidly.
What is the right algorithm in matters of speed and resource usage for this matching problem?