Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Sir This is my stored procedure in sql server 2005
which retrieve 20000 rows result but it working slow.
but my another sp which retrieve 40000 rows result working nice .

any mistake or tips regarding my stored procedure is acceptable.


please give me idea.

SQL
ALTER PROCEDURE [dbo].[SearchCatalogOrUrlInformation] 
	
@username int,
@urlorcatalog varchar(100)
AS

BEGIN  

SET NOCOUNT ON;
DECLARE 
@M_username int,
@M_urlorcatalog varchar(100)

SET @M_username=@username	
SET @M_urlorcatalog=@urlorcatalog

    
SELECT  COMPANY.urlorcatalog AS 'Source',COMPANY.companyname AS 'Companies',CATEGORY.categoryname AS 'Category',REGION.regionname AS 'Region',usr.name AS 'User',count(email.email) AS 'Collection',COMPANY.dataentrytype as 'Type', COMPANY.date AS 'Date'

FROM dbo.companyinfo COMPANY

INNER JOIN dbo.emailinfo email ON COMPANY.dataID = email.DataID
INNER JOIN dbo.userinfo usr ON COMPANY.userid = usr.ID
LEFT OUTER  JOIN dbo.category CATEGORY ON COMPANY.category=CATEGORY.ID 
LEFT OUTER  JOIN dbo.regioninfo REGION ON COMPANY.region=REGION.ID 

 WHERE
 COMPANY.userid=@M_username AND COMPANY.urlorcatalog LIKE '%'+ @M_urlorcatalog + '%'

GROUP BY
 
COMPANY.urlorcatalog,COMPANY.companyname,CATEGORY.categoryname,REGION.regionname,COMPANY.dataentrytype,COMPANY.date,usr.name
  
ORDER BY  COMPANY.date DESC


END
Posted
Updated 25-Aug-11 0:17am
v3
Comments
Prerak Patel 25-Aug-11 6:14am    
Use code block for code segments.
Herman<T>.Instance 25-Aug-11 6:23am    
do both stored procedures use left outer joins?

Take your SQL statement and put it in a new query window

Then choose Query -> Display Estimated Execution Plan

If you don't understand query plans, have a read of SQL Tuning Tutorial - Understanding a Database Execution Plan (1)[^]

This will help you understand where your query is taking the time to execute. Maybe a new index is required. Do all of your tables have clustered indexes?

LIKE in the WHERE clause can sometimes causes isues, have a look here

http://myitforum.com/cs2/blogs/jnelson/archive/2007/11/16/108354.aspx[^]

Again though, see how the query plan statistics are being used? Get to know this tool, it will help you write efficient SQL and tune your statements
 
Share this answer
 
Comments
udusat13 25-Aug-11 6:54am    
please view my sp once and check any mistake

thanks.............
Dylan Morley 25-Aug-11 6:55am    
No obvious mistake, would need to see the query plan to know where the most time is being spent.

udusat13 25-Aug-11 7:02am    
http://www.codeproject.com/KB/database/sql-tuning-tutorial-1.aspx

i have read this for execution plan but i am getting confused how to implement it
Dylan Morley 25-Aug-11 7:12am    
In SQL Studio, choose 'New Query' from the toolbar

Paste your SQL statement into query window.

Now choose Query -> Display Estimated Execution Plan

Look at the results
Without knowing the table structure / execution plan (likely missing an index) here is one way to try and speed up the query. This builds a limit list of based on the company. With the thought that it will cut down on the number of rows to be considered in the where clause as a result of the left joins.

SQL
DECLARE 
@M_username int,
@M_urlorcatalog varchar(100)
 
SET @M_username=@username	
SET @M_urlorcatalog=@urlorcatalog
 
-------------------------------------------------
-- Limit list of just the users / URL Catalogs --
-------------------------------------------------
SELECT
	c.dataID
	, c.userid
	, c.companyname
	, c.urlorcatalog
        , c.dataentrytype
	, c.[Date]
INTO #Company
FROM 	companyinfo c
WHERE
 COMPANY.userid=@M_username AND 
 COMPANY.urlorcatalog LIKE '%'+ @M_urlorcatalog + '%'
	
	
SELECT  
	COMPANY.urlorcatalog AS 'Source'
	, COMPANY.companyname AS 'Companies'
	, CATEGORY.categoryname AS 'Category'
	, REGION.regionname AS 'Region'
	, usr.name AS 'User'
	, count(email.email) AS 'Collection'
	, COMPANY.dataentrytype as 'Type'
	, COMPANY.date AS 'Date'
FROM #Company COMPANY
	INNER JOIN dbo.emailinfo email ON COMPANY.dataID = email.DataID
	INNER JOIN dbo.userinfo usr ON COMPANY.userid = usr.ID
	LEFT OUTER  JOIN dbo.category CATEGORY ON COMPANY.category=CATEGORY.ID 
	LEFT OUTER  JOIN dbo.regioninfo REGION ON COMPANY.region=REGION.ID  
GROUP BY
 COMPANY.urlorcatalog
 , COMPANY.companyname
 , CATEGORY.categoryname
 , REGION.regionname
 , COMPANY.dataentrytype
 , COMPANY.date,usr.name 
ORDER BY  COMPANY.date DESC

DROP TABLE #Company
 
Share this answer
 
Comments
udusat13 26-Aug-11 1:04am    
Thanks for watching my sp.
Regarding ur suggestion i have modified code like this


ALTER PROCEDURE [dbo].[SearchCatalogOrUrlInformation]

@username int,
@urlorcatalog varchar(100)
AS

BEGIN

SET NOCOUNT ON;


DECLARE
@M_username int,
@M_urlorcatalog varchar(100)

SET @M_username=@username
SET @M_urlorcatalog=@urlorcatalog

-------------------------------------------------
-- Limit list of just the users / URL Catalogs --
-------------------------------------------------

SELECT
c.dataID
, c.userid
, c.companyname
, c.urlorcatalog
, c.dataentrytype
, c.[Date]
INTO #Company
FROM companyinfo c
WHERE
COMPANY.userid=@M_username AND
COMPANY.urlorcatalog LIKE '%'+ @M_urlorcatalog + '%'


SELECT
COMPANY.urlorcatalog AS 'Source'
, COMPANY.companyname AS 'Companies'
, CATEGORY.categoryname AS 'Category'
, REGION.regionname AS 'Region'
, usr.name AS 'User'
, count(email.email) AS 'Collection'
, COMPANY.dataentrytype as 'Type'
, COMPANY.date AS 'Date'
FROM #Company COMPANY
INNER JOIN dbo.emailinfo email ON COMPANY.dataID = email.DataID
INNER JOIN dbo.userinfo usr ON COMPANY.userid = usr.ID
LEFT OUTER JOIN dbo.category CATEGORY ON COMPANY.category=CATEGORY.ID
LEFT OUTER JOIN dbo.regioninfo REGION ON COMPANY.region=REGION.ID
GROUP BY
COMPANY.urlorcatalog
, COMPANY.companyname
, CATEGORY.categoryname
, REGION.regionname
, COMPANY.dataentrytype
, COMPANY.date
,usr.name

ORDER BY COMPANY.date DESC

DROP TABLE #Company

but it generate error message like
Incorrect syntax near '#Company'.
near this line
DROP TABLE #Company

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900