Chapter 9

Data persistence

Learn how to use AI to improve database development and management efficiency, and master best practices such as AI-assisted database design, query optimization, and data migration.

Learn AI-driven data persistence using Sequential Thinking

AI applications in databases involve multiple aspects, usingStructured thinking methodsCan help you systematically master:

1
Database type overview
Quickly understand relational, non-relational, and distributed databases
2
AI-assisted database design
Data model generation, schema design, naming conventions
3
AI-assisted Query Optimization
Slow query analysis, SQL optimization, index recommendations
4
AI-assisted data management
Migration script generation, data validation, performance tuning
5
AI-driven database operations
Monitoring alerts, automatic tuning, fault diagnosis

Database type overview

Quickly understand three types of databases and build background knowledge for AI practice.

Relational database (SQL)

Representative:PostgreSQL、MySQL、SQLite

Features: ACID transactions, structured data, complex queries

Applicable: Transactions required, fixed data structure, complex queries

Non-relational databases (NoSQL)

Representative: MongoDB (document), Redis (key-value), Cassandra (columnar), Neo4j (graph)

Features: flexible mode, horizontal scaling, high-performance read/write

Applicable: semi-structured data, high-concurrency read/write, flexible schema

Distributed database

Representative:SelectDB(Apache Doris)、SAP HANA、ClickHouse、TiDB、Snowflake

Features: Supports distributed architecture, big data analysis, and vector search

Applicable: Big data analytics, real-time OLAP, AI/ML scenarios, vector search

Best practices for AI in databases

Use AI to improve database development and management efficiency, with AI-assisted practices across the entire process from design to operations.

AI-assisted database design

Generate data models with AI

Describe business requirements to AI, and it generates a Prisma Schema or TypeORM Entity:

Prompt template:

I need to design a database model for a blog system, including the following requirements:
1. User table: id, username, email, password hash, created time
2. Post table: id, title, content, author ID (foreign key), category ID (foreign key), created time, updated time
3. Category table: id, name, description
4. Tag table: id, name
5. Posts and tags have a many-to-many relationship

Please generate a Prisma Schema, including:
- Types and constraints for all fields
- Relationships between tables
- Necessary indexes
- Follow Prisma best practices

Example of AI-generated Prisma Schema

// prisma/schema.prisma
model User {
  id        String   @id @default(cuid())
  username  String   @unique
  email     String   @unique
  passwordHash String @map("password_hash")
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")
  
  posts     Post[]
  
  @@map("users")
}

model Post {
  id          String   @id @default(cuid())
  title       String
  content     String   @db.Text
  authorId    String   @map("author_id")
  categoryId  String   @map("category_id")
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")
  
  author      User     @relation(fields: [authorId], references: [id])
  category    Category @relation(fields: [categoryId], references: [id])
  tags        PostTag[]
  
  @@index([authorId])
  @@index([categoryId])
  @@map("posts")
}

model Category {
  id          String   @id @default(cuid())
  name        String   @unique
  description String?
  posts       Post[]
  
  @@map("categories")
}

model Tag {
  id    String    @id @default(cuid())
  name  String    @unique
  posts PostTag[]
  
  @@map("tags")
}

model PostTag {
  postId String @map("post_id")
  tagId  String @map("tag_id")
  
  post   Post   @relation(fields: [postId], references: [id])
  tag    Tag    @relation(fields: [tagId], references: [id])
  
  @@id([postId, tagId])
  @@map("post_tags")
}

AI-assisted naming conventions

Use AI to ensure naming consistency:

Prompt:

Check whether the naming of the following Prisma Schema follows best practices:
- Table names use plural form
- Field names use camelCase
- Foreign key fields use the table name + Id format
- Timestamp fields use createdAt/updatedAt

[Paste Schema code]

Please point out the parts that do not comply with the conventions and provide suggestions for changes.

Fundamentals of Data Model Design

Preserve core concepts and provide a foundation for AI practice. Using AI can quickly generate data models that follow best practices.

Table structure design

  • Standardization: reduce data redundancy and ensure consistency
  • Field type: Choose the appropriate data type
  • Naming conventions: table names and field names follow conventions

Relational design

  • 1:1: Users and user profiles
  • One-to-many: users and orders
  • Many-to-many: Users and roles (via an intermediary table)

Index fundamentals

  • Primary key index: Auto-created, unique and non-empty
  • Regular index: Accelerate queries
  • Composite index: Composite index of multiple fields

AI-assisted design example

Describe your business requirements to AI, and it can generate a complete data model, including table structures, relationships, indexes, and constraints. See the "AI-assisted database design" section above for a detailed example.

Data migration basics

Keep the core migration flow and focus on showing how AI helps migration. Using AI can quickly generate migration scripts and verify the results.

Migration tools

  • Prisma Migrate: Automatically generate migration scripts
  • TypeORM Migrations: Manually write the Migration class
  • Flyway/Liquibase: Java ecosystem migration tool

