简体   繁体   中英

What is the best practice to use aggregation functions with views

What is the best practice to use aggregation function with a view in sql query with a huge data

select name , sum(value) from v_transactions Group by name

this view have 150,000 Record

If the view is presenting 150k rows, but take 5 minutes to aggregate them, the problem is the performance of the view not the aggregation that you're doing.

  • SELECT * FROM v_transactions will probably take almost exactly the same amount of time


It is possible that the view was only designed to be queried for a small number of rows

  • FROM v_transaction WHERE some_key = some_value

In which case, trying to query the view for all of its rows may be "misusing" it.


It is possible that the view is doing work unrelated to your query

  • Complicated calculations and joins to generate other fields

Again, if the view exists specifically to generate fields that you're not using, your use of the view may be considered "misuse".


In any case, as it's a view there will be an alternative source to aggregate from. The could be other views or tables that v_transactions derives it's data from.

Ask around, find out what views, tables and functions you have access to. You may or may not have access to what you want , but until you investigate you're not going to find out.


Another possibility is that the name field is the expensive calculation. Is there another field you can group by instead? Some id or key that corresponds to the name? This would give you several options to test for performance...

  • SELECT some_id, SUM(value) FROM v_transactions GROUP BY some_id
  • SELECT some_id, name, SUM(value) FROM v_transactions GROUP BY some_id, name
  • SELECT some_id, MAX(name), SUM(value) FROM v_transactions GROUP BY some_id
  • etc, etc

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