ããã¯ããªã«ãããããŠæžãããã®ïŒ
TiDBã®ã¢ãŒããã¯ãã£ãŒããã£ãããšææ¡ããããšããããã¡ãã®ç¶ãã§ãã
TiDBのアーキテクチャーをざっくりと眺めてみる(全体概要、ストレージ概要まで) - CLOVER🍀
ä»åã¯ã³ã³ãã¥ãŒãã£ã³ã°ãæ±ã£ãŠã¿ãããšæããŸãã察象ã®TiDBã®ããŒãžã§ã³ã¯8.1ã§ãã
ã³ã³ãã¥ãŒãã£ã³ã°
察象ã®ããŒãžã¯ãã¡ãã§ãã
å šäœåã§ãããšãTiDBãµãŒããŒïŒTiDB clusterïŒã®éšåã§ããã
TiDB Architecture | PingCAP Docs
ãããã£ãæŠèŠã§ããã
- MySQLãããã³ã«ã®ãšã³ããã€ã³ããå€éšã«å ¬éããã¹ããŒãã¬ã¹ãªSQLã¬ã€ã€ãŒ
- SQLãªã¯ãšã¹ããåä¿¡ããŠSQLæã®è§£æãšæé©åãè¡ããåæ£å®è¡ãã©ã³ãçæãã
- æ°Žå¹³ã¹ã±ãŒãªã³ã°ãå¯èœã§ãLVSïŒLinux Virtual ServerïŒãHAProxyãªã©ã®è² è·åæ£ã³ã³ããŒãã³ããéããŠå€éšã«ã€ã³ã¿ãŒãã§ãŒã¹ãæäŸå¯èœ
- ããŒã¿ã¯ä¿åããã³ã³ãã¥ãŒãã£ã³ã°ãšSQLåæã®ã¿ã圹å²
ããã§ã¯ããå°ãæ·±å ããŸããããã¥ã¡ã³ãå
ã§TiDBãTiDBãã®ãã®ã®ããšãèšã£ãŠããã®ããã³ã³ãã¥ãŒãã£ã³ã°ã³ã³ããŒãã³ãã§ãã
TiDBãµãŒããŒã®ããšãèšã£ãŠããã®ãã¡ãã£ãšææ§ãªã®ã§ãããTiDBãµãŒããŒã®ããšãèšã£ãŠããå Žåã¯ããè¡šèšããŠãããããšæããŸãã
TiDBãµãŒããŒã¯ãTiKVãæäŸããåæ£ã¹ãã¬ãŒãžããšã¥ãããã©ã³ã¶ã¯ã·ã§ã³åŠçãšããŒã¿åæåŠçã®åªããèœåãçµã¿åããã
ã³ã³ãã¥ãŒãã£ã³ã°ãšã³ãžã³ã§ãã
Based on the distributed storage provided by TiKV, TiDB builds the computing engine that combines great capability of transactional processing with that of data analysis.
ãã®ããŒãžã§ã¯ä»¥äžã®å 容ã玹ä»ãããŠããŸãã
- TiDBãµãŒããŒã®ããŒãã«ããTiKVã®ããŒãšå€ãžã®ããŒã¿ãããã³ã°ã¢ã«ãŽãªãºã
- TiDBãµãŒããŒã®ã¡ã¿ããŒã¿ç®¡ç
- TiDBãµãŒããŒã®SQLã¬ã€ã€ãŒã¢ãŒããã¯ãã£ãŒ
ã¹ãã¬ãŒãžã«é¢ããŠã¯TiKVã®ã¹ãã¬ãŒãžæ§é ã®ã¿ã玹ä»ããŠããŠãåããŒã¹ã®ã¹ãã¬ãŒãžã§ããTiFlashã¯æ±ããŸããã
ããŒãã«ã®ããŒã¿ããããŒãšå€ãžã®ãããã³ã°
ãŸãã¯ããŒãã«ã®ããŒã¿ããããŒãšå€ãžã®ãããã³ã°ã«ã€ããŠã
TiDB Computing / Mapping table data to Key-Value
ããã§æ±ãã®ã¯2ã€ã®ã¿ã€ãã®ããŒã¿ã§ãã
- ããŒãã«å ã®åè¡ã®ããŒã¿ïŒä»¥éãããŒãã«ããŒã¿ïŒ
- ããŒãã«å ã®ãã¹ãŠã®ã€ã³ããã¯ã¹ããŒã¿ïŒä»¥éãã€ã³ããã¯ã¹ããŒã¿ïŒ
ããŒãã«ããŒã¿ã®ããŒãšå€ãžã®ãããã³ã°
TiDB Computing / Mapping table data to Key-Value / Mapping of table data to Key-Value
ããŒãã«ããŒã¿ãšããŒãšå€ã®ãããã³ã°ã¯ã次ã®ããã«èšèšãããŠããŸãã
- åãããŒãã«ã®ããŒã¿ãç°¡åã«æ€çŽ¢ã§ããããã«ãåããŒãã«ã«IDïŒTableIDïŒãå²ãåœãŠã
- TableIDã¯ã¯ã©ã¹ã¿ãŒå šäœã§äžæã®æŽæ°
- ããŒãã«å
ã®åè¡ã«IDïŒRowIDïŒãå²ãåœãŠã
- RowIDã¯ããŒãã«å ã§äžæã®æŽæ°
- ããŒãã«ã®ãã©ã€ããªãŒããŒãæŽæ°åã®å Žåãäž»ããŒã®å€ãRowIDãšããŠäœ¿çšãã
åè¡ã¯ã次ã®èŠåã«ãããã£ãŠããŒãšå€ã®ãã¢ã«ãšã³ã³ãŒããããŸãã
Key: tablePrefix{TableID}_recordPrefixSep{RowID} Value: [col1, col2, col3, col4]
tablePrefix
ãšrecordPrefixSep
ã¯ããŒç©ºéå
ã§ä»ã®ããŒã¿ãšåºå¥ããããã®æååå®æ°ã§ãã
ãã¡ããèŠããšãå®äœã¯tablePrefix
ã¯t
ãrecordPrefixSep
ã¯r
ã§ããã
TiDB Computing / Mapping table data to Key-Value / Summary of mapping relationships
ãšã³ã³ãŒãã®äŸã¯ãã¡ãã«æžãããŠããŸãã
TiDB Computing / Mapping table data to Key-Value / Example of Key-Value mapping relationship
å®ã¯ããã®ãããã«è¿ãæ å ±ã¯åã«å°ãèŠãŠããŸãã
TiDB(TiKV)のデータのリージョンごとの配置状況を確認してみる - CLOVER🍀
TiDB(TiKV)のキーのエンコード・デコード結果を確認する - CLOVER🍀
ã€ã³ããã¯ã¹ããŒã¿ã®ããŒãšå€ãžã®ãããã³ã°
TiDB Computing / Mapping table data to Key-Value / Mapping of indexed data to Key-Value
TiDBã¯ãã©ã€ããªãŒããŒãšã»ã«ã³ããªãŒã€ã³ããã¯ã¹ïŒãŠããŒã¯ã€ã³ããã¯ã¹ã§ãã£ãŠããªããŠãïŒã®äž¡æ¹ããµããŒãããŠããŸãã
ããŒãã«ããŒã¿ãããã ã£ãããã«ãã€ã³ããã¯ã¹ããŒã¿ã«ãIDïŒIndexIDïŒãå²ãåœãŠãŸãã
ãã©ã€ããªãŒããŒããã³ãŠããŒã¯ã€ã³ããã¯ã¹ã®å Žåã¯ãããŒããªã¥ãŒãããšã«RowIDãããã«èŠã€ããããããã«ä»¥äžã®ããã«
ãšã³ã³ãŒããããŸãã
Key: tablePrefix{tableID}_indexPrefixSep{indexID}_indexedColumnsValue Value: RowID
察象ãäžæã«ãªããªãã€ã³ããã¯ã¹ã®å Žåã¯ãã²ãšã€ã®ããŒãè€æ°ã®è¡ã«ãããããããšããããŸãããã®ããããŒã®ç¯å²ã«å¿ããŠRowIDã«
察å¿ããã¯ãšãªãŒãå®è¡ããå¿
èŠãããããã以äžã®ã«ãŒã«ã§ãšã³ã³ãŒããããŸãã
Key: tablePrefix{TableID}_indexPrefixSep{IndexID}_indexedColumnsValue_{RowID} Value: null
indexPrefixSep
ã¯ãtablePrefix
ãšrecordPrefixSep
ãšåããæååå®æ°ã§ããå®äœã¯i
ã§ããã
TiDB Computing / Mapping table data to Key-Value / Summary of mapping relationships
ãšã³ã³ãŒãã®äŸã¯ãããŒãã«ããŒã¿ãšåæ§ã«ãã¡ãã«æžãããŠããŸãã
TiDB Computing / Mapping table data to Key-Value / Example of Key-Value mapping relationship
ã¡ã¿ããŒã¿ç®¡ç
次ã¯ã¡ã¿ããŒã¿ç®¡çã«ã€ããŠã
TiDB Computing / Metadata management
TiDBå ã®åããŒã¿ããŒã¹ãããŒãã«ã«é¢ããå®çŸ©ãå±æ§ã¯ã¡ã¿ããŒã¿ãšããŠæ±ããããã®æ å ±ãTiKVã«ä¿åãããŸãã
ããããã®ããŒã¿ããŒã¹ãããŒãã«ã«ã¯äžæã®IDãå²ãåœãŠãããŸããããŒãã«ããŒã¿ãããŒãšå€ã«ãšã³ã³ãŒãããããšããã®IDã¯
m_
prefixä»ãã®ããŒãšããŠãšã³ã³ãŒããããŸãã
ãŸããTiDBã¯å°çšã®ããŒãšå€ã®ãã¢äœ¿ã£ãŠãã¹ãŠã®ããŒãã«æ§é æ
å ±ã®ææ°ã®ããŒãžã§ã³çªå·ãä¿åããŸãããã®ããŒãšå€ã®ãã¢ã¯
ã°ããŒãã«ã«ç®¡çãããŠããŠãDDLæäœãè¡ããããšããŒãžã§ã³çªå·ã1å¢å ããŸãã
TiDBã¯ãã®ããŒãšå€ã®ãã¢ãPDãµãŒããŒã«æ°žç¶çã«ä¿åããŸããããŒã¯/tidb/ddl/global_schema_version
ãå€ã¯int64åã®æŽæ°å€ã§ãã
ãã®äžæ¹ã§TiDBã¯ã¹ããŒãå€æŽããªã³ã©ã€ã³ã§é©çšãããããPDãµãŒããŒã«ä¿åãããŠããããŒãã«æ§é æ
å ±ã®ããŒãžã§ã³çªå·ãå€æŽãããŠ
ããªããã©ãããåžžã«ãã§ãã¯ããããã¯ã°ã©ãŠã³ãã¹ã¬ãããå®è¡ããŸãããã®ã¹ã¬ããã«ãããããŒãžã§ã³ã®å€æŽãäžå®æéå
ã«
ååŸãããããšãä¿èšŒãããŸãã
SQLã¬ã€ã€ãŒ
æåŸã¯SQLã¬ã€ã€ãŒã§ãã
TiDB Computing / SQL layer overview
SQLã¬ã€ã€ãŒã¯ãTiDBãµãŒããŒã®ããšã§ããSQLæãããŒãšå€ã®æäœã«å€æããåæ£ããŒããªã¥ãŒã¹ãã¬ãŒãžã¬ã€ã€ãŒã§ããTiKVã«è»¢éããŸãã
ãããŠTiKVããè¿ãããçµæãçµã¿ç«ãŠãŠãæçµçã«ã¯ã©ã€ã¢ã³ãã«ã¯ãšãªãŒã®çµæãšããŠè¿ããŸãã
TiDBãµãŒããŒã¯ã¹ããŒãã¬ã¹ã§ããããŒã¿ãæãããã©ã®ããŒããåãã§ãã
SQLã³ã³ãã¥ãŒãã£ã³ã°
TiDB Computing / SQL layer overview / SQL computing
SQLã³ã³ãã¥ãŒãã£ã³ã°ã®ãã£ãšãç°¡åãªãœãªã¥ãŒã·ã§ã³ã¯ãåè¿°ã®ããã«ããŒãã«ããŒã¿ãããŒãšå€ã«ãããã³ã°ããããšã§ãã
ããã§SQLã¯ãšãªãŒãKey Valueã¯ãšãªãŒã«ãããã³ã°ãããKey Valueã€ã³ã¿ãŒãã§ãŒã¹ãéããŠå¯Ÿå¿ããããŒã¿ã®ååŸãèšç®åŠçã
è¡ããŸãã
äŸãšããŠãselect count(*) from user where name = "TiDB"
ãšããSQLæããããŸãããã®SQLæãå®è¡ããã«ã¯ãTiDBã¯ããŒãã«å
ã®
ãã¹ãŠã®ããŒã¿ãèªã¿åããname
ã«ã©ã ããTiDBãã§ãããã©ãã確èªããäžèŽããå Žåã¯ãã®è¡ãè¿ããŸãã
ãã®ããã»ã¹ã¯ä»¥äžã®ãšããã§ãã
- ããŒã®ç¯å²ãæ§ç¯ãã
- ããŒãã«å
ã®ãã¹ãŠã®RowIDã¯
[0, MaxInt64)
ã®ç¯å²ã«ãã - è¡ããŒã¿ã®ããŒã®ãšã³ã³ãŒãã£ã³ã°ã«ãŒã«ã«ãããã£ãŠ0ãã
MaxInt64
ã®ç¯å²ã§[StartKey, EndKey)
ãæ§ç¯ãã - StartKeyã®å€ã¯å«ãïŒleft-closedïŒãEndKeyã®å€ã¯å«ãŸãªãïŒright-openïŒ
- ããŒãã«å
ã®ãã¹ãŠã®RowIDã¯
- ããŒã®ç¯å²ãã¹ãã£ã³ãã
- åã®ã¹ãããã§æ§ç¯ããããŒã®ç¯å²ã«ãããã£ãŠãTiKVã®ããŒã¿ãèªã¿åã
- ããŒã¿ã®ãã£ã«ã¿ãªã³ã°
- èªã¿åãããåè¡ã«å¯ŸããŠã
name = "TiDB"
ãèšç®ãã - çµæã
true
ã®å Žåã¯ãã®è¡ãè¿ããããã§ãªãå Žåã¯ã¹ããããã
- èªã¿åãããåè¡ã«å¯ŸããŠã
count(*)
ãèšç®ãã- èŠä»¶ãæºããåè¡ããšã«ã
count(*)
ã®çµæã«å ç®ãã
- èŠä»¶ãæºããåè¡ããšã«ã
ãã®ããã»ã¹ãå³ç€ºãããã®ããã¡ãã§ãã
ã€ãŸããTiDBãµãŒããŒãTiKVããåè¡ã®ããŒã¿ãåãåºããTiDBãµãŒããŒããã£ã«ã¿ãªã³ã°ãšéèšãããŠããŸãã
ããã¯ããããããã®ã§ããã以äžã®ãããªåé¡ããããŸãã
- ããŒã¿ãã¹ãã£ã³ãããéã«ãåè¡ã¯å°ãªããšãã²ãšã€ã®ïŒRPCæäœã®ãªãŒããŒãããã䌎ãïŒKey Valueæäœãè¡ã
- ã¹ãã£ã³ããããŒã¿ã®éãå€ãå Žåã¯ããã®ãªãŒããŒãããããšãŠãé«ããªãå¯èœæ§ããã
- æ¡ä»¶ãæºãããªãããŒã¿ã¯èªã¿åãå¿ èŠããªãããã¹ãŠã®è¡ã«é©çšããå¿ èŠã¯ãªã
- ãã®ã¯ãšãªãŒã®è¿ãããçµæã«ã¯èŠä»¶ã«äžèŽããè¡ã®æ°ã®ã¿ãå¿ èŠã§ããããããã®è¡ã®å€ã¯å¿ èŠãªã
èŠããã«ãã ããå€ããšããããã§ããã
åæ£SQLæäœ
TiDB Computing / SQL layer overview / Distributed SQL operations
åè¿°ã®SQLã³ã³ãã¥ãŒãã£ã³ã°ã®ç¯ã§æããããåé¡ã解決ããRPCåŒã³åºãã倧éè¡ãããã®ãé¿ããããã«ã¯ãèšç®ãã¹ãã¬ãŒãžããŒãã«
ã§ããã ãè¿ã¥ããå¿
èŠããããŸãã
ãŸãSQLã®è¿°èªã§ããname = "TiDB"
ãã¹ãã¬ãŒãžããŒãã«ããã·ã¥ããŠã³ããæå¹ãªè¡ã®ã¿ãè¿ãããããã«ããŸããããã§ãããã¯ãŒã¯
転éãå¿
èŠä»¥äžã«è¡ãããç¶æ³ãåé¿ããŸãã
次ã«ãéèšé¢æ°count(*)
ãã¹ãã¬ãŒãžããŒãã«ããã·ã¥ããŠã³ããŠäºåéèšããŸããåã¹ãã¬ãŒãžããŒãã¯ãcount(*)
ã®çµæã®ã¿ãè¿ããŸãã
SQLã¬ã€ã€ãŒïŒTiDBãµãŒããŒïŒã¯ãåããŒãããè¿ãããcount(*)
ã®çµæãåèšããŸãã
ãã®æ§åãè¡šããã®ã以äžã®å³ã§ãã
SQLã¬ã€ã€ãŒã®ã¢ãŒããã¯ãã£ãŒ
TiDB Computing / SQL layer overview / Architecture of SQL layer
ãããŸã§ããSQLã¬ã€ã€ãŒã®æ©èœã®ããã€ãã®çŽ¹ä»ã§ãSQLæã®åŠçæ¹æ³ã«ã€ããŠåºæ¬çãªå 容ã«ãªã£ãŠããŸããã
ã§ãããå®éã®SQLã¬ã€ã€ãŒã«ã¯å€ãã®ã¢ãžã¥ãŒã«ãšã¬ã€ã€ãŒãããããã£ãšè€éã§ãã以äžã®å³ãéèŠãªã¢ãžã¥ãŒã«ãšåŒã³åºãé¢ä¿ã
瀺ããŠããŸãã
ãŠãŒã¶ãŒã®SQLãªã¯ãšã¹ãã¯ãTiDBãµãŒããŒã«çŽæ¥ããŸãã¯ããŒããã©ã³ãµãŒçµç±ã§éä¿¡ãããŸãã
TiDBãµãŒããŒã¯MySQLãããã³ã«ãã±ããã解æãããªã¯ãšã¹ãã®å
容ãååŸããŸãããããŠSQLãªã¯ãšã¹ããæ§æçãæå³çã«è§£æãã
ã¯ãšãªãŒãã©ã³ãéçºããŠæé©åããŸããã¯ãšãªãŒãã©ã³ãå®è¡ããåŸã¯ãããŒã¿ãååŸããŠåŠçããŸãã
ãã¹ãŠã®ããŒã¿ã¯TiKVã¯ã©ã¹ã¿ãŒã«ä¿åãããŠããããããã®ããã»ã¹ã§ã¯TiDBãµãŒããŒã¯TiKVãšå¯Ÿè©±ããŠããŒã¿ãååŸããŸãã
æåŸã«TiDBãµãŒããŒã¯ãã¯ãšãªãŒã®çµæããŠãŒã¶ãŒã«è¿ããŸãã
ãããã«
TiDBã®ã¢ãŒããã¯ãã£ãŒãã³ã³ãã¥ãŒãã£ã³ã°ã®æŠèŠããã£ãšèŠãŠã¿ãŸããã
ã³ã³ãã¥ãŒãã£ã³ã°ããŒãã§ãããTiDBãµãŒããŒã®åœ¹å²ãããŒã¿ãããŒãšå€ã«ãããã³ã°ããèãæ¹ããããŠSQLå®è¡ã®æµãã®åºç€çãª
å
容ãããã£ããããªæ°ã¯ããŸãã
ãšã¯ããããŸã ãŸã äžæŸã¿ãªæãã¯ãããããã®ã§ããã®ãŸãŸããã¥ã¡ã³ããèªã¿é²ããŠãããšç解ãæ·±ãŸããšãããªãšæããŸãã