热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

远程插入比本地插入慢x85,为什么以及如何改进?

:)我有一家第三方公司,它每秒对数据库执行200-300次插入,并且存在巨

:)

我有一家第三方公司,它每秒对数据库执行200-300次插入,并且存在巨大的滞后,我正在尝试监视造成滞后的原因。

我有带有TimeScaleDb 1.4的PostgreSQL 11

所以我知道通常的指示可能是磁盘IO,CPU和内存。

CPU,内存和磁盘IO可以轻松地从顶部进行监视。

top显示每个进程的内存和内存使用量,另外还有0.0 wa部分,该数字从零开始增加时,意味着正在等待硬盘的读写任务,这意味着磁盘IO可能是问题。

这家公司正在向我们的数据库中添加行时,我监视并注意到,这些都不是问题!所以我决定制作自己的压力测试脚本。

该公司表示,由于我有TimeScaleDB,并且由于此表上有很多索引,因此它延迟了要插入数据库的行。我以为他们错了,因为如果这是一个问题,我会看到磁盘IO负载问题。

所以我决定自己测试一下。我创建了自己的压力测试脚本,该脚本根据表的结构生成伪造的数据并相应地向其中添加行。

该脚本是用GoLang编写的,在这里我将展示其主要思想。这是我为此任务创建的goroutine函数:

func insertQuery(c <-chan *TestDataRow,d chan bool) {
for {
ret := <-c
if ret == nil {
log.Printf("broke!")
d <- true
break
}
fmt.Print(". ")
_,err = db.NamedExec(`insert into

([COLUMN1,COLUMN2,...])
values ([:VALUE_1,:VALUE_2,...]);`,*ret)
if err != nil {
log.Fatalf("could insert row to db: %v",err)
}
}
}

所以*TestDataRow包含需要插入数据库的字段。在主进程完成将所有行数据发送到go例程过程后,它将发送TestDataRow为nil,以使go例程知道队列中将不再有任何行。一旦go例程检测到空值,它将通过通道true发送d到主进程,让主进程知道它也完成了任务。

这是我的压力测试工具的主要功能:

func main() {

我读取配置文件,连接到数据库,并创建要使用的假数据

initConfig()
initPsql(viper.GetString("db.host"),viper.GetString("db.user"),viper.GetString("db.password"),viper.GetString("db.database"),viper.GetString("db.schema"),viper.GetInt("db.port"))
var rowsCount int64 = 10000
InitDummyData(rowsCount)

我解析-time-count标志,并计算将每一行插入数据库时​​应使用微秒延迟的时间

log.Printf("pg-cdr-stress-my-ass-off v%v\n",VERSION)
pTime := flag.String("time","","[required] to run this test for how long ? (example - 10s,1m5s,2h,1d2h)")
pCount := flag.String("count","[required] how many times ? (10000,10KB,2MB)")
pHelp := flag.Bool("help",false,"show usage")
flag.Parse()
log.Printf("help flag: %v",*pHelp)
if *pHelp {
flag.PrintDefaults()
} else if len(*pTime) == 0 || len(*pCount) == 0 {
log.Println("please provide all required parameters")
log.Println("usage:")
flag.PrintDefaults()
} else {
log.Printf("received parameters: time: %v count: %v\n",*pTime,*pCount)
d,err := time.ParseDuration(*pTime)
if err != nil {
log.Fatalf("could not parse time: %v",err)
}
milliSeconds := d.Milliseconds()
microSeconds := d.microseconds()
var count int
if parsedCount,err := strconv.Atoi(*pCount); err == nil {
count = parsedCount
} else {
n,err := bytesize.Parse(*pCount)
if err != nil {
log.Fatalf("could not parse count: %v",err)
}
count,err = strconv.Atoi(strings.TrimSuffix(n.Format("%.0f","bytes",false),"B"))
if err != nil {
log.Fatalf("could not parse count parameter property: %v",err)
}
}
log.Printf("will run %v times at %v milliseconds",count,milliSeconds)
executeEverymicroSeconds := microSeconds / int64(count)
log.Printf("will execute every %v microSeconds",executeEverymicroSeconds)
milDuration := time.microsecond * time.Duration(executeEverymicroSeconds)
if err != nil {
log.Fatalf("could not parse duration: %v",err)
}

当所有行都插入数据库时​​,我创建了具有100000队列的通道c来发送TestDataRow和通道closeC从go例程中返回,并执行了{{1} }常规行事

insertQuery

我开始一个for循环,等待一段时间的微秒延迟,然后再添加每个插入,在其中创建一个虚拟行,将其发送到go例程,递增 currentCount := 0
c := make(chan *TestDataRow,100000)
closeC := make(chan bool)
go insertQuery(c,closeC)
并中断循环,如果我添加了所需的行数

currentCount

然后我要向子例程go发送 currentNano := time.Now()
for range time.Tick(milDuration) {
c <- GenerateDummyRow()
currentCount++
fmt.Printf("%v ",currentCount)
if currentCount >= count {
break
}
}
log.Println("done looping,waiting for child..")
,让它知道没有更多行要添加,并等待nil的输出来知道子例程例程何时结束这是任务,然后我打印此任务花费的时间

closeC

我对这段代码没有问题。.我不希望对其进行改进,因为它仅用于本次测试,只是向您展示我在做什么,这样您就可以看到全部图片。

现在,我正在使用参数 c <- nil
<-closeC
log.Println("child is done!")
elpased := time.Since(currentNano)
log.Printf("task finished in %v seconds",float64(elpased.Milliseconds())/1000)
}
}
运行此应用程序,因此它将在1秒范围内添加500行,这意味着每次插入之间有2000微秒的延迟。

