Prolog compilers already do this. Imagine if you had the program:
p(t1) :- body_1.
p(t2) :- body_2.
p(tn) :- body_n.
and you issued the query p(t).
There is a lot of literature on this topic, and I don't know if there is a recent review which covers it all. You could start with:
Colomb, J. Enhancing Unification in Prolog through Clause Indexing, J. ...