Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Databases General > Re: reduce time...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 6 Topic 3109 of 3295
Post > Topic >>

Re: reduce time for search query

by "Plamen Ratchev" <Plamen@[EMAIL PROTECTED] > Mar 6, 2008 at 05:43 PM

I do not see tables structure, indexes, the view definition, sample data, 
expected results...

Here is a quick attempt to improve this. Check the you have correct
indexes 
on join and filter columns where applicable.

In the main query below I removed two of the functions and used
sub-queries. 
Not sure that will help but was easier for me to see the logic. Changed
the 
main function to be table valued (you can go a step further here to make
it 
table valued for all users and join by user, I did not want to dig into
the 
logic there) so there will be no two calls in SELECT and WHERE, and some 
other cosmetic changes. Note that this is untested.

SELECT U.UserID,
          U.FirstName + ' ' + U.LastName AS 'Name',
          P.PropertyValue AS Location,
          N.headline AS Headline,
          I.industryName AS Industry,
          professionalInfo.summary AS Summary,
          professionalInfo.interests,
         (SELECT COUNT(*)
          FROM network AS N
          WHERE (N.memberId = U.UserID
               OR N.friendId = U.UserID)
             AND status = 1) AS Connections,
         (SELECT COUNT(*)
          FROM endorsements AS E
          WHERE E.memberId = U.UserID
             AND status = 'Accepted') AS Recommendations,
          F.generation AS Degree
FROM Users AS U
JOIN UserProfile AS P
  ON U.UserID = P.UserID
JOIN professionalInfo AS N
  ON U.UserID = N.memberId
JOIN industries AS I
  ON N.primaryIndustry = I.industryId
CROSS APPLY dbo.fnCommonFriendsStep(U.UserID, 36) AS F
WHERE EXISTS (SELECT *
                     FROM vw_search AS S
                     WHERE S.UserID = U.UserID)
  AND P.PropertyDefinitionID = 29
  AND F.generation >= 0
ORDER By Degree ASC



And here is the changed function:

CREATE FUNCTION dbo.fnCommonFriendsStep
 (@[EMAIL PROTECTED]
 INT, @[EMAIL PROTECTED]
 INT)
RETURNS @[EMAIL PROTECTED]
 (generation INT DEFAULT 0)
AS
BEGIN

INSERT INTO @[EMAIL PROTECTED]
 DEFAULT VALUES

IF @[EMAIL PROTECTED]
 = @[EMAIL PROTECTED]
 RETURN

DECLARE @[EMAIL PROTECTED]
 TABLE (generation INT, p INT)
DECLARE @[EMAIL PROTECTED]
 INT

SET @[EMAIL PROTECTED]
 = 0

INSERT @[EMAIL PROTECTED]
 (generation, p)
SELECT 0, memberId
FROM Network
WHERE friendId = @[EMAIL PROTECTED]
 0, friendId
FROM Network
WHERE memberId = @[EMAIL PROTECTED]
 NOT EXISTS (SELECT 1 FROM @[EMAIL PROTECTED]
 WHERE p = @[EMAIL PROTECTED]
)
   AND @[EMAIL PROTECTED]
 >= 0
BEGIN
SELECT @[EMAIL PROTECTED]
 = @[EMAIL PROTECTED]
 + 1

INSERT @[EMAIL PROTECTED]
 (generation, p)
SELECT @[EMAIL PROTECTED]
 memberId
FROM Network AS N
WHERE EXISTS (SELECT *
                     FROM @[EMAIL PROTECTED]
 AS F
                     WHERE N.friendid = F.p
                        AND F.generation = @[EMAIL PROTECTED]
 - 1)
    AND NOT EXISTS (SELECT *
                            FROM @[EMAIL PROTECTED]
 AS F
                            WHERE N.memberid = F.p)
UNION
SELECT @[EMAIL PROTECTED]
 friendId
FROM Network AS N
WHERE EXISTS (SELECT *
                     FROM @[EMAIL PROTECTED]
 AS F
                     WHERE N.memberid = F.p
                        AND F.generation = @[EMAIL PROTECTED]
 - 1)
   AND NOT EXISTS (SELECT *
                           FROM @[EMAIL PROTECTED]
 AS F
                           WHERE N.friendid = F.p)

IF @[EMAIL PROTECTED]
 = 0
SELECT @[EMAIL PROTECTED]
 = -2
END

UPDATE @[EMAIL PROTECTED]
 generation = @[EMAIL PROTECTED]
 + 1

RETURN
END



HTH,

Plamen Ratchev
http://www.SQLStudio.com
 




 6 Posts in Topic:
reduce time for search query
paankhate@[EMAIL PROTECTE  2008-02-26 04:41:24 
Re: reduce time for search query
Najm <najm25@[EMAIL PR  2008-02-27 10:59:09 
Re: reduce time for search query
"Plamen Ratchev"  2008-02-27 22:51:27 
Re: reduce time for search query
jan.afzal@[EMAIL PROTECTE  2008-03-05 02:18:20 
Re: reduce time for search query
"Plamen Ratchev"  2008-03-06 17:43:22 
Re: reduce time for search query
Erland Sommarskog <esq  2008-03-08 19:49:45 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 9:47:16 CST 2008.