| | 1 | [[PageOutline]] |
| | 2 | = MariaDB SEQUENCE 스토리지 엔진 = |
| | 3 | https://mariadb.com/kb/en/sequence-storage-engine/ |
| | 4 | |
| | 5 | 10.0 버전부터 지원. |
| | 6 | |
| | 7 | 잘 사용하면 커서를 사용하지 않고(반복 작업을 하지 않고) 성능을 향상시킬 수 있다. |
| | 8 | |
| | 9 | sequence 스토리지 엔진을 사용하려면 테이블 이름을 다음과 같이 지정해주면 된다. '''seq_FROM_to_TO''' 또는 '''seq_FROM_to_TO_step_STEP''' |
| | 10 | * FROM: 시작숫자 |
| | 11 | * TO: 끝 숫자 |
| | 12 | * STEP: 간격. 1일 때는 생략. |
| | 13 | |
| | 14 | 오름차순일 때는 FROM에서 시작한다. TO 이전에 끝날 수 있다. |
| | 15 | {{{ |
| | 16 | SELECT * FROM seq_1_to_15_step_3; |
| | 17 | +-----+ |
| | 18 | | seq | |
| | 19 | +-----+ |
| | 20 | | 1 | |
| | 21 | | 4 | |
| | 22 | | 7 | |
| | 23 | | 10 | |
| | 24 | | 13 | |
| | 25 | +-----+ |
| | 26 | }}} |
| | 27 | |
| | 28 | 내림차순일 때는 TO에서 끝난다. FROM에서 시작하지 않을 수 있다. |
| | 29 | {{{ |
| | 30 | SELECT * FROM seq_15_to_1_step_3; |
| | 31 | +-----+ |
| | 32 | | seq | |
| | 33 | +-----+ |
| | 34 | | 13 | |
| | 35 | | 10 | |
| | 36 | | 7 | |
| | 37 | | 4 | |
| | 38 | | 1 | |
| | 39 | +-----+ |
| | 40 | }}} |
| | 41 | |
| | 42 | == 응용 == |
| | 43 | 예 1. 40년 뒤까지 3월 14일이 무슨 요일인지 출력. |
| | 44 | {{{ |
| | 45 | SELECT DAYNAME('2008-03-14' + INTERVAL (seq) YEAR) as day, '2008-03-14' + INTERVAL (seq) YEAR as date FROM seq_0_to_40; |
| | 46 | }}} |
| | 47 | |
| | 48 | 예 2. 각 방별 상태값 출력. |
| | 49 | {{{ |
| | 50 | select transTime as '시각', |
| | 51 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].roomNum')) when 1 then '거실' else concat('방 ', json_extract(json, concat('$.rooms[', n.seq - 1, '].roomNum')) - 1) end as '방', |
| | 52 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].occupant')) when 0 then '비재실' when 1 then '재실' when 2 then '취침' end as '재실', |
| | 53 | json_extract(json, concat('$.rooms[', n.seq - 1, '].temperature')) as '온도', |
| | 54 | case when json_extract(json, concat('$.rooms[', n.seq - 1, '].mode')) is null or json_extract(json, concat('$.rooms[', n.seq - 1, '].mode')) = 1 then json_extract(json, concat('$.rooms[', n.seq -1, '].settingTemperature')) else null end as '설정 온도', |
| | 55 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].mode')) when 0 then 'Off 모드' when 1 then '난방 모드' when 2 then '외출 모드' else json_extract(json, concat('$.rooms[', n.seq - 1, '].mode')) end as '설정 모드', |
| | 56 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].lastSettingDevice')) when 0 then '사용자' when 1 then '앱' when 2 then '머신러닝' when 3 then '스케줄' end as '최종 설정 기기', |
| | 57 | json_extract(json, concat('$.rooms[', n.seq - 1, '].lastSettingTime')) as '최종 설정 시각', |
| | 58 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].settingDevice')) when 0 then '사용자' when 1 then '앱' when 2 then '머신러닝' when 3 then '스케줄' end as '설정 기기', |
| | 59 | case json_extract(json, concat('$.rooms[', n.seq - 1, '].onOff')) when 0 then 'Off' when 1 then 'On' end as '밸브' |
| | 60 | |
| | 61 | from switch_trans_json s |
| | 62 | -- 실증 세대는 스위치 번호가 최대 5까지 있기 때문에 |
| | 63 | cross join seq_1_to_5 n |
| | 64 | where houseNum like 'SS-GRO2-DASANGNG-KCH%' |
| | 65 | and transTime >= '2020-12-19 00:00:00' |
| | 66 | and transTime < '2020-12-19 09:00:00' |
| | 67 | and json_extract(json, concat('$.rooms[', n.seq - 1, '].roomNum')) is not null |
| | 68 | order by 1; |
| | 69 | }}} |
| | 70 | ---- |
| | 71 | [WikiStart 처음으로] |
| | 72 | |