473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Fine tuning this sql-query. help!

Hi, I have problem running this query. It will time out for me...
My database are small just about 200 members.

I have a site for swaping appartments (rental). my query should look
for match
in a triangle. Like this member A -> B->C
A give his appartment to B. B gives his appartment to C and finally
C gives his appartment to A

Soo my query looks for matching parameters like rooms, location, size
and
soo on..

I have one table for existing appartments and one for "whanted
appartments"
and 1 table called "intresse" where members can store "yes" or "no" if
they are interessted in a appartment.

I also have a table called "omrade" to store locations of interest.

Hope you can helpe me with some tip soo i can run this query in a few
seconds instead of 20-30 sec

Thanks M


SELECT

F.medlemsNr as medlemsNr, F.lfId AS lfId, F.ort AS ort, F.gatuadress
AS gatuadress, F.gatuNr AS gatuNr, F.rum AS rum,F.storlek AS
storlek,
F.hyra AS hyra, count(F.medlemsNr) As hits

FROM

medlem08 A, medlem08 B, medlem08 C, lagenhetF08 D,
lagenhetO08 E, lagenhetF08 F, lagenhetO08 G, lagenhetF08 H,
lagenhetO08 I
WHERE

D.rum >= I.rumMin AND D.rum <= I.rumMax AND
D.storlek >= I.storlekMin AND D.storlek <= I.storlekMax AND
I.hyraMax = 0" & " OR D.hyra <= I.hyraMax) AND
I.balkong = '" & "" & "' OR D.balkong = I.balkong) AND
(I.badkar = '" & "" & "' OR D.badkar = I.badkar) AND
(I.bredband = '" & "" & "' OR D.bredband = I.bredband) AND
(I.hiss = '" & "" & "' OR D.hiss = I.hiss) AND
(I.spis = '" & "" & "' OR D.spis = I.spis) AND
(I.brf = '" & "" & "' OR D.brf = I.brf) AND

D.postNr IN (select postNr from ONSKEMAL08 where loId=I.loId) AND
F.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=A.medlemsNr) AND

H.rum >= G.rumMin AND H.rum <= G.rumMax AND
H.storlek >= G.storlekMin AND H.storlek <= G.storlekMax AND
(G.hyraMax = 0" & " OR H.hyra <= G.hyraMax) AND
(G.balkong = '" & "" & "' OR H.balkong = G.balkong) AND
(G.badkar = '" & "" & "' OR H.badkar = G.badkar) AND
(G.bredband = '" & "" & "' OR H.bredband = G.bredband) AND
(G.spis = '" & "" & "' OR H.spis = G.spis) AND
(G.brf = '" & "" & "' OR H.brf = G.brf) AND

H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId) AND

F.rum >= E.rumMin AND F.rum <= E.rumMax AND
F.storlek >= E.storlekMin AND F.storlek <= E.storlekMax AND
(E.hyraMax = 0" & " OR F.hyra <= E.hyraMax) AND
(E.balkong = '" & "" & "' OR F.balkong = E.balkong) AND
(E.badkar = '" & "" & "' OR F.badkar = E.badkar) AND
(E.bredband = '" & "" & "' OR F.bredband = E.bredband) AND
(E.hiss = '" & "" & "' OR F.hiss = E.hiss) AND
(E.spis = '" & "" & "' OR F.spis = E.spis) AND
(E.brf = '" & "" & "' OR F.brf = E.brf) AND
F.postNr IN (select postNr from ONSKEMAL08 where loId=E.loId) AND

A.medlemsNr=D.medlemsNr AND A.medlemsNr=E.medlemsNr AND

B.medlemsNr<>A.medlemsNr AND C.medlemsNr<>A.medlemsNr AND
B.medlemsNr<>C.medlemsNr AND
B.sparr<>1 AND C.sparr<>1 AND

A.typ=11 AND A.medlemsNr=" & session("medlemsNr") & " AND
B.medlemsNr=F.medlemsNr AND B.medlemsNr=G.medlemsNr AND
B.typ=11 AND A.triangel=1 AND B.triangel=1 AND C.triangel=1 AND " &_
C.medlemsNr=H.medlemsNr AND C.medlemsNr=I.medlemsNr AND
C.typ=11 group by F.lfId, F.medlemsNr,F.ort,
F.gatuadress,F.gatuNr,F.rum,F.storlek,F.hyra
Jul 20 '05 #1
9 3387
majsen (mi****@home.staertesjoen.se) writes:
Hi, I have problem running this query. It will time out for me...
My database are small just about 200 members.

I have a site for swaping appartments (rental). my query should look
for match
in a triangle. Like this member A -> B->C
A give his appartment to B. B gives his appartment to C and finally
C gives his appartment to A

Soo my query looks for matching parameters like rooms, location, size
and
soo on..

I have one table for existing appartments and one for "whanted
appartments"
and 1 table called "intresse" where members can store "yes" or "no" if
they are interessted in a appartment.

I also have a table called "omrade" to store locations of interest.

Hope you can helpe me with some tip soo i can run this query in a few
seconds instead of 20-30 sec


Without knowledge of the tables, their indexes, and the number of rows
in each table, there is not much advice I can give. There query is complex,
and all the conditions like:

(G.hyraMax = 0" & " OR H.hyra <= G.hyraMax) AND

may make indexes difficult to use anyway. Did I understand that
medlem08 only had 200 rows. How many apartments are involved? And
what is the difference between lagenhetF08 and lagenhet008? (And
why all this 08? Can only Stockholmers apply? :-) And why all these
" & " in the middle of it all? Why they make no damage, the query
was already difficult to read.

Once advice is to use ANSI-join instead of old-stlye joins. This should
have no impact on peformance, but it could make the query easier to
read, since you separate join conditions and filter conditions. An
example of this syntax:
SELECT o.orderid, o.orderdate, od.qty, od.product
FROM orders o
JOIN orderdetails o ON o.orderid = od.orderid
WHERE o.customerid = 12
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Hi,

my tables

lagenhetF08 ->existing appartment 200 posts
lagenhetO08->whanted appartment 200 posts
member08 ->the member 200 posts
Intresse08->intresst yes or no ? posts
omrade08->location 30 + posts

One member can have 1 or 2 appartment and
look for 1 or 2 appartments

about the " & " signs...i forgott to remove them :-)

I have used the tool tracking sql-server and applied
my workload to the index tuning wizard. But it only
slowed down the query even more...

Mabe i could not use selfjoin?
What do u say about my subquerys. Do they slow down
the process?
Regards Mikael


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
mikael Hellström (mi****@home.staertesjoen.se) writes:
lagenhetF08 ->existing appartment 200 posts
lagenhetO08->whanted appartment 200 posts
member08 ->the member 200 posts
Intresse08->intresst yes or no ? posts
omrade08->location 30 + posts
Not an astonish lot. Then again, let's see, you have nine tables
in triplets of three. I have not been able to get through your
query entirely (have you considered rewriting in ANSI joins yet?),
but give the requirements, I guess you will have to cross-join
each triplet with each other. Since:
One member can have 1 or 2 appartment and
look for 1 or 2 appartments
We could assume that each triplet returns, say, 250 rows. That's
250^3 rows, which is over 15 million! Suddently 20-30 seconds starts
to feel quite reasonable....

So what is happening - basically - is that SQL Server first has
to construct all those 15 million rows, and then apply the filter
conditions to remove the pity handful few that matches.

Now, in fact for one of the triplets you are providing a key value,
which should reduces the number of rows to filter to 62500. Then again,
if your site grows, and you day have 5000 users, you have 5000^2 rows.

I know too little about this, to say anything for sure, but it looks
like just submitting a naïve query is not likely to cut it. You may
have to compute matches in advance.
What do u say about my subquerys. Do they slow down
the process?


