评估
Prisma Accelerate 通过高级连接池和全局边缘缓存优化数据库交互。其连接池在 16 个区域可用,并帮助应用程序根据需求负载均衡和扩展数据库请求。
考虑到以上信息,我们建议您使用高容量评估 Accelerate 以观察其在负载下的性能。
Accelerate 的连接池如何在负载下优化性能
Prisma Accelerate 采用动态的无服务器连接池基础设施。当发出请求时,将在配置 Prisma Accelerate 时分配的区域中为项目快速配置连接池。此连接池保持活动状态,为许多其他请求提供服务,同时重用已建立的数据库连接。连接池将在一段时间不活动后断开连接,因此重要的是使用一致的流量流评估 Prisma Accelerate。
主要优势
-
优化的查询性能:无服务器连接池器会适应查询负载,确保在高峰需求期间高效管理数据库连接。
Prisma Accelerate 的连接池器无法提高数据库中查询的性能。在查询性能成为问题的情况下,我们建议优化 Prisma 查询,应用索引或利用 Accelerate 的边缘缓存。
-
最大化连接重用:执行一致数量的查询有助于维护 Accelerate 连接池器的活动实例。这会增加连接重用,确保后续查询的响应时间更快。
通过了解和利用这种机制,您可以确保您的数据库查询在规模上始终如一地高效执行。
评估 Prisma Accelerate 连接池性能
以下是一些使用示例模型评估 Prisma Accelerate 的示例。
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 缓存性能
Prisma Accelerate 的边缘缓存也针对大量查询进行了优化。缓存会自动针对重复查询进行优化。因此,随着查询频率的增加,缓存命中率也会增加。将查询结果添加到缓存也是非阻塞的,因此短时间内的大量查询可能不会使用缓存或持续负载。
要评估 Accelerate 的边缘缓存,您可以使用以下内容修改上述脚本
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)
})