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