They are certainly not speed boosters...

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

Hi again,

Here are my query rewrited...

this is what my querty look like when sql-server rewrite
my question on the new ANSI standard...
It look insane. I now can se the amount of timeconsuming
operations it have to execute to answer my question.
It work all right. This query deliver 1 (one)
matching result :-)
it takes about 30 sec + to execute.

My question are ... How can i rewrite the question
to improve performence.

I have tried to split the question into 3 parts without
any sucesss.

Regards Mikael

'--------------------------------------------------------
SELECT F.medlemsNr AS medlemsNr, F.lfId AS lfId, F.ort
AS ort, F.gatuAdress AS gatuadress, F.gatuNr AS gatuNr, F.rum AS rum,
F.storlek AS storlek,
F.hyra AS hyra, COUNT(F.medlemsNr) AS hits

FROM lagenhetF08 H INNER JOIN
lagenhetO08 G ON H.rum >= G.rumMin AND H.rum <= G.rumMax AND H.storlek
= G.storlekMin AND H.storlek <= G.storlekMax INNER JOIN lagenhetF08 F INNER JOIN
lagenhetO08 E ON F.rum >= E.rumMin AND F.rum <= E.rumMax AND F.storlek= E.storlekMin AND F.storlek <= E.storlekMax INNER JOIN

