RDB SQL을 Key-value no sql로 변경하는 방법 

SQL in CockroachDB: Mapping Table Data to Key-Value Storage

SQL in CockroachDB: Mapping Table Data to Key-Value Storage

SQL? I thought CockroachDB was a key-value store?!?

In the past we described CockroachDB as a distributed, transactionally consistent, key-value store. We knew that a key-value API was not the endpoint we wanted to provide and a few months ago started work on a higher level structured data API that would support tables and indexes. Along with supporting such rich structures, we anticipated eventually supporting SQL for manipulating and accessing this structured data. After a bit of soul searching we embraced the inevitable and moved forward full-speed with SQL as the core of our structured data layer.

There are a lot of components to an SQL system: query parsing, query analysis, query planning, query execution, transactions, and persistent storage – to name a few. The CockroachDB SQL system is built on top of the internal CockroachDB key-value store and leverages the monolithic sorted key-value map to store all of the SQL table data and indexes. This post will focus on CockroachDB’s mapping of SQL data to the key-value store* and show how that mapping helps implement SQL functionality. Future posts will talk about query analysis, planning, and execution.

An SQL table is a set of rows where each row is a set of columns. Each column has an associated type (bool, int, float, string, bytes). A table also has associated indexes which allow for the efficient retrieval of a range of rows from a table. This sounds nothing at all like a key-value API where string keys map to string values. How do we map the SQL table data to KV storage?

First, a primer: CockroachDB’s internal key-value API supports a number of operations, but we only need to know about a few of them for this post:

  • ConditionalPut(key, value, expected-value) - Conditionally set the value at the specified key if the existing value matches the expected value.
  • Scan(start-key, end-key) - Retrieve all of the keys between start-key (inclusive) and end-key (exclusive).

In CockroachDB, keys and values are both strings which can contain unrestricted byte values. OK, let’s move on!

Key Encoding

A foundational piece of the puzzle for mapping SQL table data to keys and values is encoding typed column data into strings. For example, given a tuple of values <1, 2.3, "four”>, we would like to encode this to a string that conceptually looks like:

/1/2.3/"four”

We’re using forward-slash as a visual separator between values, though this is only for readability purposes. We could dedicate an entire blog post to how these encodings work; in the interest of brevity, this post only discusses their properties, not their implementation. The encoded keys sort such that each field of the key is considered separately:

/1/2.3/"four”

/2/3.1/"six”

/10/4.6/"seven”

If you were to naively sort these strings you’d discover that /10/… sorts before /2/…. How the encoding works can appear a bit magical if you haven’t encountered it before. If you’re interested in the nitty-gritty details, see {Encode,Decode}{Varint,Float,Bytes,Null} in the util/encoding package.

With this encoding tool at our disposal, we’re ready to take a peek at the encoding of SQL table data. In CockroachDB, each table has a unique 64-bit integer ID assigned to it at creation. This table ID is used as the prefix for all keys associated with that table. Now let’s consider the following table and data:

CREATE TABLE test (
      key       INT PRIMARY KEY,
      floatVal  FLOAT,
      stringVal STRING
)

