Performance: Why database connectivity matters
Database connectivity affects server response time and overall user experience by controlling how fast data is retrieved and sent to the frontend.
import { Pool } from 'pg'; const pool = new Pool(); export async function handler(req, res) { const result = await pool.query('SELECT * FROM users'); res.json(result.rows); }
import { Client } from 'pg'; export async function handler(req, res) { const client = new Client(); await client.connect(); const result = await client.query('SELECT * FROM users'); await client.end(); res.json(result.rows); }
| Pattern | Connection Overhead | Server Response Time | Resource Usage | Verdict |
|---|---|---|---|---|
| New connection per request | High (connect + disconnect each time) | Slow (adds 50-200ms delay) | High CPU and memory usage | [X] Bad |
| Connection pooling | Low (reuse existing connections) | Fast (minimal delay) | Efficient resource use | [OK] Good |
const mysql = require('mysql');
const connection = mysql.createConnection({host: 'localhost', user: 'root', password: 'wrong'});
connection.connect(err => {
if (err) {
console.log('Connection error');
} else {
console.log('Connected');
}
});const mysql = require('mysql');
const connection = mysql.createConnection({host: 'localhost', user: 'root', password: 'pass'});
connection.connect();
connection.query('SELECT * FROM users', (err, results) => {
if (err) throw err;
console.log(results);
});
connection.end();Pass the test and this pattern moves unknown