0

1) I am making an application in which I am showing a summary table. On clicking each summary column I can do sorting in ascending or descending manner. eg there are 5 columns

2) So in a normal instance I will be creating minimum 5 individual sql queries to provide this functionality. This can be created as prepared statement.

3) But is it a good practice to create a dynamic single sql code which can take into account all where conditions & all order by as I have done below? How will it impact DB performance? Since now I cannot write anything as prepared statement, will my execution be slow?

Please suggest Thanks

 CREATE DEFINER=`root`@`localhost` PROCEDURE `lSummary`(in fixedConditionList VARCHAR(1000),in OptionalConditionListForBackEnd VARCHAR(1000),in orderByColumnList VARCHAR(1000),in optionalJoinStatement VARCHAR(1000),in startLimit int(10),in OptionalFilterListWithAnd VARCHAR(1000),in OptionalFilterListWithOr VARCHAR(1000),in selectColumnList VARCHAR(10000),in EndLimit int(10))
BEGIN
    DECLARE SQLStatement varchar(5000);

    SET @SQLStatement = CONCAT("select l.lCaseId,concat('Staf') as canWrite,concat('Staf') as canShare,DATE_FORMAT(c.nextCourtDate,'%d-%m-%Y') as nextCourtDate,TIME_FORMAT( c.nextTime,'%h:%i %p' ) as nextTime,totalNumberOfSSteps,totalNumberOfSteps, ",selectColumnList," from litigation l left join(select relatedToId,relatedToPrefix,count(*) as totalNumberOfSteps from flowsubmilestoneinuse group by relatedToId) as wt on l.lCaseId=wt.relatedToId and l.lCasePrefix=wt.relatedToPrefix left join (select relatedToId,relatedToPrefix,count(*) as totalNumberOfSSteps from flowsubmilestoneinuse where milestoneReachedById Is not null group by relatedToId  ) as wc on l.lCaseId=wc.relatedToId and l.lCasePrefix=wc.relatedToPrefix left join detail c on c.lCaseId= l.lCaseId ",optionalJoinStatement," where (",fixedConditionList,") AND (",OptionalConditionListForBackEnd,") AND (",OptionalFilterListWithAnd,") AND (",OptionalFilterListWithOr,") order by ",orderByColumnList," Limit ",startLimit,EndLimit);


    EXECUTE STMT;     

END 
2
  • Certainly a prepared statement can accept a dynamic parameter at runtime. That is not a problem.
    – arkascha
    Feb 12, 2014 at 7:45
  • Hi can you please guide me with some reference to tutorials if possible. Thanks
    – Lajpat
    Feb 12, 2014 at 9:13

1 Answer 1

0

Regarding DB performance: In general - Yes, dynamic query affects performance. Under the hood, SQL Server engine creates query execution plan, even for dynamic queries. In this situation, you have to try and evaluate performance. Also, you can analyze execution plan and maybe improve it.

I can see you are pretty good with T-SQL, but I have an impression that you are using wrong tool for this. Sure, SQL Server can cope with this type of task, but my advise is: let the client side deal with sorting while using SQL Server for it's primary role - fetching the data.

Think abut this: once you have your set of data in memory, it's much faster to do in-memory sorting than sending a new request to server. Right?

Of course, it depends on client technology you are using. For example, if you are using web based client, there are lot of jQuery based products which can do it for you (see www.jtable.org, for example). Also, for .NET there are lot of table controls which are easy to use and optimized for sorting.

1
  • Thank you Kenan. I will look into your suggestions
    – Lajpat
    Feb 13, 2014 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.