简体   繁体   中英

How to optimize SELECT some_field, max(primary_key) FROM table GROUP BY some_field

I have SQL query in SQL Azure:

SELECT some_field, max(primary_key) FROM table GROUP BY some_field

Table has currently over 6 million rows. Index on (some_field asc, primary_key desc) is created. primary_key field is incremental. There is about 700 distinct values of some_field. This select takes at least 30 seconds.

There are only inserts into this table, no updates or deletes.

I can create separate table to store some_field and maximal value of primary key and write trigger to build it, but I am looking for more elegant solution. Is there any?

Dont know if this will be performant but you you can give it a shot...

;WITH cte AS
(
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY some_field ORDER BY primary_key DESC) AS rn
   FROM table
)
SELECT *
FROM cte
WHERE rn = 1

Definitely do the secondary table of "somefield" and "highestPK" columns that is indexed on the "somefield" column. Build that once up front as a baseline and use that.

Then, whenever any new records are inserted into your 6 million record table, have a simple trigger to update your secondary table with something as simple as..

update SecondaryTable
   set highestPK = newlyInsertedPKID
   where somefield = newlyInsertedSomeFieldValue

This way, it stays updated with every insert as the highest PK for your "somefield" column will qualify, and if no update is available, insert into the secondary table with the new "somefield" value.

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