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 86103
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78620
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95334
42 블록체인 기반 플랫폼 비즈니스를 이해하자 졸리운_곰 2017.09.10 1553
41 다운타임 없는 서비스 구현 패턴 file 졸리운_곰 2017.09.10 1229
40 테이블의 수직분할과 수평분할에 대한 이해 file 졸리운_곰 2017.09.10 3908
39 정규화와 응집도에 대한 고찰 file 졸리운_곰 2017.05.28 1782
38 머신러닝 새 도전…“클라우드를 벗어나라" file 졸리운_곰 2017.05.28 1711
37 보안성 높이는 공공 거래장부 블록체인 file 졸리운_곰 2017.05.28 1757
36 디지털, 속도의 전쟁 VS 데이터, 품질의 전쟁 file 졸리운_곰 2017.05.05 1515
35 04. 데이터 모델링의 3단계 진행 file 졸리운_곰 2016.03.15 1659
34 데이터베이스 설계의 기본 원리.pdf file 졸리운_곰 2016.03.15 2353
33 실체유형(Entity Type) 정의 사항 및 도출 file 졸리운_곰 2015.05.21 2196
32 마농의 SQL 백문백답: 단순하고 쉽게 작성하는 SQL 노하우 [1회] file 졸리운_곰 2015.05.21 1829
31 sql개발자-sql전문가자격시험 시험 예제.pdf file 졸리운_곰 2015.02.15 2302
30 데이터베이스 선정에는 비밀이 있다 - 4부 졸리운_곰 2015.01.15 2095
29 데이터베이스 선정에는 비밀이 있다 - 3부 졸리운_곰 2015.01.15 2041
28 데이터베이스 선정에는 비밀이 있다 - 2부 졸리운_곰 2015.01.15 1827
27 데이터베이스 선정에는 비밀이 있다 - 1부 졸리운_곰 2015.01.15 2390
26 지금 우리에게 필요한 것은 데이터베이스 성능 최적화이다 (2부) 졸리운_곰 2015.01.15 1484
25 우리에게 필요한 것은 데이터베이스 성능 (1부) 졸리운_곰 2015.01.15 1773
24 21회 결과 secret 졸리운_곰 2014.11.10 0
23 [데이터아키텍쳐준전문가] 시험 fail 자료 secret 졸리운_곰 2014.08.31 0
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED