Skip to Content
CockroachDB:权威指南
book

CockroachDB:权威指南

by Guy Harrison, Jesse Seldess, Ben Darnell
May 2025
Intermediate to advanced
488 pages
6h 44m
Chinese
O'Reilly Media, Inc.
Content preview from CockroachDB:权威指南

第 5 章 CockroachDB 模式设计 CockroachDB 模式设计

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

完善的数据模型是高性能、可维护应用程序的基础。 在本章中,我们将回顾关系模式设计的基本原理,尤其关注与分布式数据库操作有关的模式设计方面,以及高级 CockroachDB 功能,如列族和 JSON 二进制 (JSONB) 支持。我们将介绍如何创建表格、索引和其他模式对象,以支持设计良好的 CockroachDB 应用程序。

尽管 CockroachDB 支持在线高效更改模式的机制,但对生产应用程序的模式更改仍是影响较大的更改,通常涉及对应用程序代码和生产数据库配置的协调更改。 如果操作不当,就有可能导致应用程序功能、可用性或性能的损失。 因此,尽管在生产中更改 CockroachDB 模式是完全可能的,但最好还是在应用程序设计过程中就做好模式。

关系数据库设计是一个很大的话题,已经成为许多书籍和持续辩论的主题。 我们不想在这里讨论高级设计原则,也不想参与任何关于各种设计模式纯粹性的争论。 大多数数据库模式都是关系模式的数学纯度与物理数据库系统的实用性之间的折衷。 因此,在本章中,我们将尝试只简要介绍关系模式的理论方面,同时深入探讨如何设计一种能与 CockroachDB 实现良好配合的模式。

逻辑数据建模

应用程序数据模型通常分两个阶段创建。建立逻辑数据模型包括对应用程序将存储和处理的信息进行建模,并确保正确、完整和明确地表示所有必要数据。 然后将逻辑数据模型映射到物理数据模型。物理数据模型描述在 DBMS 中创建的表、索引和视图。

逻辑数据模型通常只满足应用程序的功能要求。 物理数据模型还必须满足非功能性要求,尤其是性能要求。

在实践中,这两个阶段经常被模糊在一起,尤其是在 Agile 和其他迭代开发环境中。 尽管如此,无论是否明确说明,确定应用程序可能处理哪些数据所需的分析与如何在特定数据库系统中最好地表示这些数据之间肯定是有区别的。

我们在第 1 章中介绍了关系模型的一些核心概念。 从理论上讲,在逻辑数据建模过程中,我们处理的是关系、元组和属性,而在物理设计中,我们处理的是表、行和列。 然而,在学术界之外,这些区别往往被忽视,在实践中,使用表和列的语言开发逻辑模型是司空见惯的。

正常化

规范化数据模型是一种已消除任何数据冗余,并且所有数据都可通过主键和外键完全识别的数据模型。 虽然从性能角度看,规范化数据模型很少是最终目标,但规范化数据模型几乎总是模式的最佳初始表示,因为它能最大限度地减少冗余和模糊性。

关系理论定义了多个 "级别 "的规范化。 第三种规范化形式是公认的充分规范化数据模型的标准。

在第三种正常形式中,元组(行)中的每个属性(列)都只依赖于该元组的整个主键,而不依赖于任何其他属性或键。 我们有时会把它记成 "主键、整个主键,除了主键什么都没有"。例如,请看图 5-1 所示的数据。

图 5-1. 学生测试数据(去规范化) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Nanosatellites

Nanosatellites

Rogerio Atem de Carvalho, Jaime Estela, Martin Langer
Trino:权威指南,第二版

Trino:权威指南,第二版

Matt Fuller, Manfred Moser, Martin Traverso
软件工程基础

软件工程基础

Nathaniel Schutta, Dan Vega

Publisher Resources

ISBN: 9798341659186