SQL - query optimization to work with big data
<p>I have table 33_PROBLEM with columns ROOT and ROOT_CUBED. Ten i have a simple procedure, that insert data, let´s begins with ROOT from -10000 to 10000, which means ROOT_CUBED from -10000^3 to 10000^3.</p>

<p><strong>Question is simple:</strong>
How can I get all triplets combinations of ROOT_CUBED values, that add to number given?</p>

<p><strong>Said in different way:</strong>
I want to find A, B, C for which is true, that A^3 + B^3 + C^3 = number_given</p>

<hr>

<p>Here is some example for searched number 33:</p>

<pre><code>SELECT T1.r1, 
       T2.r2, 
       T3.r3 
FROM   (SELECT root_3 AS R1 
        FROM   `33_problem`) AS T1, 
       (SELECT root_3 AS R2 
        FROM   `33_problem`) AS T2, 
       (SELECT root_3 AS R3 
        FROM   `33_problem`) AS T3 
WHERE  T1.r1 + T2.r2 + T3.r3 = 33 
</code></pre>

<p>It works well ... on a small amount of rows. This query makes (COUNT *)^3 rows, which for 20000 input lines equals to 8e+12 rows !! ... RIP serever ...</p>

<hr>

<p>what is the right way to solve this one?</p>

<hr>

<p>( I got the idea from <a href="https://www.youtube.com/watch?v=wymmCdLdPvM" rel="nofollow">https://www.youtube.com/watch?v=wymmCdLdPvM</a> and I hope, when someone comes with some answers, i will understand better, how SQL works and how queries and databases should be designed to work good even for big data )</p>