Optimization of systems of algebraic equations for evaluating datalog queries