medlem08 A INNER JOIN
lagenhetO08 I INNER JOIN
lagenhetF08 D ON I.rumMin <= D.rum AND I.rumMax >= D.rum AND
I.storlekMin <= D.storlek AND I.storlekMax >= D.storlek ON
A.medlemsNr = D.medlemsNr ON E.medlemsNr = A.medlemsNr INNER JOIN
medlem08 B ON A.medlemsNr <> B.medlemsNr AND F.medlemsNr = B.medlemsNr
INNER JOIN
medlem08 C ON A.medlemsNr <> C.medlemsNr AND B.medlemsNr <> C.medlemsNr
AND I.medlemsNr = C.medlemsNr ON
G.medlemsNr = B.medlemsNr AND H.medlemsNr = C.medlemsNr
WHERE (I.hyraMax = 0) AND (I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.hyraMax = 0) AND (G.balkong
= ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.hyraMax = 0) AND (E.balkong = ' '
OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) OR
(I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.hyraMax = 0) AND (G.balkong
= ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.hyraMax = 0) AND (E.balkong = ' '
OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (D.hyra
<= I.hyraMax) OR
(I.hyraMax = 0) AND (I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.balkong = ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.hyraMax = 0) AND (E.balkong = ' '
OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (H.hyra
<= G.hyraMax) OR
(I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.balkong = ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.hyraMax = 0) AND (E.balkong = ' '
OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (D.hyra
<= I.hyraMax) AND (H.hyra <= G.hyraMax) OR
(I.hyraMax = 0) AND (I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.hyraMax = 0) AND (G.balkong
= ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.balkong = ' ' OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (F.hyra
<= E.hyraMax) OR
(I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.hyraMax = 0) AND (G.balkong
= ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.balkong = ' ' OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (D.hyra
<= I.hyraMax) AND (F.hyra <= E.hyraMax) OR
(I.hyraMax = 0) AND (I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.balkong = ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.balkong = ' ' OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (H.hyra
<= G.hyraMax) AND (F.hyra <= E.hyraMax) OR
(I.balkong = ' ' OR
D.balkong = I.balkong) AND (I.badkar = ' ' OR
D.badkar = I.badkar) AND (I.bredband = ' ' OR
D.bredband = I.bredband) AND (I.hiss = ' ' OR
D.hiss = I.hiss) AND (I.spis = ' ' OR
D.spis = I.spis) AND (I.brf = ' ' OR
D.brf = I.brf) AND (D.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = I.loId)) AND (F.medlemsNr NOT IN
(SELECT medlemsNr2
FROM INTRESSE08
WHERE medlemsNr1 = A.medlemsNr)) AND (G.balkong = ' ' OR
H.balkong = G.balkong) AND (G.badkar = ' ' OR
H.badkar = G.badkar) AND (G.bredband = ' ' OR
H.bredband = G.bredband) AND (G.hiss = ' ' OR
H.hiss = G.hiss) AND (G.spis = ' ' OR
H.spis = G.spis) AND (G.brf = ' ' OR
H.brf = G.brf) AND (H.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = G.loId)) AND (E.balkong = ' ' OR
F.balkong = E.balkong) AND (E.badkar = ' ' OR
F.badkar = E.badkar) AND (E.bredband = ' ' OR
F.bredband = E.bredband) AND (E.hiss = ' ' OR
F.hiss = E.hiss) AND (E.spis = ' ' OR
F.spis = E.spis) AND (E.brf = ' ' OR
F.brf = E.brf) AND (F.postNr IN
(SELECT postNr
FROM ONSKEMAL08
WHERE loId = E.loId)) AND (B.sparr <> 1) AND (C.sparr <> 1) AND
(A.typ = 11) AND (A.medlemsNr = '100255') AND (B.typ = 11) AND
(A.triangel = 1)
AND (B.triangel = 1) AND (C.triangel = 1) AND (C.typ = 11) AND (D.hyra
<= I.hyraMax) AND (H.hyra <= G.hyraMax) AND (F.hyra <= E.hyraMax)
GROUP BY F.lfId, F.medlemsNr, F.ort, F.gatuAdress, F.gatuNr, F.rum,
F.storlek, F.hyra

'-------------------------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
mikael Hellström (mi****@home.staertesjoen.se) writes:
Here are my query rewrited...

this is what my querty look like when sql-server rewrite
my question on the new ANSI standard...
It look insane. I now can se the amount of timeconsuming
operations it have to execute to answer my question.
It work all right. This query deliver 1 (one)
matching result :-)
it takes about 30 sec + to execute.

My question are ... How can i rewrite the question
to improve performence.

I have tried to split the question into 3 parts without
any sucesss.


I don't know what happened during that rewrite, but I had to give up going
through the query. Your original query was some 50-60 lines, but this
one is over 300 lines. I think managed to include some duplicate conditions.

Rewriting an old-style join into ANSI join takes some exercice. Conditions
like A.MedlemsID <> B.MedlemsID are not really join conditions, but rather
filter. I think the right answer is that you don't really have any join
conditions between the members - it's a cross join.

I'm a afraid that whole thing is too complex to cover in a newsgroup post.
To fully attack it, I would have to have the CREATE TABLE statements
for the tables, as well as sample data in some form. I have to admit that
at this point I don't much idea of where to start. I can see what the
problem comes from - all the cross joins. But I know too little about
the tables and the business rules to say what to do about it.

Maybe some partial attack is good. If I understood things right, one
member may have one or two apartments. Maybe it's a good idea, to find
the inquiring member's apartment, get that data, and insert that into
the query. That does at least reduce the complexity of the query.

It might even be that you should first match that member against the rest
for possible matches, and then take those matches to a triangle test.

And, no, this is by no means a trivial problem, but appears to be a
tough challenge even for a seasoned SQL programmer.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Hi again,
and thank you for looking into my problem...

I have thought of 2 things to do

1) mabe i could skip the table 'omrade' and store
the data in lagenhetO08 (appartment whanted). This
should speed up the query but it would not be
a very good databas design thou. It would be
a nvarchar string 120, 121 and so on... And i will
use 'Like' to search thru the string for match.
Seems wierd...but it may speed up the process?

2) I have tried to split my query (as you se below) into
tre parts. The first 2 question run very fast...1-3 sec
but the last query slowes it down to 30 sec...again.

first query: who whant my appartment (who match)?
second query: witch apartment do i want? (who match)?
third query: search matches from result 1 and 2

This should end up in a triangle match.

i create two new tables to store the result
the key 'medlemsNr'. I thought to use not permanent
tables if this would work. Just session tables.

Soo...my problem is ... can i speed up query 3 in some way?
Should i change my database design? And skip the table
Omrade?
Regards Mikael Hellström

-- Hämta medlemmar som är intresserade av min lägenhet--

strSQL = SELECT H.medlemsNr as medlemsNr
INTO INTRESSERADE_AV_MIN
FROM medlem08 A, medlem08 C, lagenhetF08 D, lagenhetO08 I , lagenhetF08
H

WHERE
D.rum >= I.rumMin AND D.rum <= I.rumMax AND
D.storlek >= I.storlekMin AND D.storlek <= I.storlekMax AND
(I.hyraMax = 0 OR D.hyra <= I.hyraMax) AND
(I.balkong = ' ' OR D.balkong = I.balkong) AND
(I.badkar = ' ' OR D.badkar = I.badkar) AND
(I.bredband = ' ' OR D.bredband = I.bredband) AND
(I.hiss = ' ' OR D.hiss = I.hiss) AND
(I.spis = ' ' OR D.spis = I.spis) AND
(I.brf = ' ' OR D.brf = I.brf) AND
D.postNr IN (select postNr from ONSKEMAL08 where loId=I.loId) AND
I.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=A.medlemsNr) AND
A.medlemsNr=100255 AND C.triangel=1 AND A.medlemsNr=D.medlemsNr AND
H.medlemsNr=C.medlemsNr AND C.medlemsNr=I.medlemsNr AND
C.typ=11 order by medlemsNr
'- Hämta medlemmar som jag är intresserad av -----

