Avatar billede axkris Nybegynder
28. august 2005 - 10:43 Der er 24 kommentarer og
1 løsning

Optimering af forespørgsel, skal gøres i et sql-hug

Hej alle

Hvis nu jeg var go til sql, så havde jeg kørt denne sammen i en sætning / et recordsæt, så alt data blev fundet i et sql-hug, men det er jeg ikke, og derfor er min kode noget ineffektiv og indlæsningstiden er for høj. Kan du hjælpe mig med at optimere sætningen med subselects og joins mv, så alt hentes på en gang?

<%
'finder de forumer som har tilknyttet en eller flere moderatorer (undlad forumer som er specielle, låste eller inaktive)
strSQL = "SELECT FORUM_FORUM.FORUM_ID FROM FORUM_FORUM INNER JOIN FORUM_MODERATOR ON FORUM_FORUM.FORUM_ID = FORUM_MODERATOR.FORUM_ID INNER JOIN FORUM_CATEGORY ON FORUM_FORUM.CAT_ID = FORUM_CATEGORY.CAT_ID INNER JOIN FORUM_GROUPS ON FORUM_CATEGORY.CAT_ID = FORUM_GROUPS.GROUP_CATID WHERE FORUM_GROUPS.GROUP_ID = 6 AND FORUM_CATEGORY.CAT_ID <> 41 AND FORUM_FORUM.F_STATUS = 1"
set rs = dataConn.execute(strSQL)
while not rs.eof
    'finder de tilknyttede moderatorer (et forum kan godt have flere moderatorer)
    strSQL = "SELECT MEMBER_ID FROM FORUM_MODERATOR WHERE FORUM_ID = " & RS("FORUM_ID")
    set rs2 = dataConn.execute(strSQL)
    while not rs2.eof
        'indlæs brugerprofilen for de fundne moderatorer (en brugerprofil pr. moderator)
        strSQL = "SELECT M_NAME FROM FORUM_MEMBERS WHERE MEMBER_ID = " & RS2("MEMBER_ID")
        set rs3 = dataConn.execute(strSQL)
        while not rs3.eof
            'udskriv navnet på moderatoreren
            response.write rs3("M_NAME") & ","
            rs3.movenext
        wend
        rs2.movenext
    wend   
    rs.movenext
wend
                 
%>
Avatar billede terry Ekspert
28. august 2005 - 10:59 #1
axkris>as far as I can see your tables are related, so why dont you make a VIEW in Enterprise mangager and then copy the SQL into your ASP?
Avatar billede axkris Nybegynder
28. august 2005 - 11:04 #2
Ok, but what text do I insert? I can't just insert this code the with 3 selects.
Avatar billede axkris Nybegynder
28. august 2005 - 11:04 #3
the with = with the
Avatar billede terry Ekspert
28. august 2005 - 11:11 #4
In the first select you have an INNER JOIN. If you copyy this select int a new view you should see a thin line between the two tables. If I am righ then you should now be able to add the FORUM_MODERATOR table to the view and then make a JOIN using drag and drop. The join is on FORUM_ID. You can do the same for the FORUM_MEMBERS table and JOIN on MEMBER_ID.

But maybe this is not what you want?
Avatar billede axkris Nybegynder
28. august 2005 - 11:14 #5
I havn't before made any views in Enterprise, so I think I just have to work a bit with it...drag and drop.
Avatar billede axkris Nybegynder
28. august 2005 - 11:23 #6
Done... yahhoooo... it is easy... must use Enterprice's view from now on.

Thanx - press "svar" :-)
Avatar billede terry Ekspert
28. august 2005 - 11:24 #7
Good idea, it is actually an easy way for making your SQL if you aren to good doing it directly in code. I use it quite often myself :o)

