473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Does (code) size matter?!?

The age-old question: does size really matter? I mean code size - get your
minds out of the gutter!

I have a table to store customer discounts (SQL 2000). The table has up to
10 possible levels of minimum qty and discount. Then there are 8 possible
discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to increment
1 - 10.

minqty1 disc1 minqty2 disc2 .... minqty10
disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'

Question: Is using dynamic sql and variables like this any more or less
efficient than simply repeating this code 10 times per discount type?

If I simply coded the 10 discount levels I could use a table variable
instead of the temp table I am using with dynamic sql.

Oct 16 '08 #1
7 1442
To me it sounds like the ten discount levels should be in a table, and
dealt with my joining to the table. Or perhaps I just don't
understand what you are doing.

Roy Harvey
Beacon Falls, CT

On Thu, 16 Oct 2008 15:56:05 -0700, "Artie" <ar*******@yahoo.com>
wrote:
>The age-old question: does size really matter? I mean code size - get your
minds out of the gutter!

I have a table to store customer discounts (SQL 2000). The table has up to
10 possible levels of minimum qty and discount. Then there are 8 possible
discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to increment
1 - 10.

minqty1 disc1 minqty2 disc2 .... minqty10
disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'

Question: Is using dynamic sql and variables like this any more or less
efficient than simply repeating this code 10 times per discount type?

If I simply coded the 10 discount levels I could use a table variable
instead of the temp table I am using with dynamic sql.

Oct 17 '08 #2
I have a table to store customer discounts (SQL 2000). The table has up
to 10 possible levels of minimum qty and discount. Then there are 8
possible discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to increment
1 - 10.

minqty1 disc1 minqty2 disc2 .... minqty10
disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'
I believe this task would be much simpler with a normalized database design
(no repeating groups). I have no idea what a discount type is nor your
business rules for determining the best available discount but the example
below should get you started:

CREATE TABLE dbo.discount_types
(
discount_type int NOT NULL
CONSTRAINT PK_discount_types PRIMARY KEY
description varchar(100) NOT NULL
)

CREATE TABLE dbo.discounts
(
discount int NOT NULL
CONSTRAINT PK_discounts PRIMARY KEY,
discount_type int NOT NULL
CONSTRAINT FK_discounts_discount_type FOREIGN KEY
REFERENCES dbo.discount_types(discount_type),
minqty int NOT NULL
);

--set-based query to determine best discount
SELECT MAX(Discount)
FROM dbo.discounts
WHERE minqty < @order_quantity;

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Artie" <ar*******@yahoo.comwrote in message
news:ND****************@nlpi070.nbdc.sbc.com...
The age-old question: does size really matter? I mean code size - get
your minds out of the gutter!

I have a table to store customer discounts (SQL 2000). The table has up
to 10 possible levels of minimum qty and discount. Then there are 8
possible discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to increment
1 - 10.

minqty1 disc1 minqty2 disc2 .... minqty10
disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'

Question: Is using dynamic sql and variables like this any more or less
efficient than simply repeating this code 10 times per discount type?

If I simply coded the 10 discount levels I could use a table variable
instead of the temp table I am using with dynamic sql.


Oct 17 '08 #3
I certainly agree with normalizing the db but unfortunately that is not an
option. This is for an application so I need to work with what is there.

Maybe I answered my own question with the table variable vs temp table. If
I code each of the 10 discount level checks, I can use a table variable. If
I use dynamic sql then I need to use a temp table.
"Dan Guzman" <gu******@nospam-online.sbcglobal.netwrote in message
news:%E***************@flpi147.ffdc.sbc.com...
>I have a table to store customer discounts (SQL 2000). The table has up
to 10 possible levels of minimum qty and discount. Then there are 8
possible discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to
increment 1 - 10.

minqty1 disc1 minqty2 disc2 ....
minqty10 disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'

I believe this task would be much simpler with a normalized database
design (no repeating groups). I have no idea what a discount type is nor
your business rules for determining the best available discount but the
example below should get you started:

CREATE TABLE dbo.discount_types
(
discount_type int NOT NULL
CONSTRAINT PK_discount_types PRIMARY KEY
description varchar(100) NOT NULL
)

CREATE TABLE dbo.discounts
(
discount int NOT NULL
CONSTRAINT PK_discounts PRIMARY KEY,
discount_type int NOT NULL
CONSTRAINT FK_discounts_discount_type FOREIGN KEY
REFERENCES dbo.discount_types(discount_type),
minqty int NOT NULL
);

--set-based query to determine best discount
SELECT MAX(Discount)
FROM dbo.discounts
WHERE minqty < @order_quantity;

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Artie" <ar*******@yahoo.comwrote in message
news:ND****************@nlpi070.nbdc.sbc.com...
>The age-old question: does size really matter? I mean code size - get
your minds out of the gutter!

