Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement search on column of varchar type
    primarykey
    data
    text
    <p>I have a table <code>tblJobs</code> on which I want to implement search on a column <code>JobDescription</code>. I</p> <p>I have more than 70,000 rows in this table. I successfully implemented this, but the way I used is too slow. </p> <p>Can anyone pls help me to sort out this problem? The SQL stored procedure I am using is this:</p> <pre><code> Create Procedure Sp_SearchJobs ( @SearchValue varchar(Max) ) AS BEGIN WITH SplitTblJobs AS ( SELECT tbljobs.*, s.ListValue FROM tbljobs tbljobs CROSS APPLY dbo.FN_ListToTable(' ',tbljobs.JobDescription) AS s ) , WordMatchCount AS ( SELECT s.JobID,COUNT(*) AS CountOfWordMatch FROM dbo.FN_ListToTable('',@SearchValue) v INNER JOIN SplitTblJobs s ON v.ListValue=s.ListValue GROUP BY s.JobID HAVING COUNT(*)&gt;0 ) , SearchLen AS ( SELECT n.Number,SUBSTRING(@SearchValue,1,n.Number) AS PartialSearchValue FROM Numbers n WHERE n.Number&lt;=LEN(@SearchValue) ) , MatchLen AS ( SELECT tbljobs.JobID,MAX(l.Number) MatchStartLen FROM tbljobs tbljobs LEFT OUTER JOIN SearchLen l ON LEFT(tbljobs.JobDescription,l.Number)=l.PartialSearchValue GROUP BY tbljobs.JobID ) SELECT tbljobs.*,w.CountOfWordMatch,m.MatchStartLen FROM tbljobs tbljobs LEFT OUTER JOIN WordMatchCount w ON tbljobs.JobID=w.JobID LEFT OUTER JOIN MatchLen m ON tbljobs.JobID=m.JobID WHERE w.CountOfWordMatch&gt;0 ORDER BY w.CountOfWordMatch DESC,m.MatchStartLen DESC,LEN(tbljobs.JobDescription) DESC,tbljobs.JobDescription ASC END </code></pre> <p>Here in the function there is a function used. The code for that table-valued function is this:</p> <pre><code> Create FUNCTION [dbo].[FN_ListToTable] ( @SplitOn char(1) ,@List varchar(8000) ) RETURNS TABLE AS RETURN ( SELECT ListValue FROM (SELECT LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue FROM ( SELECT @SplitOn + @List + @SplitOn AS List2 ) AS dt INNER JOIN dbo.Numbers n ON n.Number &lt; LEN(dt.List2) WHERE SUBSTRING(List2, number, 1) = @SplitOn ) dt2 WHERE ListValue IS NOT NULL AND ListValue!=''); </code></pre> <p>There is another table used here . The structure of that is as follows:</p> <pre><code> CREATE TABLE [dbo].[Numbers] ( [Number] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_Numbers] PRIMARY KEY CLUSTERED ( [Number] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO </code></pre> <p>To make this procedure to run ..</p> <ol> <li>Make table described above</li> <li>Make function</li> <li>Make stored procedure.</li> </ol> <p>I want to implement search on decscription column of <code>tbljobs</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload