Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 1.96 KB

limit-db-query-results.md

File metadata and controls

54 lines (38 loc) · 1.96 KB

Should limit the number of returns for a SQL query (@creedengo/limit-db-query-results)

⚠️ This rule warns in the ✅ recommended config.

Why is this an issue?

SQL queries often involve processing large amounts of data, and fetching a large number of rows can consume significant CPU resources. By limiting the number of rows returned, you reduce the amount of processing that needs to be done by the database engine, which in turn lowers CPU consumption.

Transmitting a large number of rows over a network can be resource-intensive. By restricting the result set size, you reduce the amount of data that needs to be transferred between the database and the application, improving network efficiency.

If you store data about customers, you certainly don’t need to retrieve information of all at once, because the larger the table will be, the more elements the query will return.

// Non-compliant: Direct SQL query without LIMIT
const mysql = require("mysql2");
const connection = mysql.createConnection({ host: "localhost", user: "root" });

connection.query("SELECT * FROM users", (err, results) => {
  if (err) throw err;
  console.log(results);
});

It may therefore be a good idea to limit the results and use pagination, for example.

// Compliant: SQL query with LIMIT clause
const mysql = require("mysql2");
const connection = mysql.createConnection({ host: "localhost", user: "root" });

connection.query("SELECT * FROM users LIMIT 10", (err, results) => {
  if (err) throw err;
  console.log(results);
});

Resources

Documentation

Articles & blog posts