仅提供一条信息。当将行发送到go例程时,我将打印行号;在将行插入数据库中时,我将打印点-count 500 -time 1s。确保主应用在发送更多数据之前不等待go例程完成添加行。

因此,如果我远程执行此任务,这意味着我在使用postgresql连接到远程ubuntu服务器时在本地运行此脚本,则会得到以下输出:

.

在远程服务器上运行此脚本,这意味着它将在本地连接到数据库并以相同的参数运行相同的脚本

2019/11/28 15:48:55 will run 500 times at 1000 milliseconds
2019/11/28 15:48:55 will execute every 2000 microSeconds
1 . 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 . 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 . 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 . 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 . 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 . 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 2019/11/28 15:48:56 done looping,waiting for child..
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2019/11/28 15:50:20 broke!
2019/11/28 15:50:20 child is done!
2019/11/28 15:50:20 task finished in 85.309 seconds

正如您在此处看到的那样,远程插入行的速度是……慢了85倍?!为什么呢?

我也许与网络速度有关

然后我用Google搜索并在https://www.commandlinefu.com/commands/view/5799/test-network-speed-without-wasting-disk上显示有关如何检查网络速度的文章。所以我执行了它,然后执行以下操作:

019/11/28 13:58:49 will execute every 2000 microSeconds
1 . 2 3 4 . . 5 . 6 . 7 . 8 . 9 . 10 . . 11 . 12 . 13 . 14 . 15 . 16 . 17 . 18 . 19 . 20 . 21 . 22 . 23 . 24 . 25 . 26 . 27 . 28 . 29 . 30 . 31 . 32 . 33 . 34 . 35 . 36 . 37 . 38 . 39 . 40 41 42 . 43 . 44 . 45 . . 46 . 47 . 48 . 49 . 50 . 51 . . 52 . 53 . 54 . 55 . 56 . 57 . 58 . 59 . 60 . 61 . 62 . 63 . 64 . 65 . 66 . 67 . 68 . 69 . 70 . 71 . 72 . 73 . 74 . 75 . 76 . 77 . 78 . 79 . 80 . 81 . 82 . 83 . 84 . 85 . 86 . 87 . 88 . 89 . 90 . 91 . 92 . 93 . 94 . 95 . 96 . 97 . 98 . 99 . 100 . 101 . 102 . 103 . 104 . 105 . 106 . 107 . 108 . 109 . 110 . 111 . 112 . 113 . 114 . 115 . 116 . 117 . 118 . 119 . 120 . 121 . 122 . 123 . 124 . 125 . 126 . 127 . 128 .
129 . 130 . 131 . 132 . 133 . 134 . 135 . 136 . 137 . 138 . 139 . 140 . 141 . 142 . 143 . 144 . 145 . 146 . 147 . 148 . 149 . 150 . 151 . 152 . 153 . 154 . 155 . 156 . 157 . 158 . 159 . 160 . 161 . 162 . 163 . 164 . 165 . 166 . 167 . 168 . 169 . 170 . 171 . 172 . 173 . 174 . 175 . 176 . 177 . 178 . 179 . 180 . 181 . 182 . 183 .
184 . 185 . 186 . 187 . 188 . 189 . 190 . 191 . 192 . 193 . 194 . 195 . 196 . 197 . 198 . 199 . 200 . 201 . 202 . 203 . 204 . 205 . 206 . 207 . 208 . 209 . 210 . 211 . 212 . 213 . 214 . 215 . 216 . 217 . 218 . 219 . 220 . 221 . 222 . 223 . 224 . 225 . 226 . 227 . 228 . 229 . 230 . 231 . 232 . 233 . 234 . 235 . 236 . 237 . 238 .
239 . 240 . 241 . 242 . 243 . 244 . 245 . 246 . 247 . 248 . 249 . 250 . 251 . 252 . 253 . 254 . 255 . 256 . 257 . 258 . 259 . 260 . 261 . 262 . 263 . 264 . 265 . 266 . 267 . 268 . 269 . 270 . 271 . 272 . 273 . 274 . 275 . 276 . 277 . 278 . 279 . 280 . 281 . 282 . 283 . 284 . 285 . 286 . 287 . 288 . 289 . 290 . 291 . 292 . 293 .
294 . 295 . 296 . 297 . 298 . 299 . 300 . 301 . 302 . 303 . 304 . 305 . 306 . 307 . 308 . 309 . 310 . 311 . 312 . 313 . 314 . 315 . 316 . 317 . 318 . 319 . 320 . 321 . 322 . 323 . 324 . 325 . 326 . 327 . 328 . 329 . 330 . 331 . 332 . 333 . 334 . 335 . 336 . 337 . 338 . 339 . 340 . 341 . 342 . 343 . 344 . 345 . 346 . 347 . 348 .
349 . 350 . 351 . 352 . 353 . 354 . 355 . 356 . 357 . 358 . 359 . 360 . 361 . 362 . 363 . 364 . 365 . 366 . 367 . 368 . 369 . 370 . 371 . 372 . 373 . 374 . 375 . 376 . 377 . 378 . 379 . 380 . 381 . 382 . 383 . 384 . 385 . 386 . 387 . 388 . 389 . 390 . 391 . 392 . 393 . 394 . 395 . 396 . 397 . 398 . 399 . 400 . 401 . 402 . 403 .
404 . 405 . 406 . 407 . 408 . 409 . 410 . 411 . 412 . 413 . 414 . 415 . 416 . 417 . 418 . 419 . 420 . 421 . 422 . 423 . 424 . 425 . 426 . 427 . 428 . 429 . 430 . 431 . 432 . 433 . 434 . 435 . 436 . 437 . 438 . 439 . 440 . 441 . 442 . 443 . 444 . 445 . 446 . 447 . 448 . 449 . 450 . 451 . 452 . 453 . 454 . 455 . 456 . 457 . 458 .
459 . 460 . 461 . 462 . 463 . 464 . 465 . 466 . 467 . 468 . 469 . 470 . 471 . 472 . 473 . 474 . 475 . 476 . 477 . 478 . 479 . 480 . 481 . 482 . 483 . 484 . 485 . 486 . 487 . 488 . 489 . 490 . 491 . 492 . 493 . 494 . 495 . 496 . 497 . 498 . 499 . 500 2019/11/28 13:58:50 done looping,waiting for child..
. 2019/11/28 13:58:50 broke!
2019/11/28 13:58:50 child is done!
2019/11/28 13:58:50 task finished in 1.001 seconds

所以网络不是问题。

我想念什么?

有关此问题的任何信息将不胜感激。


我不会讲Go,但是如果它是通过网络发送的很多小语句而不是本地的很多小语句,那么问题就在于网络延迟(而不是带宽)。

您应该使用多行插入

INSERT INTO ... VALUES (...),(...),(...)

COPY


推荐阅读
author-avatar
G路过的彩虹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有