Making the ranking query efficient

独自空忆成欢 提交于 2019-12-24 11:37:41

问题


I am using the following code in ACCESS 2010 to rank ~30000 rows from a table. However it takes around 15 minutes to do the ranking (I have to do this for 5 more columns and it could easily take more than an hour). I need these columns for further processing and hence tried creating a new table. I have also tried appending and updating to existing tables. Everything takes the same time. Am I missing something obvious here that could make it work faster?

SELECT MasterTable.Sales, (SELECT Count(*)+1 as HowMany From MasterTable AS Dupe WHERE Dupe.Sales > MasterTable.Sales) AS SalesRank INTO tableRank FROM MasterTable;

Details on the MasterTable: 11 columns. 1 primary key (Text). 5 parameters (Sales SalesQty Profit Hits Cost, all numeric). Ranks of 5 parameters(like SalesRank and so on) 30,000 rows


回答1:


Did you try a left join with grouping?

SELECT
    m1.Sales,
    COUNT(*) AS HowMany
FROM
    MasterTable AS m1
    LEFT JOIN MasterTable m2 ON m1.Sales <= m2.Sales
GROUP BY
    m1.Sales


来源:https://stackoverflow.com/questions/30848800/making-the-ranking-query-efficient

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