Click to See Complete Forum and Search --> : stored procedures


audux
04-20-2006, 09:50 PM
a nice day to every one, this is my first time to post here, i was informed about this community and i hope that i could share here

i am using sql 2000

i have a problem with retrieving records in tables, i would like to ask if there are ways on how could i optimized my query for faster returns.

the concept is summation of fields in a table which applies to 2 different tables, joining the tables together for other data.

this is how the content of my procedure look like:

SELECT A.pname, A.pid, B.case, C.cfldsum, D.dfldsum, E.efldsum
FROM a INNER JOIN
b ON A.pid = B.pid INNER JOIN
( -- defining 1st alias
SELECT case, sum(fld1 * fld2) as cfldsum
FROM xC
WHERE tbl = 'OLD'
GROUP BY case
) c ON B.case = C.case LEFT JOIN
( -- defining 2nd alias
SELECT case, sum(fld1 * fld2) as dfldsum
FROM xC
WHERE tbl = 'NEW'
GROUP BY case
) d ON B.case = D.case LEFT JOIN
( -- defining 3rd alias
SELECT case, sum(fld1 + fld2 + fld3 + fld4 + fld5) AS efdlsum
FROM xE
GROUP BY case
) e ON B.case = E.case
WHERE C.cfldsum - (D.dfldsum + E.efldsum) IS NOT NULL AND C.cfldsum - (D.dfldsum + E.efldsum) > 0

do i have to modify my query, or there are functions that i have to set.
anyway table xC contains around 700,000 records.

chazzy
04-20-2006, 09:57 PM
just wondering, have you used any sql analyzer or anything like that to analyze your query against your actual database? if you use anything like toad (not sure what works w/ sql server, i would imagine it has something like that built in actually) that will give you a full plan.

you can also run "explain (put your sql here)" to see what it says.

audux
04-20-2006, 10:27 PM
yes i was using one, it was sql query analyzer,

like what i usually do, type in my query there and once it works pastes that query in creating sp's

kryptonboy22
04-21-2006, 05:37 AM
try to read this
http://www.webdeveloper.com/forum/showthread.php?t=35393&highlight=slow+SQL+query

previous thread with the same issue how to manage large record.
hope it helps!!!