strSQL2 = SELECT G.medlemsNr as medlemsNr
INTO INTRESSE_FOR_MIG
FROM medlem08 A, medlem08 B, lagenhetF08 F, lagenhetO08 G , lagenhetO08
E

WHERE
F.rum >= E.rumMin AND F.rum <= E.rumMax AND
F.storlek >= E.storlekMin AND F.storlek <= E.storlekMax AND
(E.hyraMax = 0 OR F.hyra <= E.hyraMax) AND
(E.balkong = ' ' OR F.balkong = E.balkong) AND
(E.badkar = ' ' OR F.badkar = E.badkar) AND
(E.bredband = ' ' OR F.bredband = E.bredband) AND
(E.hiss = ' ' OR F.hiss = E.hiss) AND
(E.spis = ' ' OR F.spis = E.spis) AND
(E.brf = ' ' OR F.brf = E.brf) AND
F.postNr IN (select postNr from ONSKEMAL08 where loId=E.loId) AND
E.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=A.medlemsNr) AND
A.medlemsNr=100255 AND B.triangel=1 AND A.medlemsNr=E.medlemsNr AND
G.medlemsNr=B.medlemsNr AND B.medlemsNr=F.medlemsNr AND
B.typ=11 order by medlemsNr

'-- Är de intresserade av varandras objekt ---------------

strSQL3 = SELECT DISTINCT G.medlemsNr as medlemsNr

