Skip to main content

评估

Prisma Accelerate 通过高级连接池和全局边缘缓存优化数据库交互。它的连接池在 16 个区域可用,可帮助应用根据需求进行负载均衡和数据库请求扩展。

¥Prisma Accelerate optimizes database interactions through advanced connection pooling and global edge caching. Its connection pooler is available in 16 regions and helps applications load-balance and scale database requests based on demand.

考虑到以上信息,我们建议在高流量下评估 Accelerate,以了解其在负载下的性能。

¥Considering the information above, we recommend evaluating Accelerate with high volume to see it perform under load.

Accelerate 的连接池如何在负载下优化性能

¥How Accelerate's connection pool optimizes performance under load

Prisma Accelerate 采用动态、无服务器连接池基础架构。触发请求时,会在配置 Prisma Accelerate 时指定的区域中快速为项目配置连接池。此连接池保持活动状态,在重用已建立的数据库连接的同时,为许多其他请求提供服务。连接池会在一段时间不活动后断开连接,因此务必使用稳定的流量来评估 Prisma Accelerate。

¥Prisma Accelerate employs a dynamic, serverless connection pooling infrastructure. When a request is made, a connection pool is quickly provisioned for the project in the region assigned while configuring Prisma Accelerate. This connection pool remains active, serving many additional requests while reusing established database connections. The connection pool will disconnect after a period of inactivity, so it’s important to evaluate Prisma Accelerate with a consistent stream of traffic.

主要优势:

¥Key Benefits:

  • 优化的查询性能:无服务器连接池会根据查询负载进行自适应调整,确保在高峰需求期间高效管理数据库连接。

    ¥Optimized Query Performance: The serverless connection pooler adapts to the query load, ensuring the database connections are managed efficiently during peak demand.

    Prisma Accelerate 的连接池无法提高数据库查询的性能。在查询性能存在问题的情况下,我们建议优化 Prisma 查询、应用索引或利用 Accelerate 的边缘缓存。

    ¥Prisma Accelerate’s connection pooler cannot improve the performance of queries in the database. In scenarios where query performance is an issue, we recommend optimizing the Prisma query, applying indexes, or utilizing Accelerate’s edge caching.

  • 最大化连接重用:执行一致数量的查询有助于维护 Accelerate 连接池的活动实例。这增加了连接重用,确保后续查询的响应时间更快。

    ¥Maximize Connection Reuse: Executing a consistent volume of queries helps maintain active instances of Accelerate connection poolers. This increases connection reuse, ensuring faster response times for subsequent queries.

通过理解和利用此机制,你可以确保数据库查询在大规模环境中始终如一地高效执行。

¥By understanding and harnessing this mechanism, you can ensure that your database queries perform consistently and efficiently at scale.

评估 Prisma Accelerate 连接池性能

¥Evaluating Prisma Accelerate connection pooling performance

以下是一个使用示例模型评估 Prisma Accelerate 的示例:

¥Below you will find an example of how to evaluate Prisma Accelerate using a sample model:

model Notes {
id Int @id @default(autoincrement())
title String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
}
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient().$extends(withAccelerate())

function calculateStatistics(numbers: number[]): {
average: number
p50: number
p75: number
p99: number
} {
if (numbers.length === 0) {
throw new Error('The input array is empty.')
}

// Sort the array in ascending order
numbers.sort((a, b) => a - b)

const sum = numbers.reduce((acc, num) => acc + num, 0)
const count = numbers.length

const average = sum / count
const p50 = getPercentile(numbers, 50)
const p75 = getPercentile(numbers, 75)
const p99 = getPercentile(numbers, 99)

return { average, p50, p75, p99 }
}

function getPercentile(numbers: number[], percentile: number): number {
if (percentile <= 0 || percentile >= 100) {
throw new Error('Percentile must be between 0 and 100.')
}

const index = (percentile / 100) * (numbers.length - 1)
if (Number.isInteger(index)) {
// If the index is an integer, return the corresponding value
return numbers[index]
} else {
// If the index is not an integer, interpolate between two adjacent values
const lowerIndex = Math.floor(index)
const upperIndex = Math.ceil(index)
const lowerValue = numbers[lowerIndex]
const upperValue = numbers[upperIndex]
const interpolationFactor = index - lowerIndex
return lowerValue + (upperValue - lowerValue) * interpolationFactor
}
}

