query efficiency - select the 2 latest “group/batch” records from table

落爺英雄遲暮 提交于 2019-12-12 01:25:42

问题


We have a tested a quite interesting SQL query. Unfortunately, It turned out that this query runs a little bit slow - O(n2) - and we are looking for a optimized solution or maybe also a totally different one?

Goal:

We would like to get for:
 - some customers ("record_customer_id"), e.g. ID 5
     - the latest 2 "record_init_proc_id" 
       - for every "record_inventory_id"

http://www.sqlfiddle.com/#!9/07e5d/4

The query works fine and shows the correct results but uses at least two full table scans which is of course horrible when having too many rows to scan.

Is it possible to (a) select first all record_customer_id(s) in a specific date range, saving the results of that query and then (b) run the working query in SQL fiddle over these saved results?

Or is there maybe a totally different approach out there which has a great performance?

Any help is greatly appreciated!


回答1:


I give a generic O(N) solution in my Groupwise-Max blog



来源:https://stackoverflow.com/questions/30343270/query-efficiency-select-the-2-latest-group-batch-records-from-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!