I have a table to store customer discounts (SQL 2000). The table has up
to 10 possible levels of minimum qty and discount. Then there are 8
possible discount types.
I created a proc to look for the best discount available. It has to go
though each of the 8 types and 10 levels of discounts per type.

The 8 types have different where clauses so I did not see a way around
having 8 separate sets of syntax.
For the 10 discount levels, I used an int variable (@disclvl) to
increment 1 - 10.

minqty1 disc1 minqty2 disc2 ....
minqty10 disc10

The proc uses dynamic sql like:
where 'minqty' + @disclvl + ' = blahblah'

Question: Is using dynamic sql and variables like this any more or less
efficient than simply repeating this code 10 times per discount type?

If I simply coded the 10 discount levels I could use a table variable
instead of the temp table I am using with dynamic sql.



Oct 17 '08 #4
Artie wrote:
I certainly agree with normalizing the db but unfortunately that is not an
option. This is for an application so I need to work with what is there.
Can you fake it by adding normalized views?
Oct 18 '08 #5
Artie (ar*******@yahoo.com) writes:
I certainly agree with normalizing the db but unfortunately that is not an
option. This is for an application so I need to work with what is there.
One approach would be to first unpivot this beast into temp table:

SELECT whatid, disclvl,
CASE disclvl WHEN 1 THEN minqty1
THEN 2 THEN minqty 2
..
FROM discounts
CROSS JOIN (SELECT disclvl = 1
UNION ALL
SELECT 2
...
SELECT 10) AS d

And then you can work with table in a more civilised way.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Oct 18 '08 #6
bill (bi**********@gmail.com) writes:
I am bumping this thread, becuase I'd like someone who is more
familiar with the insides of the SQL Server engine to tell me how the
idea I outlined below will perform. I am thinking of doing this for a
similar problem that I face, and want to know if there will be any
issues. For the reasons I outline near the end of the message
(paragraph right before my signature) I think things will work fine,
but I'd like to hear from someone else.
An alternative is is a query that goes:

SELECT part_no, n,
CASE n WHEN 1 THEN disc_qt_01
WHEN 2 THEN disc_qt_02
WHEN 3 THEN disc_qt_03
WHEN 4 THEN disc_qt_04
WHEN 5 THEN disc_qt_05
END,
CASE n WHEN 1 THEN disc_price_01
WHEN 2 THEN disc_price_02
WHEN 3 THEN disc_price_03
WHEN 4 THEN disc_price_04
WHEN 5 THEN disc_price_05
END
FROM discount_flat
CROSS JOIN (SELECT n = 1 UNION ALL SELECT 2 UNION ALL 3
SELECT 4 UNION ALL SELECT 5) AS n

I would expect this to perform better than your query, which is likely
to access the table once per term in the UNION. But, as always, you
need to benchmark to find out.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Oct 31 '08 #7
I like Erland's query better too. Because everything resolves to one
row, the view I proposed may (or may not) perform as well as Erland's
idea. However, even if view performs as well, Erland's has the
advantage of not requiring one to make a view. From a practical
standpoint, it is often the case that the developer doesn't have the
permissions to create views. From an elegance standpoint, other
things being equal, fewer permanent objects in the database the
better.

Thanks,

Bill

On Oct 31, 3:18*pm, Erland Sommarskog <esq...@sommarskog.sewrote:
An alternative is is a query that goes:

* SELECT part_no, n,
* * * * *CASE n WHEN 1 THEN disc_qt_01
* * * * * * * * WHEN 2 THEN disc_qt_02
* * * * * * * * WHEN 3 THEN disc_qt_03
* * * * * * * * WHEN 4 THEN disc_qt_04
* * * * * * * * WHEN 5 THEN disc_qt_05
* * * * *END,
* * * * *CASE n WHEN 1 THEN disc_price_01
* * * * * * * * WHEN 2 THEN disc_price_02
* * * * * * * * WHEN 3 THEN disc_price_03
* * * * * * * * WHEN 4 THEN disc_price_04
* * * * * * * * WHEN 5 THEN disc_price_05
* * * * *END
* FROM * discount_flat
* CROSS JOIN (SELECT n = 1 UNION ALL SELECT 2 UNION ALL 3
* * * * * * * SELECT 4 UNION ALL SELECT 5) AS n

I would expect this to perform better than your query, which is likely
to access the table once per term in the UNION. But, as always, you
need to benchmark to find out.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 3 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcus | last post by:
Hi all, Quick question... I have a class that is over 1200 lines of code and 50 KB in size. This file does not display any html, it is just included and after I call all the necessary...
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
11
by: chinu | last post by:
mail hi all.. is it possible to find the size of a structure upto bit size.., where the structure contains only one element and that too with bit specifier..eg.. struct x { char a : 4; } y;
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
4
by: rn5a | last post by:
Consider the following code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath("/Folder1"))...
11
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I know I sound like a one-note Johnny on this but I'm still looking for a solution. I need to display characters coming in from a serial port or a socket. I also need to be able to type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.