async function main() {
const timings = []

// fire a query before going to the loop
await prisma.notes.findMany({
take: 20,
})

// we recommend evaluationg Prisma Accelerate with a large loop
const LOOP_LENGTH = 10000

for (let i = 0; i < LOOP_LENGTH; i++) {
const start = Date.now()
await prisma.notes.findMany({
take: 20,
})

timings.push(Date.now() - start)
}

const statistics = calculateStatistics(timings)
console.log('Average:', statistics.average)
console.log('P50:', statistics.p50)
console.log('P75:', statistics.p75)
console.log('P99:', statistics.p99)
}

main()
.then(async () => {
await prisma.$disconnect()
})
.catch((e) => {
await prisma.$disconnect()
process.exit(1)
})

评估 Prisma Accelerate 缓存性能

¥Evaluating Prisma Accelerate caching performance

Prisma Accelerate 的边缘缓存也针对大量查询进行了优化。缓存会自动针对重复查询进行优化。因此,缓存命中率将随着查询频率的增加而增加。将查询结果添加到缓存也是非阻塞的,因此短时间的查询可能不会利用缓存或持续的负载。

¥Prisma Accelerate’s edge cache is also optimized for a high volume of queries. The cache automatically optimizes for repeated queries. As a result, the cache hit rate will increase as the query frequency does. Adding a query result to the cache is also non-blocking, so a short burst of queries might not utilize the cache or a sustained load.

为评估 Accelerate 的边缘缓存,你可以使用以下命令修改上述脚本:

¥To evaluate Accelerate’s edge caching, you can modify the above script with the below:

import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient().$extends(withAccelerate())

function calculateStatistics(numbers: number[]): {
average: number
p50: number
p75: number
p99: number
} {
if (numbers.length === 0) {
throw new Error('The input array is empty.')
}

// Sort the array in ascending order
numbers.sort((a, b) => a - b)

const sum = numbers.reduce((acc, num) => acc + num, 0)
const count = numbers.length

const average = sum / count
const p50 = getPercentile(numbers, 50)
const p75 = getPercentile(numbers, 75)
const p99 = getPercentile(numbers, 99)

return { average, p50, p75, p99 }
}

function getPercentile(numbers: number[], percentile: number): number {
if (percentile <= 0 || percentile >= 100) {
throw new Error('Percentile must be between 0 and 100.')
}

const index = (percentile / 100) * (numbers.length - 1)
if (Number.isInteger(index)) {
// If the index is an integer, return the corresponding value
return numbers[index]
} else {
// If the index is not an integer, interpolate between two adjacent values
const lowerIndex = Math.floor(index)
const upperIndex = Math.ceil(index)
const lowerValue = numbers[lowerIndex]
const upperValue = numbers[upperIndex]
const interpolationFactor = index - lowerIndex
return lowerValue + (upperValue - lowerValue) * interpolationFactor
}
}

async function main() {
const timings = []

// fire a query before going to the loop
await prisma.notes.findMany({
take: 20,
cacheStrategy: {
ttl: 30,
},
})

// we recommend evaluating Prisma Accelerate with a large loop
const LOOP_LENGTH = 10000

for (let i = 0; i < LOOP_LENGTH; i++) {
const start = Date.now()
await prisma.notes.findMany({
take: 20,
cacheStrategy: {
ttl: 30,
},
})

timings.push(Date.now() - start)
}

const statistics = calculateStatistics(timings)
console.log('Average:', statistics.average)
console.log('P50:', statistics.p50)
console.log('P75:', statistics.p75)
console.log('P99:', statistics.p99)
}

main()
.then(async () => {
await prisma.$disconnect()
})
.catch((e) => {
await prisma.$disconnect()
process.exit(1)
})