Migration strategy

  • Add field: Use default values or allow NULL
  • Delete field: first mark as deprecated, then delete
  • Change the field type: Data transformation may be required
  • Rename tables/fields: use a two-step method

AI-assisted migration

AI can generate migration scripts, verify migration results, and optimize migration performance. Refer to the "AI-assisted data migration" section above for detailed examples and Prompt templates.

Practical examples

Through real code examples, learn how to use AI to improve database development efficiency.

AI-generated data model

Complete Prisma Schema generation example (Prompt + result):

// 1. Provide requirements to AI
Prompt: "Design a database for an e-commerce system, including users, products, orders, and order items tables..."

// 2. Prisma Schema generated by AI
model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String?
  orders    Order[]
  createdAt DateTime @default(now())
}

model Product {
  id          String      @id @default(cuid())
  name        String
  price       Decimal     @db.Decimal(10, 2)
  description String?
  orderItems  OrderItem[]
  createdAt   DateTime    @default(now())
}

model Order {
  id         String     @id @default(cuid())
  userId     String
  user       User       @relation(fields: [userId], references: [id])
  items      OrderItem[]
  total      Decimal    @db.Decimal(10, 2)
  status     String     @default("pending")
  createdAt  DateTime   @default(now())
  
  @@index([userId])
}

model OrderItem {
  id        String   @id @default(cuid())
  orderId   String
  productId String
  order     Order    @relation(fields: [orderId], references: [id])
  product   Product  @relation(fields: [productId], references: [id])
  quantity  Int
  price     Decimal  @db.Decimal(10, 2)
  
  @@index([orderId])
  @@index([productId])
}

AI query optimization

Slow query analysis and optimization example (Prompt + before/after comparison):

Before optimization (2.3 seconds)
SELECT u.*, p.*, c.name
FROM users u
LEFT JOIN posts p ON u.id = p.author_id
LEFT JOIN categories c ON p.category_id = c.id
WHERE u.created_at > '2024-01-01'
ORDER BY u.created_at DESC
LIMIT 20;
After AI optimization (0.15 seconds)
-- AI suggestion: add an index and optimize JOIN
CREATE INDEX idx_users_created_at ON users(created_at DESC);

SELECT u.*, 
       (SELECT json_agg(p) FROM posts p WHERE p.author_id = u.id) as posts,
       c.name as category_name
FROM users u
LEFT JOIN categories c ON c.id = (
  SELECT category_id FROM posts 
  WHERE author_id = u.id 
  ORDER BY created_at DESC 
  LIMIT 1
)
WHERE u.created_at > '2024-01-01'
ORDER BY u.created_at DESC
LIMIT 20;

NL2SQL implementation

Complete natural language to SQL example:

// NL2SQL function implementation
async function nl2sql(question: string, schema: DatabaseSchema): Promise<string> {
  const prompt = `Convert the question to SQL based on the database schema:

Schema: ${JSON.stringify(schema)}
Question: ${question}

Return only SQL, using parameterized queries.`;

  const sql = await callLLM(prompt);
  return validateAndSanitizeSQL(sql);
}

// Usage example
const sql = await nl2sql(
  "Query all articles created in the last week and their author information",
  { tables: { users: [...], posts: [...] } }
);
// Returns: SELECT p.*, u.name as author_name 
//       FROM posts p 
//       JOIN users u ON p.author_id = u.id 
//       WHERE p.created_at > NOW() - INTERVAL '7 days'

Vector database integration

Example of vector search in RAG scenarios:

// Complete RAG flow
async function ragQuery(question: string) {
  // 1. Vectorize the question
  const questionEmbedding = await getEmbedding(question);
  
  // 2. Vector search (Pinecone)
  const results = await pineconeIndex.query({
    vector: questionEmbedding,
    topK: 5,
    includeMetadata: true
  });
  
  // 3. Build context
  const context = results.matches
    .map(m => m.metadata?.text)
    .join('\n\n');
  
  // 4. LLM generates an answer
  const answer = await generateAnswer(question, context);
  
  return answer;
}

// Usage
const answer = await ragQuery("What is Prisma?");
// AI generates accurate answers based on the retrieved documents

Learning outcomes

After completing this chapter, you will:

  • 1Understand three types of databases (relational, non-relational, distributed) and their applicable scenarios
  • 2Learn how to use AI-assisted database design methods (Schema generation, naming conventions, data type selection)
  • 3Can use AI to analyze and optimize slow queries, generating index recommendations and SQL optimization plans
  • 4Understand the principles of NL2SQL implementation and be able to build natural language query systems
  • 5Understand the application of vector databases in RAG scenarios and be able to integrate vector search capabilities
  • 6Can be used for AI-assisted database operations and maintenance (monitoring alerts, performance prediction, fault diagnosis)
  • 7Master AI-assisted data migration methods (generate migration scripts, verify migration results)