FROM medlem08 B, medlem08 C, lagenhetF08 H, lagenhetO08 G, lagenhetF08
F,
INTRESSE_FOR_MIG M, INTRESSERADE_AV_MIN J
WHERE
H.rum >= G.rumMin AND H.rum <= G.rumMax AND
H.storlek >= G.storlekMin AND H.storlek <= G.storlekMax AND
(G.hyraMax = 0 OR H.hyra <= G.hyraMax) AND
(G.balkong = ' ' OR H.balkong = G.balkong) AND
(G.badkar = ' ' OR H.badkar = G.badkar) AND
(G.bredband = ' ' OR H.bredband = G.bredband) AND
(G.hiss = ' ' OR H.hiss = G.hiss) AND
(G.spis = ' ' OR H.spis = G.spis) AND
(G.brf = ' ' OR H.brf = G.brf) AND
H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId) AND
G.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=B.medlemsNr) AND
B.medlemsNr=G.medlemsNr AND C.medlemsNr=H.medlemsNr AND
F.medlemsNr=G.medlemsNr AND H.medlemsNr=C.medlemsNr AND B.triangel=1 AND
G.medlemsNr=J.medlemsNr AND
H.medlemsNr=M.medlemsNr AND B.typ=11 AND C.typ=11
order by medlemsNr

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7
mikael Hellström (mi****@home.staertesjoen.se) writes:
Soo...my problem is ... can i speed up query 3 in some way?
Should i change my database design? And skip the table
Omrade?
Changing the database design is indeed an option worth considering.
Just please don't ask me how. As I've said, this issue is a little
too complex to be easily handled in a newsgroup post, since it requires
detailed knowledge about the business problem.

But would you could try is to take out the subqueries and the DISTINCT
from that last query, only see the effect.

Still, though, your query:
strSQL3 = SELECT DISTINCT G.medlemsNr as medlemsNr

FROM medlem08 B, medlem08 C, lagenhetF08 H, lagenhetO08 G, lagenhetF08
F,
INTRESSE_FOR_MIG M, INTRESSERADE_AV_MIN J
WHERE
H.rum >= G.rumMin AND H.rum <= G.rumMax AND
H.storlek >= G.storlekMin AND H.storlek <= G.storlekMax AND
(G.hyraMax = 0 OR H.hyra <= G.hyraMax) AND
(G.balkong = ' ' OR H.balkong = G.balkong) AND
(G.badkar = ' ' OR H.badkar = G.badkar) AND
(G.bredband = ' ' OR H.bredband = G.bredband) AND
(G.hiss = ' ' OR H.hiss = G.hiss) AND
(G.spis = ' ' OR H.spis = G.spis) AND
(G.brf = ' ' OR H.brf = G.brf) AND
H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId) AND
G.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=B.medlemsNr) AND
B.medlemsNr=G.medlemsNr AND C.medlemsNr=H.medlemsNr AND
F.medlemsNr=G.medlemsNr AND H.medlemsNr=C.medlemsNr AND B.triangel=1 AND
G.medlemsNr=J.medlemsNr AND
H.medlemsNr=M.medlemsNr AND B.typ=11 AND C.typ=11
order by medlemsNr


Is effectively a cross join between B-G-F-J and C-H-M, which means
that there are many combiantions that SQL Server will have to consider.

In the above, only F, M and J only contributes with MedlemsID. It could
be worth to test by moving them into EXISTS clauses, as demonstrated

strSQL3 = SELECT DISTINCT G.medlemsNr as medlemsNr

FROM medlem08 B, medlem08 C, lagenhetF08 H, lagenhetO08 G, lagenhetF08
F,
INTRESSE_FOR_MIG M
WHERE
H.rum >= G.rumMin AND H.rum <= G.rumMax AND
H.storlek >= G.storlekMin AND H.storlek <= G.storlekMax AND
(G.hyraMax = 0 OR H.hyra <= G.hyraMax) AND
(G.balkong = ' ' OR H.balkong = G.balkong) AND
(G.badkar = ' ' OR H.badkar = G.badkar) AND
(G.bredband = ' ' OR H.bredband = G.bredband) AND
(G.hiss = ' ' OR H.hiss = G.hiss) AND
(G.spis = ' ' OR H.spis = G.spis) AND
(G.brf = ' ' OR H.brf = G.brf) AND
H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId) AND
G.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=B.medlemsNr) AND
B.medlemsNr=G.medlemsNr AND C.medlemsNr=H.medlemsNr AND
F.medlemsNr=G.medlemsNr AND H.medlemsNr=C.medlemsNr AND B.triangel=1 AND
H.medlemsNr=M.medlemsNr AND B.typ=11 AND C.typ=11
AND EXISTS (SELECT *
FROM INTRESSERADE_AV_MIN J
WHERE J.MedlemsID = G.MedlemsID)
order by medlemsNr

