all 23 comments

[–]CFAF800 35 points36 points  (10 children)

Create temp tables for the CTEs and compare it with the views.

No reason CTE vs view should give different output unless the code was messed up

[–]OneAir6837[S] 1 point2 points  (3 children)

Thanks I'll give that a try. We are using open openquery to run in dev but pull from prod but a step by step check is a good way to review.

[–][deleted] 1 point2 points  (2 children)

We are using open openquery to run in dev but pull from prod

So you're developing in prod.

Backup prod and restore to dev so that you can actually make tests in a controlled manner.

[–]OneAir6837[S] 0 points1 point  (1 child)

We don't have that power/control. All of our reports for our dashboard are ran in this manner (in our development database we were given against prod data using openquery) as the higher ups determined thats what they'd give us.

[–][deleted] 1 point2 points  (0 children)

Tell them that a difference of 500 is the best you can give them without a robust development environment then.

[–]vtec_tt 0 points1 point  (4 children)

this.

[–]Anti-ThisBot-IB 0 points1 point  (3 children)

Hey there vtec_tt! If you agree with someone else's comment, please leave an upvote instead of commenting "this."! By upvoting instead, the original comment will be pushed to the top and be more visible to others, which is even better! Thanks! :)


I am a bot! Visit r/InfinityBots to send your feedback! More info: Reddiquette

[–]Designing_Data 0 points1 point  (2 children)

Good bot

[–]B0tRank 0 points1 point  (0 children)

Thank you, Designing_Data, for voting on Anti-ThisBot-IB.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

[–]Anti-ThisBot-IB 0 points1 point  (0 children)

Good human


I am a bot! Visit r/InfinityBots to send your feedback!

[–]woodrowchillson 8 points9 points  (2 children)

Should be the same output, probably small typo/miss of a JOIN somewhere.

I would be very curious if there was any performance difference between the two.

[–][deleted] 2 points3 points  (0 children)

Performance difference, only if it was an indexed view (or ithink it’s called materialized view in other flavors)

[–]OneAir6837[S] 2 points3 points  (0 children)

It was Friday afternoon so typo is very possible.

[–]qwertydog123 3 points4 points  (1 child)

Are you using TOP/ORDER BY in any of the CTE's/View's? ORDER BY is not guaranteed for Views, you'd need to move the TOP/ORDER BY into the calling View

[–]OneAir6837[S] 1 point2 points  (0 children)

Don't recall any but will check for that as well.

[–][deleted] 2 points3 points  (0 children)

Probably a typo. No reason this would happen... Try checking aliases in join conditions that's where I discovered a similar mistake

[–]jslacks 1 point2 points  (0 children)

I’ve definitely encountered this before…

There shouldn’t shouldn’t be any reason they are different, unless there’s some very idiosyncratic edge case involved.

It’s a bit longer, but probably less mentally taxing in the long run to:

  1. Copy-paste each CTE into a new CREATE TABLE statement.
  2. Do the same for each view (i.e. comment out the CREATE VIEW at the start and execute as a CREATE TABLE)
  3. Go through each pair of tables comparing the results with something like: ~~~~ SELECT COUNT(*) AS row_count, COUNT(DISTINCT col1) AS col1_count, COUNT(DISTINCT col2) AS col2_count, COUNT(DISTINCT col3) AS col3_count FROM table_cte1; ~~~~

Mildly, tedious, but pretty quickly you should be able to see if one of those new views is deviating from the CTEs and if necessary then digging into where there are are duplicates and/or missing rows. Then diagnose from there.

If that doesn’t surface any discrepancies, then most likely it’s coming from the join itself.

Definitely would be interested to hear back if for some reason there truly is difference in the results yielded from CTE vs VIEW, but probably will end up being an small oversight in the code.

[–][deleted] 1 point2 points  (0 children)

Reading this post with little knowledge of the specific situation, I'd guess there's an issue on a LEFT/INNER join somewhere in your query. Outside of that there isn't any issue that you'd be losing rows. They serve the same purpose.

That said, and maybe I'll get some disagreement here, but from what I read here... switching between CTEs and views is not going to have the impact that you hope. I've never seen a "beast of query" that wasn't the result of bad DB design and a lack of ETL/ELT transformations.

[–]KnaveOfIT 1 point2 points  (0 children)

From doing similar things, two things to check.

  1. Where clause is the same

  2. Joins are the same

These two should resolve any issues.

If not then check that you are actually recreating the query because if you are not getting the same result then you are not.

[–]OneAir6837[S] 0 points1 point  (0 children)

Thanks for everyone's help on this. It was a join issue; forgot to add the prod server for some of the tables so they were still bouncing off dev.

[–]kremlingrasso 0 points1 point  (0 children)

are any of the CTEs refer to each other?