简体   繁体   中英

SQL Server - GROUP BY with HAVING on more than 10 million rows hitting performance

SQL Server-如何使用GROUP BY和超过1000万行的30 + OR条件的HAVING子句提高查询性能

We can be more helpful if you show us your query (you can obfuscate it if you need), but generically you can create computed, persisted, bit columns that pre-calculate the OR statements for you:

https://blog.sqlauthority.com/2016/04/27/sql-server-computed-column-conditions-case-statement/

Instead of:

HAVING [A] > 100000 OR [B] < 1000

Use:

ALTER TABLE [FOO]
ADD IsFiltered AS CASE WHEN [A] > 100000 OR [B] < 1000 THEN 1 ELSE 0 END PERSISTED

And then add [IsFiltered] to an index for extra speed. You can also create functions to perform calculations for you:

formula for computed column based on different table's column

Alternatively, it may be time to bite the bullet and create these calculations in an SSAS cube. That can be a big leap, but cubes can provide a lot of insight into your data.

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