INSERT INTO test VALUES (10, 4.5, "hello”)

Every table in CockroachDB must have a primary key. The primary key is composed of one or more columns; in our test table above, it is composed of a single column. CockroachDB stores each non-primary key column under a separate key that is prefixed by the primary key and suffixed by the column name. The row <10, 4.5, "hello”> would be stored in our test table as:

Key Value
/test/10/floatVal 4.5
/test/10/stringVal "hello”

In this depiction, we’re using /test/ as a placeholder for the table ID and /floatVal and /stringVal as placeholders for the column ID (every column in a table has an ID that is unique within the table). Note that the primary key immediately follows the table ID in our encoding. This is the basis for index-scans in CockroachDB’s SQL implementation.

If we were to look under the hood, we would see the table metadata:

test Table ID 1000
key Column ID 1
floatVal Column ID 2
stringVal Column ID 3

In numeric form, the key-value pairs for our table look like:

Key Value
/1000/10/2 4.5
/1000/10/3 "hello”

We’ll stick with the symbolic form for keys for the rest of this post.

[You might be thinking that the common prefixes (/1000/10) for the keys are wasting storage, but RocksDB our underlying storage engine, eliminates almost all of the overhead via prefix compression of keys.]

Astute readers will note that storing key-value pairs for columns which are contained in the primary key is not necessary, as these columns’ values are already encoded in the key itself. Indeed, CockroachDB elides these.

Notice that all of the keys for a particular row will be stored next to each other due to the primary key prefix (remember, key-value data is stored in a sorted monolithic map in CockroachDB, so this property is “free”). This allows retrieval of the values for a particular row using a Scan on the prefix. And this is exactly what CockroachDB does internally.

The query:

SELECT * FROM test WHERE key = 10

Will be translated into:

Scan(/test/10/, /test/10/Ω)

This scan will retrieve only the two keys for the row. The query execution engine will then decode the keys to reconstruct the row.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Null Column Values

There is a small twist to this story: column values can be NULL unless explicitly marked as NOT NULL. CockroachDB does not store NULL values and instead uses the absence of a key-value pair for a column to indicate NULL. The observant might notice a wrinkle here: if all of the non-primary key columns for a row are NULL we won’t store any keys for the row. To address this case, CockroachDB always writes a sentinel key for each row that is the primary key without a column ID suffix. For our example row of <10, 4.5, "hello”>, the sentinel key would be: /test/10. Huzzah!

Secondary Indexes

So far we’ve ignored secondary indexes. Let’s rectify that oversight:

CREATE INDEX foo ON test (stringVal)

This creates a secondary index on the column stringVal. We haven’t declared the index as unique, so duplicate values are allowed. Similar to the rows for a table, we’ll be storing all of the index data in keys prefixed by the table ID. But we want to separate the index data from the row data. We accomplish that by introducing an index ID which is unique for each index in the table, including the primary key index (sorry, we lied earlier!):

/tableID/indexID/indexColumns[/columnID]

The keys we used as examples above get slightly longer:

Key Value
/test/primary/10 Ø
/test/primary/10/floatVal 4.5
/test/primary/10/stringVal "hello”

And now we’ll also have a single key for the row for our index foo:

Key Value
/test/foo/”hello”/10 Ø

You might be wondering why we suffixed this encoding with the primary key value (/10). For a non-unique index like foo, this is necessary to allow the same value to occur in multiple rows. Since the primary key is by definition unique for the table, appending it as a suffix to a non-unique key results in a unique key. In general, for a non-unique index, CockroachDB appends the values of all columns which are contained in the primary key but not contained in the index in question.

Now let’s see what happens if we insert <4, NULL, "hello”>into our table:

Key Value
/test/primary/4 Ø
/test/primary/4/stringVal "hello”
/test/foo/"hello”/4 Ø

All of the table data together looks like:

Key Value
/test/primary/4 Ø
/test/primary/4/stringVal "hello”
/test/primary/10 Ø
/test/primary/10/floatVal 4.5
/test/primary/10/stringVal "hello”
/test/foo/"hello”/4 Ø
/test/foo/"hello”/10 Ø

Secondary indexes are used during SELECT processing to scan a smaller set of keys. Consider:

SELECT key FROM test WHERE stringVal = "hello”

The query planner will notice there is an index on stringVal and translate this into:

Scan(/test/foo/”hello”/, /test/foo/”hello"/Ω)

Which will retrieve the keys:

Key Value
/test/foo/”hello”/4 Ø
/test/foo/”hello”/10 Ø

Notice that these keys contain not only the index column stringVal, but also the primary key column key. CockroachDB will notice the primary key column key and avoid an unnecessary lookup of the full row.

Finally, let’s look at how unique indexes are encoded. Instead of the index foo we created earlier, we’ll create uniqueFoo:

CREATE UNIQUE INDEX uniqueFoo ON test (stringVal)

Unlike non-unique indexes, the key for a unique index is composed of only the columns that are part of the index. The value stored at the key is an encoding of all primary key columns that are not part of the index. The two rows in our test table would encode to:

Key Value
/test/uniqueFoo/"hello” /4
/test/uniqueFoo/"hello” /10

We use ConditionalPut when writing the key in order to detect if the key already exists which indicates a violation of the uniqueness constraint.

So that’s how CockroachDB maps SQL data to the key-value store in a nutshell. Keep an eye out for upcoming posts on query analysis, planning, and execution.

 

* The idea of implementing SQL on top of a key-value store isn’t unique to CockroachDB. This is essentially the design of MySQL on InnoDB, Sqlite4, and other databases, as well.

 

[출처] https://www.cockroachlabs.com/blog/sql-in-cockroachdb-mapping-table-data-to-key-value-storage/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86098
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78617
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95332
22 Docker에서 SQL Server 컨테이너 이미지 구성 file 졸리운_곰 2020.01.23 1552
21 MSSQL 설치형 한글 환경으로 변경 file 졸리운_곰 2020.01.23 2885
20 PRIMARY KEY 와 FOREIGN KEY 를 전부 뽑아주는 쿼리 졸리운_곰 2018.12.16 1773
19 [MSSQL] CASE 문 . 조건에 따라 값 정하기 ! CASE WHEN THEN 졸리운_곰 2018.07.24 1565
18 Track Data Changes (SQL Server) file 졸리운_곰 2018.07.02 1302
17 Docker가 있는 SQL Server 2017 컨테이너 이미지를 실행 하는 빠른 시작 file 졸리운_곰 2018.06.26 1054
16 [MSSQL] Management Studio 이용해 데이터베이스 생성하기 file 졸리운_곰 2018.06.17 1281
15 [MSSQL - GROUP BY HAVING 을 이용한 중복 데이타 체크] file 졸리운_곰 2018.06.15 1275
14 [SQL] select 한 결과로 update 처리, SQL한문장, How to UPDATE from SELECT in SQL Server 졸리운_곰 2018.01.22 1470
13 UNION으로 결과 집합 조합 졸리운_곰 2017.08.27 1340
12 uniqueidentifier(Transact-SQL) file 가을의곰 2017.06.10 1754
11 하위 쿼리를 사용하여 다른 쿼리 또는 식에 쿼리 중첩 [MS-ACCESS : ms offce suit] 가을의곰 2017.06.10 1638
10 [MS-SQL] 테이블명, 컬럼명 검색 졸리운_곰 2017.04.17 1930
9 DB의 모든 테이블에서 데이터 검색 졸리운_곰 2017.04.17 1716
8 Microsoft SQL Server DBA 가이드-DBA라면 이정도는 알아야한다!!! file 졸리운_곰 2017.01.15 1302
7 SQL Server DBA 가이드 file 졸리운_곰 2017.01.15 1685
6 IDENTITY_INSERT가 OFF로 설정되면 ‘테이블명’ 테이블의 ID 열에 명시적 값을 삽입할 수 없습니다 file 졸리운_곰 2017.01.15 1501
5 MS SQL 서버에서 자동증가, autoincrement 처리 file 졸리운_곰 2017.01.15 1771
4 MS SQL 서버의 날짜, 시간 => 문자열 변환 포멧 설명 졸리운_곰 2017.01.15 1214
3 MS SQL 서버 코딩 표준 가이드 file 졸리운_곰 2017.01.14 1653
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED