简体   繁体   中英

Optimizing database queries

My question is when should we worry about how much data do we ask for a given task from sql database?

Lets say I have PostgreSql database. I have a product table and product has 20 fields.

In a the system some places actually only need product id, name and price. Some people argue, that I should only ask those 3 fields from the database to be efficient. But I feel It's much easier for developers to always do productService.getProduct(id) and then pick the fields they need, than to make a separate class or query for those specific fields.

Does it really matter - for speed of the query - if I ask 3 or 20 fields? How much load could it increase?

(I'm java developer with the mindset of "Early optimization is the root of all evil".)

As with all optimization, when it matters. The profiler and other tools (for example postgres' EXPLAIN ANALYZE ) will let you know.

The actual mechanisms depend on lots of things; the database being used, table/schema, tablespace settings etc. etc. so it's impossible to give any definite answer, but since the amount of data being moved is different, it will naturally make a difference whether you're moving 100,000 rows of 10 columns or 100,000 rows of 3 columns.

The actual query may not see a significant difference if the same amount of pages is being read from disk, but the memory and network use will naturally differ.

Thankfully you can refactor code and queries to select less data if the original query becomes a bottleneck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM