| Author |
Topic |
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 13:58:08
|
| I have the following function which is working perfectly fine.The only problem I have with this function is a performance.Is it some how I could modify the function in order to get beetter performance?CREATE FUNCTION dbo.viewAnswers (@TranID decimal,@groupID varchar(10),@SortOrder1 int,@SortOrder2 int ) RETURNS varchar(8000)ASBEGIN Declare @Notes varchar(8000) Set @Notes='' Select @Notes=@Notes + Case Answer when 'Yes' then 'Y' else 'N' end + ' ' From answers wa JOIN groupRules gr ON wa.GroupID=gr.groupID AND gr.RuleID=wa.RuleID Where wa.GroupID=@groupID and tranid=@TRANID and ( sortOrder between @SortOrder1 and @SortOrder2 and gr.groupID=@groupID) order by sortorder Return @NotesEND |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 13:59:32
|
| Could you post what indexes you have on answers and groupRules tables?Tara Kizer |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 14:25:38
|
| It does work and return the right value. Why I should use sp instead of function? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 14:28:21
|
| Regardless if you use a UDF or a stored procedure, the performance will be the same. So let's try to fix the performance before deciding on switching methods. So the answer to my indexes question is...?Tara Kizer |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 14:35:06
|
| Before testing the performance between UDFs and stored procedures, we need to get the query inside of it to perform better. The performance difference between the two should be negligible. Let's take this one step at a time before saying you should use a stored procedure for this.Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 15:11:21
|
| It will return some value based on a passed arguments.the return values looks like this "Y Y N Y...." |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 15:15:12
|
| I'm going to repost this since you haven't provided the answer yet:Could you post what indexes you have on answers and groupRules tables?Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 15:27:12
|
| GroupID and RuleID |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 15:29:04
|
| answers:GroupID,RuleID,TranID groupRules: GroupID, RuleID |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 15:29:37
|
| On both tables?Could you post the DDL for both tables using Enterprise Manager's Generate SQL Script? Make sure to select the option to script the constraints and indexes in the last tab.Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 15:37:05
|
| ALTER TABLE [dbo].[GroupRules] ADD CONSTRAINT [PK_GroupRules] PRIMARY KEY CLUSTERED ( [GroupID], [RuleID] ) ON [PRIMARY] GOALTER TABLE [dbo].[Answers] ADD ( [TranID], [GroupID], [RuleID] ) ON [PRIMARY] GO |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 15:41:10
|
| Huh? Could you post the entire script and not just portions of it?Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 15:51:27
|
| My primary key is a clustered index |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 15:53:21
|
quote: Originally posted by sardinka My primary key is a clustered index
This isn't what was asked for.Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 16:06:29
|
| CREATE TABLE [dbo].[GroupRules] ( [GroupID] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [RuleID] [int] NOT NULL , [RuleText] [varchar] (400) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , ) ON [PRIMARY]GOCREATE TABLE [dbo].[Answers] ( [TranID] [numeric](18, 0) NOT NULL , [GroupID] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [RuleID] [int] NOT NULL , [Answer] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DateModified] [datetime] NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[GroupRules] ADD CONSTRAINT [PK_GroupRules] PRIMARY KEY CLUSTERED ([GroupID],[RuleID]) ON [PRIMARY] GOALTER TABLE [dbo].[Answers] ADDCONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED ([TranID],[GroupID],[RuleID]) ON [PRIMARY] GOINSERT INTO GroupRules(GroupID, RuleID, RuleText)SELECT '111111',1, 'Answer1' UNION ALLSELECT '111111',2, 'Answer1' UNION ALLSELECT '111111',3, 'Answer1' UNION ALLSELECT '111111',1, 'Answer1' UNION ALLSELECT '777777',2, 'Answer1' UNION ALLSELECT '111111',4, 'Answer1' UNION ALLSELECT '777777',1, 'Answer1' UNION ALLSELECT '888888',1, 'Answer1' UNION ALLINSERT INTO Answers(TranID, GroupID, RuleID,Answer,DateModified)SELECT 123,'111111', 2,'Yes',GetDate() UNION ALLSELECT 123,'111111', 1,'No',GetDate() UNION ALLSELECT 123,'111111', 4,'Yes',GetDate() UNION ALLSELECT 123,'111111', 3,'Yes',GetDate() UNION ALLSELECT 456,'111111', 2,'Yes',GetDate() UNION ALLSELECT 456,'111111', 2,'Yes',GetDate() UNION ALLSELECT 456,'111111', 1,'Yes',GetDate() UNION ALLSELECT 456,'111111', 4,'Yes',GetDate() UNION ALLSELECT 456,'111111', 3,'Yes',GetDate() UNION ALLSELECT 789,'888888', 1,'Yes',GetDate() UNION ALLSELECT 777,'888888', 4,'Yes',GetDate() UNION ALLSELECT 987,'777777', 1,'Yes',GetDate() UNION ALLSELECT 987,'777777', 2,'No',GetDate() RESULT:123,'111111','N Y Y Y'456,'111111','Y Y Y Y'777,'888888','Y '789,'888888','Y '987,'777777','Y N' |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 16:09:58
|
| I believe it won't be able to use an index on the answers table for this part of the query:ON wa.GroupID=gr.groupID AND gr.RuleID=wa.RuleIDSince TranID is the first column in the index and isn't included here. If you ran the following query:Select answerFrom answers wa JOIN groupRules gr ON wa.GroupID=gr.groupID AND gr.RuleID=wa.RuleIDWhere wa.GroupID=@groupID and tranid=@TRANIDand ( sortOrder between @SortOrder1 and @SortOrder2 and gr.groupID=@groupID)order by sortorderWhat does the execution plan show?Tara Kizer |
 |
|
|
sardinka
Posting Yak Master
142 Posts |
Posted - 2006-10-17 : 16:27:02
|
| Select Cost:0%Sort cost:0%Parallelism: cost:0%Nested Loops:cost:6%Answers: Cost 94% |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-10-17 : 18:02:51
|
| Well we need more information than that from the execution plan. Run SET SHOWPLAN_TEXT ON and post the results here.Tara Kizer |
 |
|
|
Next Page
|