简体   繁体   中英

How is it better to handle time-consuming / CPU intensive queries?

Suppose the situation that for example we have an instance of some SQL Server (it is not the case what it is). And we have a Java applications that is using the Spring stack.

There are queries that are already optimized but they are still slow due to complex logic of aggregating that data.

I have several approaches in mind (those are short-terms for now):

  1. Proceed with tuning (like creating views) and implement jobs to recalculate these data right in the SQL server for example every 5 minutes and store it in separate table. (Yes it is not so good solution but still).
  2. Implement some kind of mechanism to count / aggregate that data in background. Probably implement one part of Lambda-architecture . I've already looked at Apache Spark and others.

Under optimized it means that those queries are using the correct indexes and everything is 'tuned'.

I know that this is not kind of question as more proposals / discussions. But still I'm questioned.

What is the better way to handle situation like this based on the above?


UPDATE #1

Based on What you can and can't do with Indexed views for MS SQL Server the Indexed view are not the way to go as they do not support COUNT, MIN, MAX, TOP, outer joins, or a few other keywords or elements. You can't modify the underlying tables and columns. The view is created with the WITH SCHEMABINDING option.

UPDATE #2

After spending some time on this. I've stopped with Materialized Views for now in sake of simplicity.

So, different database engines have the concept of a Materialized View . SQL server has the equivalent with it's Indexed Views . These are designed for your exact use case. I would strongly consider these methods before basically "rolling your own" materialized view.

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