PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > CTEs - sometimes much slower, sometimes not
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
CTEs - sometimes much slower, sometimes not

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2008, 21h29   #1
Mike C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut CTEs - sometimes much slower, sometimes not

If I look at the query plan of 2 simple queries that are essentially
the same, but one uses a CTE, the plans are the same and so is the
logical reads to execute it. Here is an example:

WITH x as (SELECT titleid, authorid FROM books)
SELECT * from x where authorid=99

SELECT titleid, authorid FROM books where authorid=99

These have the same plan and I/O so obviously it isn't evaluating the
CTE and then applying the where clause

Sometimes, it seems like the entire CTE is executed before applying
any WHERE clause data in the actual select. Does anyone know why this
is and what triggers it? I have a very complicated query using 3
ctes, and based on the performance, I can tell it is evaluating 1 of
the CTEs before the WHERE clause is applied to it.



  Réponse avec citation
Vieux 16/07/2008, 22h02   #2
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

How are the CTEs joined? And if the where clause *could* be applied within
the CTE, why isn't it applied there?



On 7/16/08 3:29 PM, in article
85f33c3e-13bd-4698-9a16-a8ce5d56d680...oglegroups.com, "Mike C"
<michaeljc70@hotmail.com> wrote:

> If I look at the query plan of 2 simple queries that are essentially
> the same, but one uses a CTE, the plans are the same and so is the
> logical reads to execute it. Here is an example:
>
> WITH x as (SELECT titleid, authorid FROM books)
> SELECT * from x where authorid=99
>
> SELECT titleid, authorid FROM books where authorid=99
>
> These have the same plan and I/O so obviously it isn't evaluating the
> CTE and then applying the where clause
>
> Sometimes, it seems like the entire CTE is executed before applying
> any WHERE clause data in the actual select. Does anyone know why this
> is and what triggers it? I have a very complicated query using 3
> ctes, and based on the performance, I can tell it is evaluating 1 of
> the CTEs before the WHERE clause is applied to it.
>
>
>


  Réponse avec citation
Vieux 16/07/2008, 22h19   #3
Mike C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

On Jul 16, 3:02pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> How are the CTEs joined? And if the where clause *could* be applied within
> the CTE, why isn't it applied there?
>
> On 7/16/08 3:29 PM, in article
> 85f33c3e-13bd-4698-9a16-a8ce5d56d...@a70g2000hsh.googlegroups.com, "Mike C"
>
> <michaelj...@hotmail.com> wrote:
> > If I look at the query plan of 2 simple queries that are essentially
> > the same, but one uses a CTE, the plans are the same and so is the
> > logical reads to execute it. Here is an example:

>
> > WITH x as (SELECT titleid, authorid FROM books)
> > SELECT * from x where authorid=99

>
> > SELECT titleid, authorid FROM books where authorid=99

>
> > These have the same plan and I/O so obviously it isn't evaluating the
> > CTE and then applying the where clause

>
> > Sometimes, it seems like the entire CTE is executed before applying
> > any WHERE clause data in the actual select. Does anyone know why this
> > is and what triggers it? I have a very complicated query using 3
> > ctes, and based on the performance, I can tell it is evaluating 1 of
> > the CTEs before the WHERE clause is applied to it.


The where clause can't be applied there because it is dynamic. If I
take a specific instance though and move it in the CTE, it is much
faster (20 sec vs 230 sec).

The query is very complicated- 3 CTEs and a total of around 40 tables
(some repeated in diff ctes). Too make a very long story short, the
only reason I am using this CTE in issue is that the user can select
criteria dynamically. This consists of 14 different fields. I cannot
apply those directly to the query because you cannot use aliases in
the where clause. I am therefore wrapping it in a CTE and then
applying this dynamic where clause.

Here's an example: I get in something that could be like "WHERE foo
BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
(obviously simplified):

SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
END as foo
FROM tttt

I am wrapping this select in the CTE so I can just apply the where
clause as is.



  Réponse avec citation
