I have a query which takes quite a long time for selecting data. Let me share my data schema and do you have any idea for high speed instead of my SQL?
---------------------------------------------------
time | statA | statB | statC | ... |
---------------------------------------------------
2018-01-01 00:00:00 | 1 | 2 | 1 | ... |
---------------------------------------------------
2018-01-01 00:00:01 | 2 | 3 | 5 | ... |
---------------------------------------------------
2018-01-01 00:00:02 | 1 | 4 | 3 | ... |
---------------------------------------------------
...
What I'd like to get using the query looks like:
---------------------------------------
time | stat | value |
---------------------------------------
2018-01-01 00:00:00 | statA | 1 |
---------------------------------------
2018-01-01 00:00:01 | statA | 2 |
---------------------------------------
2018-01-01 00:00:02 | statA | 1 |
---------------------------------------
...
...
---------------------------------------
time | stat | value |
---------------------------------------
2018-01-01 00:00:00 | statB | 2 |
---------------------------------------
2018-01-01 00:00:01 | statB | 3 |
---------------------------------------
2018-01-01 00:00:02 | statB | 4 |
---------------------------------------
...
...
So, my query is now
(select time, statA as stat, statA as value
from table)
union (
select time, statB as stat, stastB as value
from table)
union (
select time, statC as stat, stastC as value
from table)
Actually, I'm feeling like I'm so stupid because I have to write same SQLs which are only different with stat id repeatedly.
Any recommendation and ways to develop my SQL?