The result you get from using JOINS is going to give you the data from first table (recordset = rs) for every related record in FORUM_MODERATOR.
Avatar billede terry Ekspert
28. august 2005 - 11:24 #8
:o)
Avatar billede terry Ekspert
28. august 2005 - 11:27 #9
You can actually make a view and select from that instead of making the SQL in code, it will be faster because the SQL you make in code needs to be checked before it gets executed. In a view this is already done!
Avatar billede axkris Nybegynder
28. august 2005 - 11:28 #10
Now I have saved the view (in Enterprise), but how do I call it from my asp code?

(I know that I can copy the code to my asp-page, but I would like know how to call the view instead... because it is much easier to mantain it from Enterprise.)
Avatar billede axkris Nybegynder
28. august 2005 - 11:30 #11
Thanx - I just asked the same question ;-)

But how do I make the call from the asp?
Avatar billede terry Ekspert
28. august 2005 - 11:31 #12
I would think you can just use

SELECT * FROM MyVIEW
Avatar billede axkris Nybegynder
28. august 2005 - 11:42 #13
This is fun - thank you very much - why haven't I known about views before ;-) I have so many, very complex sql requests that I - over time - can convert to views and the site will load quicker and I can mantain everything much easier :-D :-D :-D
Avatar billede terry Ekspert
28. august 2005 - 11:47 #14
You should also concider using stored procedures. They need to be called in a different way but are much more flexible than views.
Avatar billede axkris Nybegynder
28. august 2005 - 11:50 #15
Thanx :-)

What is stored procedures best for? (vs. views)
Avatar billede axkris Nybegynder
28. august 2005 - 11:52 #16
I cannot seem to drag and drop in stored procedures?
Avatar billede axkris Nybegynder
28. august 2005 - 11:55 #17
What will you recommend for this sql request? The view or a stored procdure?
Avatar billede terry Ekspert
28. august 2005 - 12:09 #18
No, its not possible to drag and drop with SP's. In SP's you can make much more complex selects. You can also make code to enable you to do things which you cant do in views.
In this case I would use a VIEW and when you find that using VIEWS isnt possible then you can try using SP's. But you can NOT select directly from an SP.
Avatar billede axkris Nybegynder
28. august 2005 - 12:32 #19
Thanx - I stick to views then :-)
Avatar billede axkris Nybegynder
28. august 2005 - 13:50 #20
How do I convert a sql-request (in vb-script) to a wiev when there is a "if" in it? Do I make 2 views or can I make 1 flexible view?

Please don't say SP, because I am not ready for that yet ;-)
Avatar billede terry Ekspert
28. august 2005 - 14:27 #21
can you give an example of when you need to use if?
Avatar billede axkris Nybegynder
28. august 2005 - 14:33 #22
strSQL =     "SET DATEFORMAT DMY" & vbNewLine & _
        "SELECT E.ID, E.Event_Name, D.[Date]" & vbNewLine & _
        "FROM    KALENDER_EVENTS E LEFT OUTER JOIN" & vbNewLine & _
        "        KALENDER_DATES D ON E.ID = D.EventID" & vbNewLine & _
        "WHERE    (D.[Date] = '" & dateCounter & "/" & navmonth & "/" & navyear & "')"
if strFirstTime = "false" then
strSQL = strSQL & " AND (E.LOCATION IN (" & strLocations & ")) AND (E.CATEGORY in (" & strCategories & ")) "
end if
           
if strSite <> "" then
    strSQL = strSQL & " AND (URL LIKE '%" & strSite & "%' OR DESCRIPTION LIKE '%" & strSite & "%') "
END IF
   
strSQL = strSQL & "ORDER BY D.[Date]"
set rs = DataConn.Execute(strSQL)
Avatar billede terry Ekspert
28. august 2005 - 14:42 #23
so it isnt just an IF you want to use, you also wan to use variables (dateCounter , navnmonth ...)?

I think you will need to use a SP for this :o(
Avatar billede axkris Nybegynder
28. august 2005 - 14:45 #24
ok :)
Avatar billede terry Ekspert
28. august 2005 - 14:48 #25
try looking in Books Online (help), there is quite a lot of information on SP's
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester