Showing posts with label plan. Show all posts
Showing posts with label plan. Show all posts

Monday, March 26, 2012

Duplicate index spool or not?

Hello. I have a question about the execution plan here:

The four branches are identical and the Index Spools take in the same record set and output the same expressions. Also, no rebinding will be necessary during the entire query (as far as I can gather). My question is (and I know it may sound silly, but bare with the newbie here): does that plan say that there will be four identical temporary indexes created, one for each branch? Or is it that there will be a single spooled index which will be used on all 4 branches (and that would make perfect sense) but there's no other way for the execution plan to show it (as in, draw it)?

Here's a reduced version of my query:
(and full version here)

-- CTE here

WITH yearlySales (SalesPersonID, SalesYear, TotalSales) AS
(SELECT SalesPersonID, YEAR(OrderDate) as SalesYear, SUM(TotalDue) as TotalSales
FROM Sales.SalesOrderHeader
WHERE YEAR(OrderDate) BETWEEN 2001 AND 2003
GROUP BY SalesPersonID, YEAR(OrderDate)
)

-- Main statement

SELECT sp.SalesPersonID
FROM Sales.SalesPerson sp INNER JOIN HumanResources.Employee
ON sp.SalesPersonID = Employee.EmployeeID
WHERE

((SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2003 AND SalesPersonID = sp.SalesPersonID)

<=

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2002 AND SalesPersonID = sp.SalesPersonID)

OR

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2002 AND SalesPersonID = sp.SalesPersonID)

<=

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2001 AND SalesPersonID = sp.SalesPersonID))

The plan indicates that four identical temporary indexes are being created. This is what happens. CTE in the standard is supposed to provide more than just syntax level substitution. So if you use a particular query expression multiple times then the database engine is supposed to optimize the access patterns. But SQL Server does not currently do this and we do only syntax level substitution. Hence your query above has the plan you are seeing. You can simplify the query by doing below instead which is more efficient:

WITH yearlySales (SalesPersonID, SalesYear, TotalSales) AS
(
SELECT SalesPersonID, YEAR(OrderDate) as SalesYear, SUM(TotalDue) as TotalSales
FROM Sales.SalesOrderHeader
WHERE YEAR(OrderDate) BETWEEN 2001 AND 2003
GROUP BY SalesPersonID, YEAR(OrderDate)
),
p_yearlysales (SalesPersonID, [2001], [2002], [2003]) as
(
select SalesPersonID, [2001], [2002], [2003]
from yearlysales
pivot (max(TotalSales) for SalesYear in ([2001], [2002], [2003])) as p
)
-- Main statement

SELECT sp.SalesPersonID
FROM Sales.SalesPerson sp INNER JOIN HumanResources.Employee
ON sp.SalesPersonID = Employee.EmployeeID
JOIN p_yearlysales as p
ON p.SalesPersonID = sp.SalesPersonID
WHERE [2003] <= [2002] or [2002] <= [2001];

Friday, March 9, 2012

Dual Processors

Hi,
I've been recommended a server spec by an application vendor. I plan to buy
and start using this app, and it runs on SQL Server.
The server spec is for a single processor model. There will be a core of
about 25 users on the system with a maximum of 40. How can I decide if I
should buy a dual-processor server? Are there any rules to apply?
Thanks in advance
Steve"Steve W" <antispamsteveW@.=No-Spam=.org> wrote in message
news:uOZCyIhXEHA.376@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I've been recommended a server spec by an application vendor. I plan to
buy
> and start using this app, and it runs on SQL Server.
> The server spec is for a single processor model. There will be a core of
> about 25 users on the system with a maximum of 40. How can I decide if I
> should buy a dual-processor server? Are there any rules to apply?
>
No. Too much depends on how the application is coded and what it does. You
should rely on your vendor to recommend the hardware.
-new servers are incredibilly fast
-single processor servers are incredibilly cheap
-most applications require more IO than CPU
-If you plan on using this server for other applications too you might
want increased capacity.
For small general purpose Intel 32bit SQL Servers I would buy like this:
CPU SCSI disks disk config RAID logical disk config
----
1 2 RAID 1 mirror 10g C: for OS, rest D: for SQL
tables, indexes and logs
1 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
4 RAID 10 (mirror/stripe) (e: sql logs and backups)
And buy have as at least as much ram as you have data in your databases, up
to 2g.
David

