jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

sqlx query a lot of data cause tokio bad task. #899

Closed yuyang-ok closed 1 year ago

yuyang-ok commented 1 year ago
use std::time::Duration;

use bb8_postgres::tokio_postgres::{self, NoTls};
use sqlx::Connection;

#[tokio::main]

async fn main() {
    // for _ in 0..10 {
    //     println!("sqlx used:{:?}", sqlx_time().await.unwrap());
    //     println!("bb8 used:{:?}", bb8_time().await.unwrap());
    // }
    // tokio::spawn(async {
    //     println!("sqlx used:{:?}", sqlx_time().await.unwrap());
    // });
    tokio::spawn(async {
        println!("sqlx used:{:?}", sqlx_time().await.unwrap());
    });
    let now = std::time::Instant::now();
    loop {
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
        println!("print from main:{:?}", now.elapsed());
    }
}

const Q: &str = "select * from orders";

async fn sqlx_time() -> anyhow::Result<Duration> {
    let mut opts = sqlx::postgres::PgConnectOptions::new();
    opts = opts
        .host("192.168.0.227")
        .username("postgres")
        .password("zalando")
        .database("citus")
        .port(19000);

    let mut client = sqlx::postgres::PgConnection::connect_with(&opts).await?;
    let now = std::time::Instant::now();

    let _: Vec<_> = sqlx::query(Q).fetch_all(&mut client).await?;

    Ok(now.elapsed())
}
print from main:5.869607887s
print from main:8.865092778s
print from main:14.790433884s
print from main:15.791615327s
print from main:16.799191212s
print from main:20.282701128s
print from main:24.261279304s
print from main:36.98605543s
print from main:38.656715161s
print from main:42.124719785s
print from main:43.537019533s
print from main:44.54424445s
sqlx used:44.795342161s
print from main:45.548273104s
print from main:46.565033701s
print from main:47.57896779s

I use tokio-console I get this.

connection: http://127.0.0.1:6669/ (CONNECTED)
views: t = tasks, r = resources
controls: return to task list = ⎋ esc, toggle pause = space, quit = q              
╭Warnings───────────────────────────────────────────────────────────────────────────────────────────────────╮
│⚠ This task has woken itself for more than 50% of its total wakeups (99%)                                  │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭Task────────────────────────────────────────────────╮╭Waker────────────────────────────────────────────────╮
│ID: 10 ▶                                            ││Current wakers: 0 (clones: 2530933, drops: 2530933)  │
│Target: tokio::task                                 ││Woken: 2530933 times, last woken: 20.371µs ago       │
│Location: src/main.rs:19:5                          ││Self Wakes: 2530928 times (99%)                      │
│Total Time: 30.00s                                  ││                                                     │
│Busy: 29.98s (99.92%)                               ││                                                     │
│Scheduled: 283.75µs (0.00%)                         ││                                                     │
│Idle: 22.42ms (0.07%)                               ││                                                     │
│                                                    ││                                                     │
╰────────────────────────────────────────────────────╯╰─────────────────────────────────────────────────────╯
╭Poll Times Percentiles─╮╭Poll Times Histogram──────────────────────────────────────────────────────────────╮
│p10: 137.22µs          ││1█   █           █              █                                              █  │
│p25: 149.50µs          ││ 136.19µs                                                                385.02µs │
╰───────────────────────╯╰──────────────────────────────────────────────────────────────────────────────────
mohammed90 commented 1 year ago

Wrong sqlx. This repo is for a Go project, not Rust,

yuyang-ok commented 1 year ago

sorry