How can I get running totals of recent rows faster?sql server partitioned view execution plan questionsDoes...
Where would I need my direct neural interface to be implanted?
How to stretch the corners of this image so that it looks like a perfect rectangle?
Rotate ASCII Art by 45 Degrees
Can a virus destroy the BIOS of a modern computer?
How dangerous is XSS
How to enclose theorems and definition in rectangles?
What do you call someone who asks many questions?
Is it possible to create a QR code using text?
How do conventional missiles fly?
What exactly is ineptocracy?
Can compressed videos be decoded back to their uncompresed original format?
How do I exit BASH while loop using modulus operator?
Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?
Notepad++ delete until colon for every line with replace all
How to show a landlord what we have in savings?
Unlock My Phone! February 2018
What Exploit Are These User Agents Trying to Use?
How badly should I try to prevent a user from XSSing themselves?
In Bayesian inference, why are some terms dropped from the posterior predictive?
Avoiding the "not like other girls" trope?
Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?
How can saying a song's name be a copyright violation?
How can I prove that a state of equilibrium is unstable?
Send out email when Apex Queueable fails and test it
How can I get running totals of recent rows faster?
sql server partitioned view execution plan questionsDoes MERGE use tempdb?Which of these queries is best for performance?Every query plan statistic says my query should be faster, but it is notWindow function vs group by method: find value for MAX IDMulti-statement TVF vs Inline TVF PerformanceHelpful nonclustered index improved the query but raised logical readsAggregation in Outer Apply vs Left Join vs Derived tablesqlpackage.exe SELECT statement causing massive readsHigh processor utilization when running a stored procedure
I'm currently designing a transaction table. I realized that calculating running totals for each row will be needed and this might be slow in performance. So I created a table with 1 million rows for testing purposes.
CREATE TABLE [dbo].[Table_1](
[seq] [int] IDENTITY(1,1) NOT NULL,
[value] [bigint] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[seq] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And I tried to get 10 recent rows and its running totals, but it took about 10 seconds.
--1st attempt
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq) total
FROM Table_1
ORDER BY seq DESC
--(10 rows affected)
--Table 'Worktable'. Scan count 1000001, logical reads 8461526, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Table_1'. Scan count 1, logical reads 2608, physical reads 516, read-ahead reads 2617, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 8483 ms, elapsed time = 9786 ms.
I suspected TOP
for the reason of slow performance from the plan, so I changed query like this, and it took about 1~2 seconds. But I think this is still slow for production and wondering if this can be improved further.
--2nd attempt
SELECT *
,(
SELECT SUM(value)
FROM Table_1
WHERE seq <= t.seq
) total
FROM (
SELECT TOP 10 seq
,value
FROM Table_1
ORDER BY seq DESC
) t
ORDER BY seq DESC
--(10 rows affected)
--Table 'Table_1'. Scan count 11, logical reads 26083, physical reads 1, read-ahead reads 443, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 1422 ms, elapsed time = 1621 ms.
My questions are:
- Why the query from 1st attempt is slower than the 2nd one?
- How can I improve the performance further? I can also change schemas.
Just to be clear, both queries return the same result as below.
sql-server database-design t-sql query-performance execution-plan
add a comment |
I'm currently designing a transaction table. I realized that calculating running totals for each row will be needed and this might be slow in performance. So I created a table with 1 million rows for testing purposes.
CREATE TABLE [dbo].[Table_1](
[seq] [int] IDENTITY(1,1) NOT NULL,
[value] [bigint] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[seq] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And I tried to get 10 recent rows and its running totals, but it took about 10 seconds.
--1st attempt
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq) total
FROM Table_1
ORDER BY seq DESC
--(10 rows affected)
--Table 'Worktable'. Scan count 1000001, logical reads 8461526, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Table_1'. Scan count 1, logical reads 2608, physical reads 516, read-ahead reads 2617, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 8483 ms, elapsed time = 9786 ms.
I suspected TOP
for the reason of slow performance from the plan, so I changed query like this, and it took about 1~2 seconds. But I think this is still slow for production and wondering if this can be improved further.
--2nd attempt
SELECT *
,(
SELECT SUM(value)
FROM Table_1
WHERE seq <= t.seq
) total
FROM (
SELECT TOP 10 seq
,value
FROM Table_1
ORDER BY seq DESC
) t
ORDER BY seq DESC
--(10 rows affected)
--Table 'Table_1'. Scan count 11, logical reads 26083, physical reads 1, read-ahead reads 443, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 1422 ms, elapsed time = 1621 ms.
My questions are:
- Why the query from 1st attempt is slower than the 2nd one?
- How can I improve the performance further? I can also change schemas.
Just to be clear, both queries return the same result as below.
sql-server database-design t-sql query-performance execution-plan
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the conditionseq <= t.seq
.
– Denis Rubashkin
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
1
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
1
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago
add a comment |
I'm currently designing a transaction table. I realized that calculating running totals for each row will be needed and this might be slow in performance. So I created a table with 1 million rows for testing purposes.
CREATE TABLE [dbo].[Table_1](
[seq] [int] IDENTITY(1,1) NOT NULL,
[value] [bigint] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[seq] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And I tried to get 10 recent rows and its running totals, but it took about 10 seconds.
--1st attempt
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq) total
FROM Table_1
ORDER BY seq DESC
--(10 rows affected)
--Table 'Worktable'. Scan count 1000001, logical reads 8461526, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Table_1'. Scan count 1, logical reads 2608, physical reads 516, read-ahead reads 2617, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 8483 ms, elapsed time = 9786 ms.
I suspected TOP
for the reason of slow performance from the plan, so I changed query like this, and it took about 1~2 seconds. But I think this is still slow for production and wondering if this can be improved further.
--2nd attempt
SELECT *
,(
SELECT SUM(value)
FROM Table_1
WHERE seq <= t.seq
) total
FROM (
SELECT TOP 10 seq
,value
FROM Table_1
ORDER BY seq DESC
) t
ORDER BY seq DESC
--(10 rows affected)
--Table 'Table_1'. Scan count 11, logical reads 26083, physical reads 1, read-ahead reads 443, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 1422 ms, elapsed time = 1621 ms.
My questions are:
- Why the query from 1st attempt is slower than the 2nd one?
- How can I improve the performance further? I can also change schemas.
Just to be clear, both queries return the same result as below.
sql-server database-design t-sql query-performance execution-plan
I'm currently designing a transaction table. I realized that calculating running totals for each row will be needed and this might be slow in performance. So I created a table with 1 million rows for testing purposes.
CREATE TABLE [dbo].[Table_1](
[seq] [int] IDENTITY(1,1) NOT NULL,
[value] [bigint] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[seq] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And I tried to get 10 recent rows and its running totals, but it took about 10 seconds.
--1st attempt
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq) total
FROM Table_1
ORDER BY seq DESC
--(10 rows affected)
--Table 'Worktable'. Scan count 1000001, logical reads 8461526, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Table_1'. Scan count 1, logical reads 2608, physical reads 516, read-ahead reads 2617, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 8483 ms, elapsed time = 9786 ms.
I suspected TOP
for the reason of slow performance from the plan, so I changed query like this, and it took about 1~2 seconds. But I think this is still slow for production and wondering if this can be improved further.
--2nd attempt
SELECT *
,(
SELECT SUM(value)
FROM Table_1
WHERE seq <= t.seq
) total
FROM (
SELECT TOP 10 seq
,value
FROM Table_1
ORDER BY seq DESC
) t
ORDER BY seq DESC
--(10 rows affected)
--Table 'Table_1'. Scan count 11, logical reads 26083, physical reads 1, read-ahead reads 443, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--
--(1 row affected)
--
-- SQL Server Execution Times:
-- CPU time = 1422 ms, elapsed time = 1621 ms.
My questions are:
- Why the query from 1st attempt is slower than the 2nd one?
- How can I improve the performance further? I can also change schemas.
Just to be clear, both queries return the same result as below.
sql-server database-design t-sql query-performance execution-plan
sql-server database-design t-sql query-performance execution-plan
edited 56 secs ago
user2652379
asked 19 hours ago
user2652379user2652379
18016
18016
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the conditionseq <= t.seq
.
– Denis Rubashkin
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
1
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
1
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago
add a comment |
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the conditionseq <= t.seq
.
– Denis Rubashkin
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
1
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
1
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the condition
seq <= t.seq
.– Denis Rubashkin
18 hours ago
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the condition
seq <= t.seq
.– Denis Rubashkin
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
1
1
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
1
1
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago
add a comment |
1 Answer
1
active
oldest
votes
When dealing with such a small subset of rows returned, the triangular join is a good option. However, when using window functions you have more options that can increase their performance. The default option for window option is RANGE, but the optimal option is ROWS. Be aware that the difference is not only in the performance, but in the results as well when ties are involved.
The following code is slightly faster than the ones you presented.
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq ROWS UNBOUNDED PRECEDING) total
FROM Table_1
ORDER BY seq DESC
New contributor
Thank you for tellingROWS
. I tried it but I can't say it is faster than my 2nd query. The result wasCPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233716%2fhow-can-i-get-running-totals-of-recent-rows-faster%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When dealing with such a small subset of rows returned, the triangular join is a good option. However, when using window functions you have more options that can increase their performance. The default option for window option is RANGE, but the optimal option is ROWS. Be aware that the difference is not only in the performance, but in the results as well when ties are involved.
The following code is slightly faster than the ones you presented.
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq ROWS UNBOUNDED PRECEDING) total
FROM Table_1
ORDER BY seq DESC
New contributor
Thank you for tellingROWS
. I tried it but I can't say it is faster than my 2nd query. The result wasCPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
add a comment |
When dealing with such a small subset of rows returned, the triangular join is a good option. However, when using window functions you have more options that can increase their performance. The default option for window option is RANGE, but the optimal option is ROWS. Be aware that the difference is not only in the performance, but in the results as well when ties are involved.
The following code is slightly faster than the ones you presented.
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq ROWS UNBOUNDED PRECEDING) total
FROM Table_1
ORDER BY seq DESC
New contributor
Thank you for tellingROWS
. I tried it but I can't say it is faster than my 2nd query. The result wasCPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
add a comment |
When dealing with such a small subset of rows returned, the triangular join is a good option. However, when using window functions you have more options that can increase their performance. The default option for window option is RANGE, but the optimal option is ROWS. Be aware that the difference is not only in the performance, but in the results as well when ties are involved.
The following code is slightly faster than the ones you presented.
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq ROWS UNBOUNDED PRECEDING) total
FROM Table_1
ORDER BY seq DESC
New contributor
When dealing with such a small subset of rows returned, the triangular join is a good option. However, when using window functions you have more options that can increase their performance. The default option for window option is RANGE, but the optimal option is ROWS. Be aware that the difference is not only in the performance, but in the results as well when ties are involved.
The following code is slightly faster than the ones you presented.
SELECT TOP 10 seq
,value
,sum(value) OVER (ORDER BY seq ROWS UNBOUNDED PRECEDING) total
FROM Table_1
ORDER BY seq DESC
New contributor
New contributor
answered 12 hours ago
Luis CazaresLuis Cazares
1212
1212
New contributor
New contributor
Thank you for tellingROWS
. I tried it but I can't say it is faster than my 2nd query. The result wasCPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
add a comment |
Thank you for tellingROWS
. I tried it but I can't say it is faster than my 2nd query. The result wasCPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
Thank you for telling
ROWS
. I tried it but I can't say it is faster than my 2nd query. The result was CPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
Thank you for telling
ROWS
. I tried it but I can't say it is faster than my 2nd query. The result was CPU time = 1438 ms, elapsed time = 1537 ms.
– user2652379
21 mins ago
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233716%2fhow-can-i-get-running-totals-of-recent-rows-faster%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Those two queries return different results, don't them? The first one summarizes all values whereas the second one only values which meet the condition
seq <= t.seq
.– Denis Rubashkin
18 hours ago
@DenisRubashkin I checked and both return the same result. I added a screenshot
– user2652379
18 hours ago
you're right I should sort out window functions more attentive
– Denis Rubashkin
18 hours ago
1
I usually don't use window functions, but I remember I read some useful articles on them. Have a look at one Introduction to T-SQL Window Functions, espesially at the part Window Aggregate Enhancements in 2012. Perhaps it gives you some answers.
– Denis Rubashkin
18 hours ago
1
...and one more article of the same excellent author T-SQL Window Functions and Performance
– Denis Rubashkin
18 hours ago