1 year ago
#224254
flicker0ne
Why java's mariadb connector connects to mariadb faster than connectors in other languages?
I'm learning rust, and recently I'm trying to use the Sqlx library to connect to the database. Whether I use a pool or not, the connection speed is always at least 2s; however, when I use the Java Connector (mariadb-java-client package provided by mariadb.com) to connect to mariadb It takes less than 1 second and I don't quite understand why this is happening. I have used MysqlConnector of C# before, and it takes at least 2s to connect to mariadb. In the end, I also tried C Connector, C++ Connector and ODBC Connector, which are also slower than Java Connector.
kotlin sample code(using mariadb-java-client package):
fun main() {
var timeStart = LocalTime.now().toNanoOfDay()
Class.forName(dbClsName)
var timeUsed = LocalTime.now().toNanoOfDay() - timeStart
println("load class took time = $timeUsed ns")
val p = Properties()
p["user"] = userName
p["password"] = password
timeStart = LocalTime.now().toNanoOfDay()
val conn = DriverManager.getConnection(connStr, p)
timeUsed = LocalTime.now().toNanoOfDay() - timeStart
val secs = timeUsed.toDouble() / 1e9
println("connect successed! took time = $timeUsed ns, almost $secs s")
timeStart = LocalTime.now().toNanoOfDay()
val stmt = conn.createStatement()
val rst = stmt.executeQuery("SHOW DATABASES;")
timeUsed = LocalTime.now().toNanoOfDay() - timeStart
println("query took time = $timeUsed ns")
var i = 0
while (rst.next()){
i++
println("result${i}:\n\t${rst.getString(1)}")
}
conn.close()
}
kotlin code result:
load class took time = 15631300 ns
connect successed! took time = 101765000 ns, almost 0.101765 s
query took time = 0 ns
...
Rust sample code(using sqlx):
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let mut time_start = chrono::Local::now();
let mut db_conn = MySqlConnection::connect(CONN_STR).await?;
let duration_conn = chrono::Local::now() - time_start;
println!(
"connect db took time = {}ns, almost {}s",
duration_conn.num_nanoseconds().unwrap_or_default(),
duration_conn.num_seconds()
);
time_start = chrono::Local::now();
let results = sqlx::query("SHOW DATABASES")
.map(|row: MySqlRow| row.get::<String, usize>(0))
.fetch_all(&mut db_conn)
.await?;
let query_duration = chrono::Local::now() - time_start;
println!(
"query db took time = {}ns, almost {}s",
query_duration.num_nanoseconds().unwrap_or_default(),
query_duration.num_seconds()
);
for row_str in results {
println!("{}", row_str)
}
Ok(())
}
Rust sample result:
connect db took time = 2045829800ns, almost 2s
query db took time = 1324300ns, almost 0s
...
C# sample code(using MysqlConnector):
public static void Main() {
var time_start = DateTime.UtcNow;
using var conn = new MySqlConnection(connStr);
var time_takes = DateTime.UtcNow - time_start;
Console.WriteLine($"consturct connection finished; took time = {time_takes.Ticks}ticks,
almost {(long)time_takes.TotalSeconds}s");
time_start = DateTime.UtcNow;
conn.Open();
time_takes = DateTime.UtcNow - time_start;
Console.WriteLine($"open mysql connection finished; take time = {time_takes.Ticks}ticks,
almost {(long)time_takes.TotalSeconds}s");
using var query_command = new MySqlCommand("SHOW DATABASES;", conn);
using var reader_dbs = query_command.ExecuteReader();
while (reader_dbs.Read())
{
Console.WriteLine(reader_dbs.GetString(0));
}
}
C# sample result:
consturct connection finished; took time = 1597ticks, almost 0s
open mysql connection finished; take time = 22826302ticks, almost 2s
...
In addition, please ignore that I didn't measure the elapsed time with a benchmarking library, because the difference of the connection time between the Java connector and other connectors is visible to the naked eye.
jdbc
mariadb
mysql-connector
connector
mariadb-connector-c
0 Answers
Your Answer