Dual Processors

Hi,
I've been recommended a server spec by an application vendor. I plan to buy
and start using this app, and it runs on SQL Server.
The server spec is for a single processor model. There will be a core of
about 25 users on the system with a maximum of 40. How can I decide if I
should buy a dual-processor server? Are there any rules to apply?
Thanks in advance
Steve
"Steve W" <antispamsteveW@.=No-Spam=.org> wrote in message
news:uOZCyIhXEHA.376@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I've been recommended a server spec by an application vendor. I plan to
buy
> and start using this app, and it runs on SQL Server.
> The server spec is for a single processor model. There will be a core of
> about 25 users on the system with a maximum of 40. How can I decide if I
> should buy a dual-processor server? Are there any rules to apply?
>
No. Too much depends on how the application is coded and what it does. You
should rely on your vendor to recommend the hardware.
-new servers are incredibilly fast
-single processor servers are incredibilly cheap
-most applications require more IO than CPU
-If you plan on using this server for other applications too you might
want increased capacity.
For small general purpose Intel 32bit SQL Servers I would buy like this:
CPU SCSI disks disk config RAID logical disk config
1 2 RAID 1 mirror 10g C: for OS, rest D: for SQL
tables, indexes and logs
1 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
4 RAID 10 (mirror/stripe) (e: sql logs and backups)
And buy have as at least as much ram as you have data in your databases, up
to 2g.
David

Dual Processors

Hi,
I've been recommended a server spec by an application vendor. I plan to buy
and start using this app, and it runs on SQL Server.
The server spec is for a single processor model. There will be a core of
about 25 users on the system with a maximum of 40. How can I decide if I
should buy a dual-processor server? Are there any rules to apply?
Thanks in advance
Steve
"Steve W" <antispamsteveW@.=No-Spam=.org> wrote in message
news:uOZCyIhXEHA.376@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I've been recommended a server spec by an application vendor. I plan to
buy
> and start using this app, and it runs on SQL Server.
> The server spec is for a single processor model. There will be a core of
> about 25 users on the system with a maximum of 40. How can I decide if I
> should buy a dual-processor server? Are there any rules to apply?
>
No. Too much depends on how the application is coded and what it does. You
should rely on your vendor to recommend the hardware.
-new servers are incredibilly fast
-single processor servers are incredibilly cheap
-most applications require more IO than CPU
-If you plan on using this server for other applications too you might
want increased capacity.
For small general purpose Intel 32bit SQL Servers I would buy like this:
CPU SCSI disks disk config RAID logical disk config
1 2 RAID 1 mirror 10g C: for OS, rest D: for SQL
tables, indexes and logs
1 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
4 RAID 10 (mirror/stripe) (e: sql logs and backups)
And buy have as at least as much ram as you have data in your databases, up
to 2g.
David

Dual Processors

Hi,
I've been recommended a server spec by an application vendor. I plan to buy
and start using this app, and it runs on SQL Server.
The server spec is for a single processor model. There will be a core of
about 25 users on the system with a maximum of 40. How can I decide if I
should buy a dual-processor server? Are there any rules to apply?
Thanks in advance
Steve"Steve W" <antispamsteveW@.=No-Spam=.org> wrote in message
news:uOZCyIhXEHA.376@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I've been recommended a server spec by an application vendor. I plan to
buy
> and start using this app, and it runs on SQL Server.
> The server spec is for a single processor model. There will be a core of
> about 25 users on the system with a maximum of 40. How can I decide if I
> should buy a dual-processor server? Are there any rules to apply?
>
No. Too much depends on how the application is coded and what it does. You
should rely on your vendor to recommend the hardware.
-new servers are incredibilly fast
-single processor servers are incredibilly cheap
-most applications require more IO than CPU
-If you plan on using this server for other applications too you might
want increased capacity.
For small general purpose Intel 32bit SQL Servers I would buy like this:
CPU SCSI disks disk config RAID logical disk config
----
1 2 RAID 1 mirror 10g C: for OS, rest D: for SQL
tables, indexes and logs
1 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
2 RAID 1 (mirror) (e: sql logs and backups)
2 2 RAID 1 (mirror) (10g C: for OS, rest D: for SQL
tables and indexes)
4 RAID 10 (mirror/stripe) (e: sql logs and backups)
And buy have as at least as much ram as you have data in your databases, up
to 2g.
David