Vieux 16/07/2008, 22h40   #4
Aaron Bertrand [SQL Server MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

> criteria dynamically. This consists of 14 different fields. I cannot
> apply those directly to the query because you cannot use aliases in
> the where clause.


But you can use them in a subquery / derived table. This is a common
workaround to not having to repeat calculations, e.g.

SELECT fullname
FROM (SELECT fullname = FirstName + ' ' + LastName FROM Customers) x
GROUP BY fullname;

A

  Réponse avec citation
Vieux 16/07/2008, 22h44   #5
TheSQLGuru
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

Since you are doing dynamic sql, are you SURE you can't do the entire
excutable statement in dynamic sql without CTEs (or perhaps with them but
collapse the wheres/sargs)??

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"Mike C" <michaeljc70@hotmail.com> wrote in message
news:e662a0dd-efc1-41d9-bc90-c21ba724d5d8@34g2000hsh.googlegroups.com...
On Jul 16, 3:02 pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> How are the CTEs joined? And if the where clause *could* be applied within
> the CTE, why isn't it applied there?
>
> On 7/16/08 3:29 PM, in article
> 85f33c3e-13bd-4698-9a16-a8ce5d56d...@a70g2000hsh.googlegroups.com, "Mike
> C"
>
> <michaelj...@hotmail.com> wrote:
> > If I look at the query plan of 2 simple queries that are essentially
> > the same, but one uses a CTE, the plans are the same and so is the
> > logical reads to execute it. Here is an example:

>
> > WITH x as (SELECT titleid, authorid FROM books)
> > SELECT * from x where authorid=99

>
> > SELECT titleid, authorid FROM books where authorid=99

>
> > These have the same plan and I/O so obviously it isn't evaluating the
> > CTE and then applying the where clause

>
> > Sometimes, it seems like the entire CTE is executed before applying
> > any WHERE clause data in the actual select. Does anyone know why this
> > is and what triggers it? I have a very complicated query using 3
> > ctes, and based on the performance, I can tell it is evaluating 1 of
> > the CTEs before the WHERE clause is applied to it.


The where clause can't be applied there because it is dynamic. If I
take a specific instance though and move it in the CTE, it is much
faster (20 sec vs 230 sec).

The query is very complicated- 3 CTEs and a total of around 40 tables
(some repeated in diff ctes). Too make a very long story short, the
only reason I am using this CTE in issue is that the user can select
criteria dynamically. This consists of 14 different fields. I cannot
apply those directly to the query because you cannot use aliases in
the where clause. I am therefore wrapping it in a CTE and then
applying this dynamic where clause.

Here's an example: I get in something that could be like "WHERE foo
BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
(obviously simplified):

SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
END as foo
FROM tttt

I am wrapping this select in the CTE so I can just apply the where
clause as is.




  Réponse avec citation
Vieux 16/07/2008, 23h10   #6
Mike C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

On Jul 16, 3:40pm, "Aaron Bertrand [SQL Server MVP]"
<ten....@dnartreb.noraa> wrote:
> > criteria dynamically. This consists of 14 different fields. I cannot
> > apply those directly to the query because you cannot use aliases in
> > the where clause.

>
> But you can use them in a subquery / derived table. This is a common
> workaround to not having to repeat calculations, e.g.
>
> SELECT fullname
> FROM (SELECT fullname = FirstName + ' ' + LastName FROM Customers) x
> GROUP BY fullname;
>
> A


Well, isn't a derived table and CTE essentially the same thing? To me
it is.

Keep in mind I have at least 80 fields, about 12 of which are CASE
statements.

  Réponse avec citation
Vieux 16/07/2008, 23h12   #7
Mike C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

On Jul 16, 3:44pm, "TheSQLGuru" <kgbo...@earthlink.net> wrote:
> Since you are doing dynamic sql, are you SURE you can't do the entire
> excutable statement in dynamic sql without CTEs (or perhaps with them but
> collapse the wheres/sargs)??
>
> --
> Kevin G. Boles
> Indicium Resources, Inc.
> SQL Server MVP
> kgboles a earthlink dt net
>
> "Mike C" <michaelj...@hotmail.com> wrote in message
>
> news:e662a0dd-efc1-41d9-bc90-c21ba724d5d8@34g2000hsh.googlegroups.com...
> On Jul 16, 3:02 pm, "Aaron Bertrand [SQL Server MVP]"
>
>
>
> <ten....@dnartreb.noraa> wrote:
> > How are the CTEs joined? And if the where clause *could* be applied within
> > the CTE, why isn't it applied there?

>
> > On 7/16/08 3:29 PM, in article
> > 85f33c3e-13bd-4698-9a16-a8ce5d56d...@a70g2000hsh.googlegroups.com, "Mike
> > C"

>
> > <michaelj...@hotmail.com> wrote:
> > > If I look at the query plan of 2 simple queries that are essentially
> > > the same, but one uses a CTE, the plans are the same and so is the
> > > logical reads to execute it. Here is an example:

>
> > > WITH x as (SELECT titleid, authorid FROM books)
> > > SELECT * from x where authorid=99

>
> > > SELECT titleid, authorid FROM books where authorid=99

>
> > > These have the same plan and I/O so obviously it isn't evaluating the
> > > CTE and then applying the where clause

>
> > > Sometimes, it seems like the entire CTE is executed before applying
> > > any WHERE clause data in the actual select. Does anyone know why this
> > > is and what triggers it? I have a very complicated query using 3
> > > ctes, and based on the performance, I can tell it is evaluating 1 of
> > > the CTEs before the WHERE clause is applied to it.

>
> The where clause can't be applied there because it is dynamic. If I
> take a specific instance though and move it in the CTE, it is much
> faster (20 sec vs 230 sec).
>
> The query is very complicated- 3 CTEs and a total of around 40 tables
> (some repeated in diff ctes). Too make a very long story short, the
> only reason I am using this CTE in issue is that the user can select
> criteria dynamically. This consists of 14 different fields. I cannot
> apply those directly to the query because you cannot use aliases in
> the where clause. I am therefore wrapping it in a CTE and then
> applying this dynamic where clause.
>
> Here's an example: I get in something that could be like "WHERE foo
> BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
> (obviously simplified):
>
> SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
> END as foo
> FROM tttt
>
> I am wrapping this select in the CTE so I can just apply the where
> clause as is.


I could use a view or derived table, but don't see how that is going
to performance.....they are all functionally the same thing.
  Réponse avec citation
Vieux 17/07/2008, 16h24   #8
Mike C
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: CTEs - sometimes much slower, sometimes not

On Jul 16, 4:12pm, Mike C <michaelj...@hotmail.com> wrote:
> On Jul 16, 3:44pm, "TheSQLGuru" <kgbo...@earthlink.net> wrote:
>
>
>
> > Since you are doing dynamic sql, are you SURE you can't do the entire
> > excutable statement in dynamic sql without CTEs (or perhaps with them but
> > collapse the wheres/sargs)??

>
> > --
> > Kevin G. Boles
> > Indicium Resources, Inc.
> > SQL Server MVP
> > kgboles a earthlink dt net

>
> > "Mike C" <michaelj...@hotmail.com> wrote in message

>
> >news:e662a0dd-efc1-41d9-bc90-c21ba724d5d8@34g2000hsh.googlegroups.com...
> > On Jul 16, 3:02 pm, "Aaron Bertrand [SQL Server MVP]"

>
> > <ten....@dnartreb.noraa> wrote:
> > > How are the CTEs joined? And if the where clause *could* be applied within
> > > the CTE, why isn't it applied there?

>
> > > On 7/16/08 3:29 PM, in article
> > > 85f33c3e-13bd-4698-9a16-a8ce5d56d...@a70g2000hsh.googlegroups.com, "Mike
> > > C"

>
> > > <michaelj...@hotmail.com> wrote:
> > > > If I look at the query plan of 2 simple queries that are essentially
> > > > the same, but one uses a CTE, the plans are the same and so is the
> > > > logical reads to execute it. Here is an example:

>
> > > > WITH x as (SELECT titleid, authorid FROM books)
> > > > SELECT * from x where authorid=99

>
> > > > SELECT titleid, authorid FROM books where authorid=99

>
> > > > These have the same plan and I/O so obviously it isn't evaluating the
> > > > CTE and then applying the where clause

>
> > > > Sometimes, it seems like the entire CTE is executed before applying
> > > > any WHERE clause data in the actual select. Does anyone know why this
> > > > is and what triggers it? I have a very complicated query using 3
> > > > ctes, and based on the performance, I can tell it is evaluating 1 of
> > > > the CTEs before the WHERE clause is applied to it.

>
> > The where clause can't be applied there because it is dynamic. If I
> > take a specific instance though and move it in the CTE, it is much
> > faster (20 sec vs 230 sec).

>
> > The query is very complicated- 3 CTEs and a total of around 40 tables
> > (some repeated in diff ctes). Too make a very long story short, the
> > only reason I am using this CTE in issue is that the user can select
> > criteria dynamically. This consists of 14 different fields. I cannot
> > apply those directly to the query because you cannot use aliases in
> > the where clause. I am therefore wrapping it in a CTE and then
> > applying this dynamic where clause.

>
> > Here's an example: I get in something that could be like "WHERE foo
> > BETWEEN '01/01/2007' AND '02/01/2007'". My select looks like this
> > (obviously simplified):

>
> > SELECT CASE WHEN xxxxx THEN blah WHEN yyyyy THEN fdfdfd WHEN ....
> > END as foo
> > FROM tttt

>
> > I am wrapping this select in the CTE so I can just apply the where
> > clause as is.

>
> I could use a view or derived table, but don't see how that is going
> to performance.....they are all functionally the same thing.


Problem solved---not exactly elegant, but it works. I do a replace on
the WHERE clause passed in and replace it with whatever is in the
select list. Basically, something like REPLACE('WHERE
authorid=11','authorid',' CASE WHEN xxxx THEN yyyy WHEN fdsfd then
sss').

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17666 seconds with 16 queries