You might even go as far as trying this with all tables but G and H.

No promises for success, though.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8
Hi,
i start to get somewere now.

I replaced all sub querys

from
H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId)

to
"H.postNr = L.postNr AND L.loId=G.loId
And i replaced

G.medlemsNr Not IN (select medlemsNr2 from INTRESSE08 where
medlemsNr1=B.medlemsNr)


to

not exists(select medlemsNr2 from INTRESSE08 where medlemsNr1=" &
session("medlemsNr") & " AND medlemsNr2=F.medlemsNr AND bytestyp='T')
That change and the run of 'index tuning wisard' make
the query execute in 1-2 seconds compare to 30 sec.

Cool...so the strange thing now is that my query is
very slow 10 sec + at my webbhotel. That must have to do
with the fine tuning on my index that i perform on my
local server and i will do that at my webbhotell to and
it should work out fine.

I would like to thank you for taking time to help me
with this problem.

If you would like to swap your appartment, give me a call,
i will add you to my memberlist for free :-)
Thanks again
Regards Mikael

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #9
mikael Hellström (mi****@home.staertesjoen.se) writes:
from
H.postNr IN (select postNr from ONSKEMAL08 where loId=G.loId)

to
"H.postNr = L.postNr AND L.loId=G.loId
Hm, this could possibly generate duplicates in the output. But you
could address that with a DISTINCT before the column list.
That change and the run of 'index tuning wisard' make
the query execute in 1-2 seconds compare to 30 sec.


Great to hear that you got working better!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tober | last post by:
Anyone happen to know of any good books on performance tuning for MS-SQL 2000? I am currently building a Cognos datawarehouse and would like to ensure that the db is tuned for peak performance. ...
3
by: xixi | last post by:
can someone explain to me what is internal p lock, internal s lock, internal v lock? when i have IS lock or IX lock , i always have these internal locks together for the application handle ...
3
by: mleal | last post by:
Does anyone have some more detailed information about how Oracle and MS implement / allow Tuning on Oracle 10g and SQL Server 2005 and the differences between them? Which of them, In a deep...
13
by: atlaste | last post by:
Hi, I'm currently developing an application that uses a lot of computational power, disk access and memory caching (to be more exact: an information retrieval platform). In these kind of...
0
by: =?Utf-8?B?SkhhbGV5?= | last post by:
Our system is: IIS Server: dual Intel Xeon 2.80 GHz, 4 GB Ram Windows Server 2003 SP2 IIS 6.0 SQL Server: dual Intel Xeon 2.80 GHz, 4 GB Ram (separate server) Windows Server 2003 SP2 SQL...
2
by: Hevan | last post by:
Sql performance tuning in DB2 Table Trans has multiplle records for each employee. I am updating id into lookup from trans . Currently, it takes 3 minutes to update lookup table. Both lookup and...
4
by: Tommy Hayes | last post by:
Hello all, I want to use the SQL Server 2005 Tuning Advisor on our database, and I'm hoping someone here can just confirm the steps for me. We have a 10GB database that has a number of...
0
by: sachinsamuel | last post by:
Hi Experts, I am new to PostgreSQL. I got a task to optimize quries which are executed in my database. My frontend is Java and the middleware is hybernate. The queries are automatically created...
3
by: dunleav1 | last post by:
In 9.1 and 9.5 (Linux 64 bit) when a buffer pool is set to self- tuning, how are blocks configured in respect to blocked vs non-blocked when self-tuning is set to on? (ie) I have one bufferpool...
1
by: tvnaidu | last post by:
what is the Maximum optimization and fine-tuning flags for C code to get bext benchmarking number?. developed libraries using static libs currently.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.