query = "SELECT * FROM customers" # Noncompliant
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 all their information at once, because the larger the table is, the more elements the query will return.
query = "SELECT * FROM customers" # Noncompliant
It may therefore be a good idea to limit the results and use pagination, for example.
query = "SELECT id, name, email FROM customers LIMIT 10" # Compliant
MySQL Reference Manual - LIMIT Query Optimization
PostgreSQL: Documentation - LIMIT and OFFSET