Large Transaction Mode

Discussions relating to plugin development, and the Jiwa API.

Re: Large Transaction Mode

Postby DannyC » Tue Jun 02, 2020 6:30 pm

Ok, so the advice is to not use the "trick" of creating a local variable, stuffing the parameter values into them and using the local variables because it looks wasteful / pointless, and to use the OPTIMIZE FOR UNKNOWN hint instead.

I was a bit puzzled on why the speaker* doesn't like that method as he didn't explicitly state why (aside from "Don't ever, ever, ever do this!"), but I rewatched it and figure that it just looks illogical and pointless as you say.
using OPTIMIZE FOR UNKNOWN I guess looks a bit techie and you're still using the passed in parameters. I gather though that he's not a great fan of OPTIMIZE FOR UNKNOWN but in some situations it'll do nicely.

* I watch a lot of Brent Ozar's vids. I've used a few of his suggestions and freebie stored procedures to analyse what's going on at client sites.
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Large Transaction Mode

Postby pricerc » Tue Jun 02, 2020 7:42 pm

As one who has optimized and re-written more than a few queries and stored procedures (including a fair few JIWA ones!), I can tell you if there is a datetime parameter involved (e.g. @AsAtDate) then this is almost always a good optimization target - I've had them kill more than one query.

I had this exact problem last year with a creditor balance as at query (IIRC).

The OPTIMIZE FOR UNKNOWN usually helps.

I don't know what SQL does with datetime and optimization, but it often gets it horribly wrong. IIRC, the newer DATE type doesn't have this problem.

Actually, there's a suggestion I might pop through the suggestion box - all the database fields that only need to be DATEs (which is most of them for accounting purposes), should be changed from DATETIME to DATE fields. DATE takes up less space (just 3 bytes vs 8), is MUCH more efficient (I believe it's implemented in memory as a 32 bit integer), isn't affected by time zones and daylight savings, and it makes queries simpler because you can go
Code: Select all
DECLARE @StartDate DATE = '20200401';
DECLARE @EndDate DATE = '20200430';
...
SELECT ...
WHERE TransactionDate >= @StartDate and TransactionDate <= @EndDate

instead of
Code: Select all
DECLARE @StartDate DATETIME = '20200401';
DECLARE @EndDate DATETIME = '20200430';
...
SELECT ...
WHERE TransactionDate >= @StartDate and TransactionDate < DATEADD(day, 1, @EndDate)


At the same vein, for all new development involving date/time values, I now use DATETIMEOFFSET, since it allows the time zone to be included, which is helpful for a) anywhere with daylight savings and b) anywhere operating across more than one time zone. It also allows you to specify how many fractional seconds you want (for me, it's almost always 0, because in accounting transactions, sub-second resolution is not usually helpful).
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 504
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 20

Re: Large Transaction Mode

Postby Mike.Sheen » Wed Jun 03, 2020 11:44 am

pricerc wrote:Actually, there's a suggestion I might pop through the suggestion box - all the database fields that only need to be DATEs (which is most of them for accounting purposes), should be changed from DATETIME to DATE fields. DATE takes up less space (just 3 bytes vs 8), is MUCH more efficient (I believe it's implemented in memory as a 32 bit integer), isn't affected by time zones and daylight savings, and it makes queries simpler


Agreed. We have actually started casting DATETIME params in some stored procs to DATE variables because they're easier to deal with.

Changing the parameters to DATE, or column types to DATE is something we would love to do, but we are somewhat hamstrung by legacy. the DATE datatype didn't exist in older versions of SQL Server, so this is why we currently still have DATETIME types.

Whilst we don't mind changing our table columns and stored procedure parameters - those who have written their own reports, applications and so on might get upset with such changes - so we need to manage that change carefully.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Large Transaction Mode

Postby Mike.Sheen » Wed Jun 03, 2020 1:20 pm

DannyC wrote:I was a bit puzzled on why the *speaker* doesn't like that method as he didn't explicitly state why (aside from "Don't ever, ever, ever do this!"), but I rewatched it and figure that it just looks illogical and pointless as you say.


It's not really intuitive - which is why I added the comment explaining why I was using local variables - and that is mentioned in the video that anyone who comes along to maintain the code might not understand the reasoning behind using the local variables and be tempted to remove that seemingly inefficient and pointless code and re-introduce the problem.

Another aspect might be that at some point in the future, the SQL Optimizer might smarten it's parsing up of the contents of stored procs and it won't be tricked by moving parameters into local variables and using them - a smarter optimiser / parser would be able to recognise how the parameters are being used within the stored proc and end up - through it's now advanced intelligence - messing up the optimisations all over again.

Having said that, an introduction of such a change would be shielded by a trace flag - if Microsoft are going to be consistently timid about introducing changes - so you'd have to opt-in to such a change at the database level.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Previous

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 3 guests