| Author |
Topic |
|
johnstern
Yak Posting Veteran
67 Posts |
Posted - 2007-04-26 : 18:04:44
|
Hello, I have a stored procedured below that will be used by 3 different websites, so I need check which site is calling it in order to return the correct results. currently this is my approach but I am little bit nervous about performance( numbers of rows is about 10k), and even the actual logic I am following. This is the first time that I made this type of multi website stored procedured so I would like to get some opinions. Is this the right approach?, am I overlooking something simpler?. the below code is only an example, the select * statement is actually 20 lines long ( so that is why I did not just make 3 if else statements with their own queries. otherwise I was going to have near identical 20 lines code, 3 times) thank you for your inputCREATE PROCEDURE dbo.GetContent @SiteFlag varchar(10) ASDeclare @IsCNN int,@IsGOOGLE int, @IsMSN intset @ IsCNN = 0set @ IsGOOGLE = 0set @ IsMSN =0IF @SiteFlag = 'CNN'Set @ IsCNN = 1IF @SiteFlag = GOOGLE'Set @ IsGOOGLE = 1IF @SiteFlag = 'MSN' Set @ IsMSN = 1SELECT * FROM mytable WHERE IsCNN = @IsCNN and IsGOOGLE = @IsGOOGLE and IsMSN = @IsMSN MytableId | content | IsCNN| IsGOOGLE| IsMSN|1 | my text | 1 | 1 | 0|2 | my other text | 0 | 0 | 1|3 | my very other text | 0 | 1 | 1| |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:20:31
|
| [code]CREATE PROCEDURE dbo.uspGetContent( @SiteFlag VARCHAR(10) )ASSET NOCOUNT ONSELECT ID, ContentFROM MyTableWHERE IsCNN = 1 AND @SiteFlag = 'CNN' OR IsMSN = 1 AND @SiteFlag = 'MSN' OR IsGOOGLE = 1 AND @SiteFlag = 'Google'[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:22:51
|
You would be better off normalizing your database, and use something like thisCREATE PROCEDURE dbo.uspGetContent( @SiteFlag VARCHAR(10) )ASSET NOCOUNT ONSELECT ID, ContentFROM MyTableWHERE Site = @SiteFlag Peter LarssonHelsingborg, Sweden |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-26 : 18:25:39
|
1. select * in a proc is a no-no. if you add or remove a column to the table, the proc continues to work but clients will break.2. I would probably use a single bitmask column instead of 3 separate bit columns (so you can easily add another provider without adding another column) www.elsasoft.org |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:28:01
|
Or even betterMyTableID CONTENT-- ------------------1 my text2 my other text3 my very other textMyContentSitesID ContentID Site-- --------- -------1 1 CNN2 1 GOOGLE3 2 MSN4 3 GOOGLE5 3 MSNSELECT t.ContentFROM MyTable AS tINNER JOIN MyContentSites AS s ON s.ContentID = t.IDWHERE s.Site = @SiteFlag You could even expand the Site column to a new table MySites and change the Site column to a INT instead of VARCHAR as a foreign key to the new MySites table.Peter LarssonHelsingborg, Sweden |
 |
|
|
johnstern
Yak Posting Veteran
67 Posts |
Posted - 2007-04-26 : 18:30:24
|
| -I would very much want to normalize that database, but that is the way my client has it, but I will suggest to him- I am not using * , it was just for sample purposes ( but good to know :)) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:31:33
|
Fully normalized.[MyContents table]ContentID ContentText-- ------------------1 My text2 My other text3 My very other text[MySites table]SiteID SiteName-- ------------------1 CNN2 GOOGLE3 MSN[MyContentSites table]ID ContentID SiteID-- --------- -------1 1 12 1 23 2 34 3 25 3 3SELECT c.ContentTextFROM MyContents AS cINNER JOIN MyContentSites AS cs ON cs.ContentID = c.ContentIDINNER JOIN MySites AS s ON s.SiteID = cs.SiteIDWHERE s.SiteName = @SiteFlag This allows you to add any new Site without changing code!The MyContentSites table is the "bridge" between you content and your sites.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:34:34
|
quote: Originally posted by johnstern -I would very much want to normalize that database, but that is the way my client has it, but I will suggest to him
If it is not possible to change table layouts, use my first suggestion.Peter LarssonHelsingborg, Sweden |
 |
|
|
johnstern
Yak Posting Veteran
67 Posts |
Posted - 2007-04-26 : 18:37:23
|
| thank you very much for the fast responses, this is incredible, first time using this forum, I got answers within minutes to the questions I been breaking my head this whole afternoon |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 18:41:15
|
| http://www.datamodel.org/NormalizationRules.htmlPeter LarssonHelsingborg, Sweden |
 |
|
|
johnstern
Yak Posting Veteran
67 Posts |
Posted - 2007-04-26 : 18:50:21
|
quote: Originally posted by jezemine 2. I would probably use a single bitmask column instead of 3 separate bit columns (so you can easily add another provider without adding another column) www.elsasoft.org
Since you mentioned single bitmask column, I seached google for it but I am still confused on what a bitmask column, is. would you mind briefly explaining or pointing me to a site with some information. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 19:06:41
|
| A bitmask column is this:Rightmost bit has the value of 1 and means CNN when used.Second rightmost bit has the value of 2 and means GOOGLE when used.Third rightmost bit has the value of 4 and means MSN when used.If the value for whole column is 5, that mean both CNN and MSN is used. This approach make you hardcode the columns and you have to document them or at least remember the position used for each and every new Site.Bitmasked columns are a little harder to use than normalization as described above.Peter LarssonHelsingborg, Sweden |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-26 : 19:20:14
|
you don't have to hardcode the bit values, they could be in a lookup table. Peso's normalized solution is better than the bitmask idea. www.elsasoft.org |
 |
|
|
|