May 12, 2020 at 11:47 pm
I think the most obvious way to rewrite that query would be:
SELECT u.UserId
FROM UserTable u
WHERE EXISTS(SELECT *
from TableA a
where DateColumn > @StartDate
and a.UserId = u.UserId)
OR EXISTS(select *
from TableB b
where Success = 1
and DateColumn > @StartDate
and b.UserId = u.UserId)which I'm sure would perform better.
That was about equal to the CTE.
This runs once a month, and once the devs handed me the re-write, I re-wrote it as you did. The performance difference was negligible. I gave my blessing to their code, which was pretty much of a first.
Now, I have to make sure they don't write everything as a CTE!
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
May 12, 2020 at 11:54 pm
I feel like I'm missing something. An inner join is necessarily an existence check, why not:
SELECT u.UserId
FROM UserTable u
JOIN TableA a ON a.UserId = u.UserId
JOIN TableB b ON b.UserId = u.UserId
WHERE a.DateColumn > @StartDate
AND b.Success = 1
AND b.DateColumn > @StartDate
It's an either or situation for the two tables. The data can exist in one, the other, or both. This logic will eliminate rows
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
May 13, 2020 at 12:23 pm
jonathan.crawford wrote:I feel like I'm missing something. An inner join is necessarily an existence check, why not:
SELECT u.UserId
FROM UserTable u
JOIN TableA a ON a.UserId = u.UserId
JOIN TableB b ON b.UserId = u.UserId
WHERE a.DateColumn > @StartDate
AND b.Success = 1
AND b.DateColumn > @StartDatemore data would be consumed on average. "if exists" makes the determination after the first row fufills the predicate whereas just joining looks to read more from tablea and tableb than needed, also I get "cartesianish" vibes somehow.
also, you're requiring a match in both tablea and tableb but I'm a bit hazy whether that matches the requirements.
Agreed on both observations, especially the latter, which does make it so it must exist in both TableA and TableB, when it should be either.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 13, 2020 at 2:36 pm
If this code was run many times a day, it may be worth spending more time on it. It is executed once a month.
But, it's not the code that can be improved by much. It's the implementation in the database schema and the actual business requirements that need to be looked at. That, plus teaching the developers WHY, is a far better use of my time.
The unfortunate thing is that this code, as well as a lot more like it, was developed by folks who were let go a few weeks ago. The development team lost half of their personnel. The folks left who are trying to make sense of these issues that are now appearing because the usage of the system has risen. These guys needed a "win", badly.
Is this the most efficient code? Maybe not. But their re-write is magnitudes better than the original. I beat these folks up daily. When I gave my blessing to it, it was a desperately needed positive in a sea of negatives that looks like it will not go away any time soon.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
May 19, 2020 at 7:41 am
What's worse: a problem which starts spontaneously and for which nobody is able to track down the cause, or, the same problem spontaneously stopping over the weekend and nobody having changed anything?
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
May 19, 2020 at 8:20 am
What's worse: a problem which starts spontaneously and for which nobody is able to track down the cause, or, the same problem spontaneously stopping over the weekend and nobody having changed anything?
We had a hardware problem which exhibited this pattern, not too long ago. The cause was aging SSD's and a failure of the server/drive diagnostics to see it.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
May 19, 2020 at 5:28 pm
So yesterday we got a bit of good/bad news...
Good, in that the wife has been called back to work and goes back on Tuesday.
Bad, in that the wife got called back to work and will be making LESS per week than she was getting from unemployment, with the extra $600/wk that the gov tacked on.
But frankly, I'm glad she's going back and so is she, in part because having a 40-hr/wk job is a much less stressful source of income than unemployment (especially when you add in the "will she FIND a job when the lockdowns end or when the unemployment runs out?")
May 29, 2020 at 12:00 pm
Any admins about to ban https://www.sqlservercentral.com/forums/user/james500 ? He's spammed over 50 topics in the last hour with advertisements for counterfeit documents.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
May 29, 2020 at 12:06 pm
Any admins about to ban https://www.sqlservercentral.com/forums/user/james500 ? He's spammed over 50 topics in the last hour with advertisements for counterfeit documents.
I was half expecting/hoping that there would at least be some sort of posting-block applied to any user with > x spam messages in a day.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
May 29, 2020 at 12:26 pm
Any admins about to ban https://www.sqlservercentral.com/forums/user/james500 ? He's spammed over 50 topics in the last hour with advertisements for counterfeit documents.
No idea how to ban someone, but I discovered I could edit their profile. Just changed the password. That'll slow 'em down for a moment.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
May 29, 2020 at 12:31 pm
Thom A wrote:Any admins about to ban https://www.sqlservercentral.com/forums/user/james500 ? He's spammed over 50 topics in the last hour with advertisements for counterfeit documents.
No idea how to ban someone, but I discovered I could edit their profile. Just changed the password. That'll slow 'em down for a moment.
Excellent. Make the new password 45 characters long, expiring every minute, and must contain a mixture of Greek and Chinese characters.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
May 29, 2020 at 12:34 pm
Thom A wrote:Any admins about to ban https://www.sqlservercentral.com/forums/user/james500 ? He's spammed over 50 topics in the last hour with advertisements for counterfeit documents.
I was half expecting/hoping that there would at least be some sort of posting-block applied to any user with > x spam messages in a day.
Yeah. At least most of us here have enough reputation to nuke a topic instantly with a single report. Honestly hoped that after 3-5 reports it was at least disable them from posting. Certainly after the 40 or so it should have done.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
May 29, 2020 at 12:42 pm
with wordpress forums I think you need to have one of the addins to manage users - may not be able to delete it, but you can block it (with or without a message)
and yes that functionality to ban user after x reports is sadly missed - going through 20 or 30 posts and reporting 1 at the time to have it hidden isn't fun
May 29, 2020 at 4:26 pm
User blocked. I don't know that there's a way now to block someone with x spam messages, but filing an issue.
Thinking that 3 spam messages is enough. The false positives seem to be 1-2 for a user, so blocking someone at >=3 seems sensible.
May 29, 2020 at 4:43 pm
Probably >3 within a timeframe, to be fair? not >3 ever?
-------------------------------------------------------------------------------------------------------------------------------------
Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses
Viewing 15 posts - 64,891 through 64,905 (of 66,738 total)
You must be logged in to reply to this topic. Login to reply