Skip to content
Advertisement

Query Search Algorithm using priority array and ignoring conditions

I am facing this problem at my companies logistics:

Given array of mixed values

ordered by priority ASC (v1 is more important than v2, … etc)

I need to search values in a table t like this:

The 3 dots in query are the fixed conditions

If I cant find any value from query, perform same query ignoring the least important value from array

Again if value was not found. perform a query ignoring the next least important value

The query can ignore all elements from array as last query

Repeat that until find at least 1 query result or all elements in array are nulled and no result were found. (return false)

I did my search algorithm using binary numbers to find the value which works like this

transform binary 15 in array of boolean exploded = [1, 1, 1, 1] (15 in binary is 1111)

Than make the query considering position 0 of booleanArray exploded if true then Consider value in arr of mixed values and so on

loop from 15 to 0 (the 0 iteration is when the boolean array is [0,0,0,0] and ignores all cases The sequence will be like this with this logic:

The search algorithm works fine but I have a huge problem to performance.

The number of queries performed in this search is arr.length! (fatorial) So when the array is length 4, the worst case scenario performs 24 queries. if the arr.length is 6 (what is the length I am dealing now in my code in production) the worst case performs 720 queries which is unacceptable.

I need a way to Improve this search. Can someone help me?

Thanks in advance.

Advertisement

Answer

I came up to the solution using the concept of row’s desirability suggested by @RBarryYoung

Instead of performing multiple queries, I fetch all relevant rows into a datatable (1 query) and for each rows I applied a score based in desirability. (Code side)

It reduced the complexity of n! to just 1 query and n filters. System are up and running smoothly. Thanks for the help @RBarryYoung

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement