mardi 23 février 2016

Late calculation of a field

I am using SQLite 3.11. I create a table as follows:

            CREATE TABLE MyTable (F1 INTEGER, F2 INTEGER, F3 INTEGER, F4 INTEGER);

Then add the following records:

            INSERT INTO MyTable (F1, F2) Values (1, 1); 
            INSERT INTO MyTable (F1, F2) Values (1, 2); 
            INSERT INTO MyTable (F1, F2) Values (2, 2); 
            INSERT INTO MyTable (F1, F2) Values (2, 3); 

I want to select from MyTable and sort based on F1, then F3, then F4, as follows:

SELECT * FROM MyTable ORDER BY F1, F3, F4

But the F3 and F4 is calculated based on F1, F2 and the calculation is a bit time-consuming. In such a case, is it possible to perform a late calculation to improve the performance.

That is:

  1. Compare two records with their F1 values. If they are different, then return the order directly.
  2. If their F1 values are the same, then calculate the F3 value and sort based on F3.

Is that possible?

Thanks

Aucun commentaire:

Enregistrer un commentaire