mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00
Use the pool to insert quickly
This commit is contained in:
parent
9f8c510d36
commit
d0b9bda74f
@ -10,6 +10,8 @@ use fake::{
|
|||||||
Dummy, Fake, Faker,
|
Dummy, Fake, Faker,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use futures::future;
|
||||||
|
use futures::channel::oneshot::channel;
|
||||||
use futures::stream::TryStreamExt;
|
use futures::stream::TryStreamExt;
|
||||||
use std::io;
|
use std::io;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
@ -42,11 +44,11 @@ struct Contact {
|
|||||||
async fn main() -> Fallible<()> {
|
async fn main() -> Fallible<()> {
|
||||||
env_logger::try_init()?;
|
env_logger::try_init()?;
|
||||||
|
|
||||||
let pool = PgPool::new("postgres://postgres@127.0.0.1/sqlx__dev", 1);
|
let pool = PgPool::new("postgres://postgres@127.0.0.1/sqlx__dev", 85);
|
||||||
|
|
||||||
ensure_schema(&pool).await?;
|
ensure_schema(&pool).await?;
|
||||||
insert(&pool, 500).await?;
|
insert(&pool, 50_000).await?;
|
||||||
select(&pool, 50_000).await?;
|
// select(&pool, 50_000).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -77,30 +79,39 @@ CREATE TABLE IF NOT EXISTS contacts (
|
|||||||
|
|
||||||
async fn insert(pool: &PgPool, count: usize) -> io::Result<()> {
|
async fn insert(pool: &PgPool, count: usize) -> io::Result<()> {
|
||||||
let start_at = Instant::now();
|
let start_at = Instant::now();
|
||||||
|
let mut handles = vec![];
|
||||||
|
|
||||||
for _ in 0..count {
|
for _ in 0..count {
|
||||||
let pool = pool.clone();
|
let pool = pool.clone();
|
||||||
let contact: Contact = Faker.fake();
|
let contact: Contact = Faker.fake();
|
||||||
|
let (tx, rx) = channel::<()>();
|
||||||
|
|
||||||
sqlx::query::<PgQuery>(
|
tokio::spawn(async move {
|
||||||
r#"
|
sqlx::query::<PgQuery>(
|
||||||
INSERT INTO contacts (name, username, password, email, phone)
|
r#"
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
INSERT INTO contacts (name, username, password, email, phone)
|
||||||
"#,
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
)
|
"#,
|
||||||
.bind(contact.name)
|
)
|
||||||
.bind(contact.username)
|
.bind(contact.name)
|
||||||
.bind(contact.password)
|
.bind(contact.username)
|
||||||
.bind(contact.email)
|
.bind(contact.password)
|
||||||
.bind(contact.phone)
|
.bind(contact.email)
|
||||||
.execute(&pool)
|
.bind(contact.phone)
|
||||||
.await?;
|
.execute(&pool)
|
||||||
|
.await.unwrap();
|
||||||
|
|
||||||
|
tx.send(()).unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
handles.push(rx);
|
||||||
}
|
}
|
||||||
|
|
||||||
let elapsed = start_at.elapsed();
|
future::join_all(handles).await;
|
||||||
let per = Duration::from_nanos((elapsed.as_nanos() / (count as u128)) as u64);
|
|
||||||
|
|
||||||
println!("insert {} rows in {:?} [ 1 in ~{:?} ]", count, elapsed, per);
|
let elapsed = start_at.elapsed();
|
||||||
|
|
||||||
|
println!("insert {} rows in {:?}", count, elapsed);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user